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.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 /* We need this special structure to handle asynchronous I/O. */
34 struct sigevent sigev;
35 struct waitlist list[0];
40 lio_listio (mode, list, nent, sig)
42 struct aiocb *const list[];
46 struct sigevent defsigev;
47 struct requestlist *requests[nent];
49 volatile int total = 0;
52 /* Check arguments. */
53 if (mode != LIO_WAIT && mode != LIO_NOWAIT)
61 defsigev.sigev_notify = SIGEV_NONE;
65 /* Request the mutex. */
66 pthread_mutex_lock (&__aio_requests_mutex);
68 /* Now we can enqueue all requests. Since we already acquired the
69 mutex the enqueue function need not do this. */
70 for (cnt = 0; cnt < nent; ++cnt)
71 if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
73 list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
74 requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
75 list[cnt]->aio_lio_opcode);
77 if (requests[cnt] != NULL)
78 /* Successfully enqueued. */
81 /* Signal that we've seen an error. `errno' and the error code
82 of the aiocb will tell more. */
90 /* We don't have anything to do except signalling if we work
93 /* Release the mutex. We do this before raising a signal since the
94 signal handler might do a `siglongjmp' and then the mutex is
96 pthread_mutex_unlock (&__aio_requests_mutex);
98 if (mode == LIO_NOWAIT)
99 __aio_notify_only (sig,
100 sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
104 else if (mode == LIO_WAIT)
106 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
107 struct waitlist waitlist[nent];
111 for (cnt = 0; cnt < nent; ++cnt)
113 assert (requests[cnt] == NULL || list[cnt] != NULL);
115 if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
117 waitlist[cnt].cond = &cond;
118 waitlist[cnt].next = requests[cnt]->waiting;
119 waitlist[cnt].counterp = &total;
120 waitlist[cnt].sigevp = NULL;
121 waitlist[cnt].caller_pid = 0; /* Not needed. */
122 requests[cnt]->waiting = &waitlist[cnt];
127 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
128 points we must be careful. We added entries to the waiting lists
129 which we must remove. So defer cancelation for now. */
130 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
133 pthread_cond_wait (&cond, &__aio_requests_mutex);
135 /* Now it's time to restore the cancelation state. */
136 pthread_setcancelstate (oldstate, NULL);
138 /* Release the conditional variable. */
139 if (pthread_cond_destroy (&cond) != 0)
140 /* This must never happen. */
145 struct async_waitlist *waitlist;
147 waitlist = (struct async_waitlist *)
148 malloc (sizeof (struct async_waitlist)
149 + (nent * sizeof (struct waitlist)));
151 if (waitlist == NULL)
153 __set_errno (EAGAIN);
158 pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
161 for (cnt = 0; cnt < nent; ++cnt)
163 assert (requests[cnt] == NULL || list[cnt] != NULL);
165 if (requests[cnt] != NULL
166 && list[cnt]->aio_lio_opcode != LIO_NOP)
168 waitlist->list[cnt].cond = NULL;
169 waitlist->list[cnt].next = requests[cnt]->waiting;
170 waitlist->list[cnt].counterp = &waitlist->counter;
171 waitlist->list[cnt].sigevp = &waitlist->sigev;
172 waitlist->list[cnt].caller_pid = caller_pid;
173 requests[cnt]->waiting = &waitlist->list[cnt];
178 waitlist->counter = total;
179 waitlist->sigev = *sig;
183 /* Release the mutex. */
184 pthread_mutex_unlock (&__aio_requests_mutex);