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