]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/pthread/lio_listio.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / sysdeps / pthread / lio_listio.c
CommitLineData
cbdee279 1/* Enqueue and list of read or write requests.
6d3aff23 2 Copyright (C) 1997,1998,1999,2000,2001,2003,2005,2006
8f480b4b 3 Free Software Foundation, Inc.
cbdee279
UD
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6
7 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
cbdee279
UD
11
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
41bdb6e2 15 Lesser General Public License for more details.
cbdee279 16
41bdb6e2 17 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
cbdee279 20
6bc0b954 21#ifndef lio_listio
cbdee279 22#include <aio.h>
9f964ae3 23#include <assert.h>
cbdee279 24#include <errno.h>
d71b808a 25#include <stdlib.h>
3a9eb648 26#include <unistd.h>
cbdee279 27
8f480b4b 28#include <aio_misc.h>
cbdee279 29
6bc0b954
UD
30#define LIO_OPCODE_BASE 0
31#endif
cbdee279 32
6d3aff23
UD
33#include <shlib-compat.h>
34
35
d71b808a
UD
36/* We need this special structure to handle asynchronous I/O. */
37struct async_waitlist
38 {
39 int counter;
40 struct sigevent sigev;
41 struct waitlist list[0];
42 };
43
44
6d3aff23
UD
45/* The code in glibc 2.1 to glibc 2.4 issued only one event when all
46 requests submitted with lio_listio finished. The existing practice
47 is to issue events for the individual requests as well. This is
48 what the new code does. */
49#if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
50# define LIO_MODE(mode) ((mode) & 127)
51# define NO_INDIVIDUAL_EVENT_P(mode) ((mode) & 128)
52#else
53# define LIO_MODE(mode) mode
54# define NO_INDIVIDUAL_EVENT_P(mode) 0
55#endif
56
57
58static int
59lio_listio_internal (int mode, struct aiocb *const list[], int nent,
60 struct sigevent *sig)
cbdee279 61{
025a5afa 62 struct sigevent defsigev;
d71b808a 63 struct requestlist *requests[nent];
cbdee279 64 int cnt;
d71b808a 65 volatile int total = 0;
cbdee279
UD
66 int result = 0;
67
025a5afa
UD
68 if (sig == NULL)
69 {
70 defsigev.sigev_notify = SIGEV_NONE;
71 sig = &defsigev;
72 }
73
d71b808a
UD
74 /* Request the mutex. */
75 pthread_mutex_lock (&__aio_requests_mutex);
cbdee279
UD
76
77 /* Now we can enqueue all requests. Since we already acquired the
d71b808a 78 mutex the enqueue function need not do this. */
cbdee279
UD
79 for (cnt = 0; cnt < nent; ++cnt)
80 if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
d71b808a 81 {
6d3aff23
UD
82 if (NO_INDIVIDUAL_EVENT_P (mode))
83 list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
84
2ace5721 85 requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
6bc0b954
UD
86 (list[cnt]->aio_lio_opcode
87 | LIO_OPCODE_BASE));
d71b808a
UD
88
89 if (requests[cnt] != NULL)
90 /* Successfully enqueued. */
91 ++total;
92 else
93 /* Signal that we've seen an error. `errno' and the error code
94 of the aiocb will tell more. */
95 result = -1;
96 }
2ace5721
UD
97 else
98 requests[cnt] = NULL;
d71b808a
UD
99
100 if (total == 0)
101 {
102 /* We don't have anything to do except signalling if we work
103 asynchronously. */
3a9eb648
UD
104
105 /* Release the mutex. We do this before raising a signal since the
106 signal handler might do a `siglongjmp' and then the mutex is
107 locked forever. */
108 pthread_mutex_unlock (&__aio_requests_mutex);
109
6d3aff23 110 if (LIO_MODE (mode) == LIO_NOWAIT)
b61c8aba
UD
111 {
112#ifdef BROKEN_THREAD_SIGNALS
3a9eb648
UD
113 __aio_notify_only (sig,
114 sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
b61c8aba
UD
115#else
116 __aio_notify_only (sig);
117#endif
118 }
3a9eb648
UD
119
120 return result;
d71b808a 121 }
6d3aff23 122 else if (LIO_MODE (mode) == LIO_WAIT)
d71b808a 123 {
679d83ba 124#ifndef DONT_NEED_AIO_MISC_COND
18de8c73 125 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
a334319f 126 int oldstate;
679d83ba
UD
127#endif
128 struct waitlist waitlist[nent];
d71b808a
UD
129
130 total = 0;
131 for (cnt = 0; cnt < nent; ++cnt)
9f964ae3
UD
132 {
133 assert (requests[cnt] == NULL || list[cnt] != NULL);
134
135 if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
136 {
679d83ba 137#ifndef DONT_NEED_AIO_MISC_COND
9f964ae3 138 waitlist[cnt].cond = &cond;
679d83ba 139#endif
9759bbf1 140 waitlist[cnt].result = &result;
9f964ae3
UD
141 waitlist[cnt].next = requests[cnt]->waiting;
142 waitlist[cnt].counterp = &total;
143 waitlist[cnt].sigevp = NULL;
b61c8aba 144#ifdef BROKEN_THREAD_SIGNALS
9f964ae3 145 waitlist[cnt].caller_pid = 0; /* Not needed. */
b61c8aba 146#endif
9f964ae3
UD
147 requests[cnt]->waiting = &waitlist[cnt];
148 ++total;
149 }
150 }
d71b808a 151
679d83ba
UD
152#ifdef DONT_NEED_AIO_MISC_COND
153 AIO_MISC_WAIT (result, total, NULL, 0);
154#else
9759bbf1 155 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancellation
d71b808a 156 points we must be careful. We added entries to the waiting lists
9759bbf1 157 which we must remove. So defer cancellation for now. */
d71b808a
UD
158 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
159
160 while (total > 0)
6b9c2e67 161 pthread_cond_wait (&cond, &__aio_requests_mutex);
d71b808a 162
9759bbf1 163 /* Now it's time to restore the cancellation state. */
d71b808a 164 pthread_setcancelstate (oldstate, NULL);
b9337b6a
UD
165
166 /* Release the conditional variable. */
167 if (pthread_cond_destroy (&cond) != 0)
168 /* This must never happen. */
169 abort ();
679d83ba 170#endif
9759bbf1
UD
171
172 /* If any of the I/O requests failed, return -1 and set errno. */
173 if (result != 0)
b957e864
UD
174 {
175 __set_errno (result == EINTR ? EINTR : EIO);
176 result = -1;
177 }
d71b808a
UD
178 }
179 else
180 {
181 struct async_waitlist *waitlist;
182
183 waitlist = (struct async_waitlist *)
184 malloc (sizeof (struct async_waitlist)
185 + (nent * sizeof (struct waitlist)));
186
187 if (waitlist == NULL)
188 {
189 __set_errno (EAGAIN);
190 result = -1;
191 }
cbdee279 192 else
d71b808a 193 {
b61c8aba 194#ifdef BROKEN_THREAD_SIGNALS
3a9eb648 195 pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
b61c8aba 196#endif
d71b808a 197 total = 0;
cbdee279 198
d71b808a 199 for (cnt = 0; cnt < nent; ++cnt)
9f964ae3
UD
200 {
201 assert (requests[cnt] == NULL || list[cnt] != NULL);
202
203 if (requests[cnt] != NULL
204 && list[cnt]->aio_lio_opcode != LIO_NOP)
205 {
679d83ba 206#ifndef DONT_NEED_AIO_MISC_COND
9f964ae3 207 waitlist->list[cnt].cond = NULL;
679d83ba 208#endif
9759bbf1 209 waitlist->list[cnt].result = NULL;
9f964ae3
UD
210 waitlist->list[cnt].next = requests[cnt]->waiting;
211 waitlist->list[cnt].counterp = &waitlist->counter;
212 waitlist->list[cnt].sigevp = &waitlist->sigev;
b61c8aba 213#ifdef BROKEN_THREAD_SIGNALS
9f964ae3 214 waitlist->list[cnt].caller_pid = caller_pid;
b61c8aba 215#endif
9f964ae3
UD
216 requests[cnt]->waiting = &waitlist->list[cnt];
217 ++total;
218 }
219 }
cbdee279 220
d71b808a
UD
221 waitlist->counter = total;
222 waitlist->sigev = *sig;
223 }
224 }
cbdee279 225
d71b808a
UD
226 /* Release the mutex. */
227 pthread_mutex_unlock (&__aio_requests_mutex);
cbdee279
UD
228
229 return result;
230}
6d3aff23
UD
231
232
233#if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
234int
235attribute_compat_text_section
236__lio_listio_21 (int mode, struct aiocb *const list[], int nent,
237 struct sigevent *sig)
238{
239 /* Check arguments. */
240 if (mode != LIO_WAIT && mode != LIO_NOWAIT)
241 {
242 __set_errno (EINVAL);
243 return -1;
244 }
245
246 return lio_listio_internal (mode | LIO_NO_INDIVIDUAL_EVENT, list, nent, sig);
247}
248compat_symbol (librt, __lio_listio_21, lio_listio, GLIBC_2_1);
249#endif
250
251
252int
253__lio_listio_item_notify (int mode, struct aiocb *const list[], int nent,
254 struct sigevent *sig)
255{
256 /* Check arguments. */
257 if (mode != LIO_WAIT && mode != LIO_NOWAIT)
258 {
259 __set_errno (EINVAL);
260 return -1;
261 }
262
263 return lio_listio_internal (mode, list, nent, sig);
264}
265versioned_symbol (librt, __lio_listio_item_notify, lio_listio, GLIBC_2_4);