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.
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, see
18 <https://www.gnu.org/licenses/>. */
20 /* The following macros must be defined before including this file:
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.
28 For __WORDSIZE == 64, LIO_LISTIO must always be lio_listio, and
29 lio_listio64 is automatically defined as well. */
31 #include <bits/wordsize.h>
33 # define lio_listio64 XXX
35 /* And undo the hack. */
49 #include <shlib-compat.h>
52 /* We need this special structure to handle asynchronous I/O. */
56 struct sigevent sigev
;
57 struct waitlist list
[0];
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)
69 # define LIO_MODE(mode) mode
70 # define NO_INDIVIDUAL_EVENT_P(mode) 0
75 lio_listio_internal (int mode
, struct AIOCB
*const list
[], int nent
,
78 struct sigevent defsigev
;
79 struct requestlist
*requests
[nent
];
81 volatile unsigned int total
= 0;
86 defsigev
.sigev_notify
= SIGEV_NONE
;
90 /* Request the mutex. */
91 __pthread_mutex_lock (&__aio_requests_mutex
);
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
)
98 if (NO_INDIVIDUAL_EVENT_P (mode
))
99 list
[cnt
]->aio_sigevent
.sigev_notify
= SIGEV_NONE
;
101 requests
[cnt
] = __aio_enqueue_request ((aiocb_union
*) list
[cnt
],
102 (list
[cnt
]->aio_lio_opcode
105 if (requests
[cnt
] != NULL
)
106 /* Successfully enqueued. */
109 /* Signal that we've seen an error. `errno' and the error code
110 of the aiocb will tell more. */
114 requests
[cnt
] = NULL
;
118 /* We don't have anything to do except signalling if we work
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
124 __pthread_mutex_unlock (&__aio_requests_mutex
);
126 if (LIO_MODE (mode
) == LIO_NOWAIT
)
127 __aio_notify_only (sig
);
131 else if (LIO_MODE (mode
) == LIO_WAIT
)
133 #ifndef DONT_NEED_AIO_MISC_COND
134 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
137 struct waitlist waitlist
[nent
];
140 for (cnt
= 0; cnt
< nent
; ++cnt
)
142 assert (requests
[cnt
] == NULL
|| list
[cnt
] != NULL
);
144 if (requests
[cnt
] != NULL
&& list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
146 #ifndef DONT_NEED_AIO_MISC_COND
147 waitlist
[cnt
].cond
= &cond
;
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
];
158 #ifdef DONT_NEED_AIO_MISC_COND
159 AIO_MISC_WAIT (result
, total
, NULL
, 0);
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
);
167 pthread_cond_wait (&cond
, &__aio_requests_mutex
);
169 /* Now it's time to restore the cancellation state. */
170 pthread_setcancelstate (oldstate
, NULL
);
172 /* Release the conditional variable. */
173 if (pthread_cond_destroy (&cond
) != 0)
174 /* This must never happen. */
178 /* If any of the I/O requests failed, return -1 and set errno. */
181 __set_errno (result
== EINTR
? EINTR
: EIO
);
187 struct async_waitlist
*waitlist
;
189 waitlist
= (struct async_waitlist
*)
190 malloc (sizeof (struct async_waitlist
)
191 + (nent
* sizeof (struct waitlist
)));
193 if (waitlist
== NULL
)
195 __set_errno (EAGAIN
);
202 for (cnt
= 0; cnt
< nent
; ++cnt
)
204 assert (requests
[cnt
] == NULL
|| list
[cnt
] != NULL
);
206 if (requests
[cnt
] != NULL
207 && list
[cnt
]->aio_lio_opcode
!= LIO_NOP
)
209 #ifndef DONT_NEED_AIO_MISC_COND
210 waitlist
->list
[cnt
].cond
= NULL
;
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
];
221 waitlist
->counter
= total
;
222 waitlist
->sigev
= *sig
;
226 /* Release the mutex. */
227 __pthread_mutex_unlock (&__aio_requests_mutex
);
233 #if OTHER_SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
235 attribute_compat_text_section
236 LIO_LISTIO_OLD (int mode
, struct AIOCB
*const list
[], int nent
,
237 struct sigevent
*sig
)
239 /* Check arguments. */
240 if (mode
!= LIO_WAIT
&& mode
!= LIO_NOWAIT
)
242 __set_errno (EINVAL
);
246 return lio_listio_internal (mode
| LIO_NO_INDIVIDUAL_EVENT
, list
, nent
, sig
);
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
);
252 #endif /* OTHER_SHLIB_COMPAT */
256 LIO_LISTIO_NEW (int mode
, struct AIOCB
*const list
[], int nent
,
257 struct sigevent
*sig
)
259 /* Check arguments. */
260 if (mode
!= LIO_WAIT
&& mode
!= LIO_NOWAIT
)
262 __set_errno (EINVAL
);
266 return lio_listio_internal (mode
, list
, nent
, sig
);
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
);
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
);
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
);
285 #endif /* !PTHREAD_IN_LIBC */