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