Update.
[platform/upstream/glibc.git] / rt / lio_listio.c
1 /* Enqueue and list of read or write requests.
2    Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <aio.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25
26 #include "aio_misc.h"
27
28
29 /* We need this special structure to handle asynchronous I/O.  */
30 struct async_waitlist
31   {
32     int counter;
33     struct sigevent sigev;
34     struct waitlist list[0];
35   };
36
37
38 int
39 lio_listio (mode, list, nent, sig)
40      int mode;
41      struct aiocb *const list[];
42      int nent;
43      struct sigevent *sig;
44 {
45   struct sigevent defsigev;
46   struct requestlist *requests[nent];
47   int cnt;
48   volatile int total = 0;
49   int result = 0;
50
51   /* Check arguments.  */
52   if (mode != LIO_WAIT && mode != LIO_NOWAIT)
53     {
54       __set_errno (EINVAL);
55       return -1;
56     }
57
58   if (sig == NULL)
59     {
60       defsigev.sigev_notify = SIGEV_NONE;
61       sig = &defsigev;
62     }
63
64   /* Request the mutex.  */
65   pthread_mutex_lock (&__aio_requests_mutex);
66
67   /* Now we can enqueue all requests.  Since we already acquired the
68      mutex the enqueue function need not do this.  */
69   for (cnt = 0; cnt < nent; ++cnt)
70     if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
71       {
72         list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
73         requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
74                                                list[cnt]->aio_lio_opcode);
75
76         if (requests[cnt] != NULL)
77           /* Successfully enqueued.  */
78           ++total;
79         else
80           /* Signal that we've seen an error.  `errno' and the error code
81              of the aiocb will tell more.  */
82           result = -1;
83       }
84     else
85       requests[cnt] = NULL;
86
87   if (total == 0)
88     {
89       /* We don't have anything to do except signalling if we work
90          asynchronously.  */
91
92       /* Release the mutex.  We do this before raising a signal since the
93          signal handler might do a `siglongjmp' and then the mutex is
94          locked forever.  */
95       pthread_mutex_unlock (&__aio_requests_mutex);
96
97       if (mode == LIO_NOWAIT)
98         __aio_notify_only (sig,
99                            sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
100
101       return result;
102     }
103   else if (mode == LIO_WAIT)
104     {
105       pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
106       struct waitlist waitlist[nent];
107       int oldstate;
108
109       total = 0;
110       for (cnt = 0; cnt < nent; ++cnt)
111         if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
112           {
113             waitlist[cnt].cond = &cond;
114             waitlist[cnt].next = requests[cnt]->waiting;
115             waitlist[cnt].counterp = &total;
116             waitlist[cnt].sigevp = NULL;
117             waitlist[cnt].caller_pid = 0;       /* Not needed.  */
118             requests[cnt]->waiting = &waitlist[cnt];
119             ++total;
120           }
121
122       /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
123          points we must be careful.  We added entries to the waiting lists
124          which we must remove.  So defer cancelation for now.  */
125       pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
126
127       while (total > 0)
128         pthread_cond_wait (&cond, &__aio_requests_mutex);
129
130       /* Now it's time to restore the cancelation state.  */
131       pthread_setcancelstate (oldstate, NULL);
132
133       /* Release the conditional variable.  */
134       if (pthread_cond_destroy (&cond) != 0)
135         /* This must never happen.  */
136         abort ();
137     }
138   else
139     {
140       struct async_waitlist *waitlist;
141
142       waitlist = (struct async_waitlist *)
143         malloc (sizeof (struct async_waitlist)
144                 + (nent * sizeof (struct waitlist)));
145
146       if (waitlist == NULL)
147         {
148           __set_errno (EAGAIN);
149           result = -1;
150         }
151       else
152         {
153           pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
154           total = 0;
155
156           for (cnt = 0; cnt < nent; ++cnt)
157             if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
158               {
159                 waitlist->list[cnt].cond = NULL;
160                 waitlist->list[cnt].next = requests[cnt]->waiting;
161                 waitlist->list[cnt].counterp = &waitlist->counter;
162                 waitlist->list[cnt].sigevp = &waitlist->sigev;
163                 waitlist->list[cnt].caller_pid = caller_pid;
164                 requests[cnt]->waiting = &waitlist->list[cnt];
165                 ++total;
166               }
167
168           waitlist->counter = total;
169           waitlist->sigev = *sig;
170         }
171     }
172
173   /* Release the mutex.  */
174   pthread_mutex_unlock (&__aio_requests_mutex);
175
176   return result;
177 }