(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / pthread / lio_listio.c
blob5b98837b0d706367b1b8f317e65333583cedae08
1 /* Enqueue and list of read or write requests.
2 Copyright (C) 1997,1998,1999,2000,2001,2003 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
19 02111-1307 USA. */
21 #ifndef lio_listio
22 #include <aio.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <unistd.h>
28 #include "aio_misc.h"
30 #define LIO_OPCODE_BASE 0
31 #endif
33 /* We need this special structure to handle asynchronous I/O. */
34 struct async_waitlist
36 int counter;
37 struct sigevent sigev;
38 struct waitlist list[0];
42 int
43 lio_listio (mode, list, nent, sig)
44 int mode;
45 struct aiocb *const list[];
46 int nent;
47 struct sigevent *sig;
49 struct sigevent defsigev;
50 struct requestlist *requests[nent];
51 int cnt;
52 volatile int total = 0;
53 int result = 0;
55 /* Check arguments. */
56 if (mode != LIO_WAIT && mode != LIO_NOWAIT)
58 __set_errno (EINVAL);
59 return -1;
62 if (sig == NULL)
64 defsigev.sigev_notify = SIGEV_NONE;
65 sig = &defsigev;
68 /* Request the mutex. */
69 pthread_mutex_lock (&__aio_requests_mutex);
71 /* Now we can enqueue all requests. Since we already acquired the
72 mutex the enqueue function need not do this. */
73 for (cnt = 0; cnt < nent; ++cnt)
74 if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
76 list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
77 requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
78 (list[cnt]->aio_lio_opcode
79 | LIO_OPCODE_BASE));
81 if (requests[cnt] != NULL)
82 /* Successfully enqueued. */
83 ++total;
84 else
85 /* Signal that we've seen an error. `errno' and the error code
86 of the aiocb will tell more. */
87 result = -1;
89 else
90 requests[cnt] = NULL;
92 if (total == 0)
94 /* We don't have anything to do except signalling if we work
95 asynchronously. */
97 /* Release the mutex. We do this before raising a signal since the
98 signal handler might do a `siglongjmp' and then the mutex is
99 locked forever. */
100 pthread_mutex_unlock (&__aio_requests_mutex);
102 if (mode == LIO_NOWAIT)
104 #ifdef BROKEN_THREAD_SIGNALS
105 __aio_notify_only (sig,
106 sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
107 #else
108 __aio_notify_only (sig);
109 #endif
112 return result;
114 else if (mode == LIO_WAIT)
116 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
117 struct waitlist waitlist[nent];
118 int oldstate;
120 total = 0;
121 for (cnt = 0; cnt < nent; ++cnt)
123 assert (requests[cnt] == NULL || list[cnt] != NULL);
125 if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
127 waitlist[cnt].cond = &cond;
128 waitlist[cnt].next = requests[cnt]->waiting;
129 waitlist[cnt].counterp = &total;
130 waitlist[cnt].sigevp = NULL;
131 #ifdef BROKEN_THREAD_SIGNALS
132 waitlist[cnt].caller_pid = 0; /* Not needed. */
133 #endif
134 requests[cnt]->waiting = &waitlist[cnt];
135 ++total;
139 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
140 points we must be careful. We added entries to the waiting lists
141 which we must remove. So defer cancelation for now. */
142 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
144 while (total > 0)
145 pthread_cond_wait (&cond, &__aio_requests_mutex);
147 /* Now it's time to restore the cancelation state. */
148 pthread_setcancelstate (oldstate, NULL);
150 /* Release the conditional variable. */
151 if (pthread_cond_destroy (&cond) != 0)
152 /* This must never happen. */
153 abort ();
155 else
157 struct async_waitlist *waitlist;
159 waitlist = (struct async_waitlist *)
160 malloc (sizeof (struct async_waitlist)
161 + (nent * sizeof (struct waitlist)));
163 if (waitlist == NULL)
165 __set_errno (EAGAIN);
166 result = -1;
168 else
170 #ifdef BROKEN_THREAD_SIGNALS
171 pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
172 #endif
173 total = 0;
175 for (cnt = 0; cnt < nent; ++cnt)
177 assert (requests[cnt] == NULL || list[cnt] != NULL);
179 if (requests[cnt] != NULL
180 && list[cnt]->aio_lio_opcode != LIO_NOP)
182 waitlist->list[cnt].cond = NULL;
183 waitlist->list[cnt].next = requests[cnt]->waiting;
184 waitlist->list[cnt].counterp = &waitlist->counter;
185 waitlist->list[cnt].sigevp = &waitlist->sigev;
186 #ifdef BROKEN_THREAD_SIGNALS
187 waitlist->list[cnt].caller_pid = caller_pid;
188 #endif
189 requests[cnt]->waiting = &waitlist->list[cnt];
190 ++total;
194 waitlist->counter = total;
195 waitlist->sigev = *sig;
199 /* Release the mutex. */
200 pthread_mutex_unlock (&__aio_requests_mutex);
202 return result;