Update.
[platform/upstream/glibc.git] / rt / lio_listio64.c
1 /* Enqueue and list of read or write requests, 64bit offset version.
2    Copyright (C) 1997, 1998 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
25 #include "aio_misc.h"
26
27
28 /* We need this special structure to handle asynchronous I/O.  */
29 struct async_waitlist
30   {
31     int counter;
32     struct sigevent sigev;
33     struct waitlist list[0];
34   };
35
36
37 int
38 lio_listio64 (mode, list, nent, sig)
39      int mode;
40      struct aiocb64 *const list[];
41      int nent;
42      struct sigevent *sig;
43 {
44   struct requestlist *requests[nent];
45   int cnt;
46   volatile int total = 0;
47   int result = 0;
48
49   /* Check arguments.  */
50   if (mode != LIO_WAIT && mode != LIO_NOWAIT)
51     {
52       __set_errno (EINVAL);
53       return -1;
54     }
55
56   /* Request the mutex.  */
57   pthread_mutex_lock (&__aio_requests_mutex);
58
59   /* Now we can enqueue all requests.  Since we already acquired the
60      mutex the enqueue function need not do this.  */
61   for (cnt = 0; cnt < nent; ++cnt)
62     if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
63       {
64         requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
65                                                (list[cnt]->aio_lio_opcode
66                                                 | 128));
67         if (requests[cnt] != NULL)
68           /* Successfully enqueued.  */
69           ++total;
70         else
71           /* Signal that we've seen an error.  `errno' and the error code
72              of the aiocb will tell more.  */
73           result = -1;
74       }
75
76   if (total == 0)
77     {
78       /* We don't have anything to do except signalling if we work
79          asynchronously.  */
80       if (mode == LIO_NOWAIT)
81         __aio_notify_only (sig);
82     }
83   else if (mode == LIO_WAIT)
84     {
85       pthread_cond_t cond;
86       struct waitlist waitlist[nent];
87       int oldstate;
88
89       /* Initialize the conditional variable.  */
90       pthread_cond_init (&cond, NULL);
91
92       total = 0;
93       for (cnt = 0; cnt < nent; ++cnt)
94         if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP
95             && requests[cnt] != NULL)
96           {
97             waitlist[cnt].cond = &cond;
98             waitlist[cnt].next = requests[cnt]->waiting;
99             waitlist[cnt].counterp = &total;
100             waitlist[cnt].sigevp = NULL;
101             requests[cnt]->waiting = &waitlist[cnt];
102             ++total;
103           }
104
105       /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
106          points we must be careful.  We added entries to the waiting lists
107          which we must remove.  So defer cancelation for now.  */
108       pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
109
110       while (total > 0)
111         pthread_cond_wait (&cond, &__aio_requests_mutex);
112
113       /* Now it's time to restore the cancelation state.  */
114       pthread_setcancelstate (oldstate, NULL);
115
116       /* Release the conditional variable.  */
117       if (pthread_cond_destroy (&cond) != 0)
118         /* This must never happen.  */
119         abort ();
120     }
121   else
122     {
123       struct async_waitlist *waitlist;
124
125       waitlist = (struct async_waitlist *)
126         malloc (sizeof (struct async_waitlist)
127                 + (nent * sizeof (struct waitlist)));
128
129       if (waitlist == NULL)
130         {
131           __set_errno (EAGAIN);
132           result = -1;
133         }
134       else
135         {
136           total = 0;
137
138           for (cnt = 0; cnt < nent; ++cnt)
139             if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP
140                 && requests[cnt] != NULL)
141               {
142                 waitlist->list[cnt].cond = NULL;
143                 waitlist->list[cnt].next = requests[cnt]->waiting;
144                 waitlist->list[cnt].counterp = &waitlist->counter;
145                 waitlist->list[cnt].sigevp = &waitlist->sigev;
146                 requests[cnt]->waiting = &waitlist->list[cnt];
147                 ++total;
148               }
149
150           waitlist->counter = total;
151           waitlist->sigev = *sig;
152         }
153     }
154
155   /* Release the mutex.  */
156   pthread_mutex_unlock (&__aio_requests_mutex);
157
158   return result;
159 }