* elf/cache.c: Use <> rather than "" #includes.
[glibc.git] / sysdeps / pthread / lio_listio.c
blob29dc9d6eab9d0a7cedf793718764412f8504f5f0
1 /* Enqueue and list of read or write requests.
2 Copyright (C) 1997,1998,1999,2000,2001,2003,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #ifndef lio_listio
23 #include <aio.h>
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <unistd.h>
29 #include <aio_misc.h>
31 #define LIO_OPCODE_BASE 0
32 #endif
34 /* We need this special structure to handle asynchronous I/O. */
35 struct async_waitlist
37 int counter;
38 struct sigevent sigev;
39 struct waitlist list[0];
43 int
44 lio_listio (mode, list, nent, sig)
45 int mode;
46 struct aiocb *const list[];
47 int nent;
48 struct sigevent *sig;
50 struct sigevent defsigev;
51 struct requestlist *requests[nent];
52 int cnt;
53 volatile int total = 0;
54 int result = 0;
56 /* Check arguments. */
57 if (mode != LIO_WAIT && mode != LIO_NOWAIT)
59 __set_errno (EINVAL);
60 return -1;
63 if (sig == NULL)
65 defsigev.sigev_notify = SIGEV_NONE;
66 sig = &defsigev;
69 /* Request the mutex. */
70 pthread_mutex_lock (&__aio_requests_mutex);
72 /* Now we can enqueue all requests. Since we already acquired the
73 mutex the enqueue function need not do this. */
74 for (cnt = 0; cnt < nent; ++cnt)
75 if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
77 list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
78 requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
79 (list[cnt]->aio_lio_opcode
80 | LIO_OPCODE_BASE));
82 if (requests[cnt] != NULL)
83 /* Successfully enqueued. */
84 ++total;
85 else
86 /* Signal that we've seen an error. `errno' and the error code
87 of the aiocb will tell more. */
88 result = -1;
90 else
91 requests[cnt] = NULL;
93 if (total == 0)
95 /* We don't have anything to do except signalling if we work
96 asynchronously. */
98 /* Release the mutex. We do this before raising a signal since the
99 signal handler might do a `siglongjmp' and then the mutex is
100 locked forever. */
101 pthread_mutex_unlock (&__aio_requests_mutex);
103 if (mode == LIO_NOWAIT)
105 #ifdef BROKEN_THREAD_SIGNALS
106 __aio_notify_only (sig,
107 sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
108 #else
109 __aio_notify_only (sig);
110 #endif
113 return result;
115 else if (mode == LIO_WAIT)
117 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
118 struct waitlist waitlist[nent];
119 int oldstate;
121 total = 0;
122 for (cnt = 0; cnt < nent; ++cnt)
124 assert (requests[cnt] == NULL || list[cnt] != NULL);
126 if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
128 waitlist[cnt].cond = &cond;
129 waitlist[cnt].next = requests[cnt]->waiting;
130 waitlist[cnt].counterp = &total;
131 waitlist[cnt].sigevp = NULL;
132 #ifdef BROKEN_THREAD_SIGNALS
133 waitlist[cnt].caller_pid = 0; /* Not needed. */
134 #endif
135 requests[cnt]->waiting = &waitlist[cnt];
136 ++total;
140 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
141 points we must be careful. We added entries to the waiting lists
142 which we must remove. So defer cancelation for now. */
143 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
145 while (total > 0)
146 pthread_cond_wait (&cond, &__aio_requests_mutex);
148 /* Now it's time to restore the cancelation state. */
149 pthread_setcancelstate (oldstate, NULL);
151 /* Release the conditional variable. */
152 if (pthread_cond_destroy (&cond) != 0)
153 /* This must never happen. */
154 abort ();
156 else
158 struct async_waitlist *waitlist;
160 waitlist = (struct async_waitlist *)
161 malloc (sizeof (struct async_waitlist)
162 + (nent * sizeof (struct waitlist)));
164 if (waitlist == NULL)
166 __set_errno (EAGAIN);
167 result = -1;
169 else
171 #ifdef BROKEN_THREAD_SIGNALS
172 pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
173 #endif
174 total = 0;
176 for (cnt = 0; cnt < nent; ++cnt)
178 assert (requests[cnt] == NULL || list[cnt] != NULL);
180 if (requests[cnt] != NULL
181 && list[cnt]->aio_lio_opcode != LIO_NOP)
183 waitlist->list[cnt].cond = NULL;
184 waitlist->list[cnt].next = requests[cnt]->waiting;
185 waitlist->list[cnt].counterp = &waitlist->counter;
186 waitlist->list[cnt].sigevp = &waitlist->sigev;
187 #ifdef BROKEN_THREAD_SIGNALS
188 waitlist->list[cnt].caller_pid = caller_pid;
189 #endif
190 requests[cnt]->waiting = &waitlist->list[cnt];
191 ++total;
195 waitlist->counter = total;
196 waitlist->sigev = *sig;
200 /* Release the mutex. */
201 pthread_mutex_unlock (&__aio_requests_mutex);
203 return result;