lio_listio-common.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* Enqueue and list of read or write requests. Common code template.
  2. Copyright (C) 1997-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* The following macros must be defined before including this file:
  16. LIO_LISTIO The public symbol (lio_listio or lio_listio64).
  17. AIOCB Struct tag used by LIO_LISTIO (aiocb or aiocb64).
  18. LIO_LISTIO_OLD The internal symbol for the compat implementation.
  19. LIO_LISTIO_NEW The internal symbol for the current implementation.
  20. LIO_OPCODE_BASE Opcode shift for 64-bit version with 32-bit word size.
  21. For __WORDSIZE == 64, LIO_LISTIO must always be lio_listio, and
  22. lio_listio64 is automatically defined as well. */
  23. #include <bits/wordsize.h>
  24. #if __WORDSIZE == 64
  25. # define lio_listio64 XXX
  26. # include <aio.h>
  27. /* And undo the hack. */
  28. # undef lio_listio64
  29. #else
  30. # include <aio.h>
  31. #endif
  32. #include <assert.h>
  33. #include <errno.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <pthreadP.h>
  37. #include <aio_misc.h>
  38. #include <shlib-compat.h>
  39. /* We need this special structure to handle asynchronous I/O. */
  40. struct async_waitlist
  41. {
  42. unsigned int counter;
  43. struct sigevent sigev;
  44. struct waitlist list[0];
  45. };
  46. /* The code in glibc 2.1 to glibc 2.4 issued only one event when all
  47. requests submitted with lio_listio finished. The existing practice
  48. is to issue events for the individual requests as well. This is
  49. what the new code does. */
  50. #if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
  51. # define LIO_MODE(mode) ((mode) & 127)
  52. # define NO_INDIVIDUAL_EVENT_P(mode) ((mode) & 128)
  53. #else
  54. # define LIO_MODE(mode) mode
  55. # define NO_INDIVIDUAL_EVENT_P(mode) 0
  56. #endif
  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. if (sig == NULL)
  67. {
  68. defsigev.sigev_notify = SIGEV_NONE;
  69. sig = &defsigev;
  70. }
  71. /* Request the mutex. */
  72. __pthread_mutex_lock (&__aio_requests_mutex);
  73. /* Now we can enqueue all requests. Since we already acquired the
  74. mutex the enqueue function need not do this. */
  75. for (cnt = 0; cnt < nent; ++cnt)
  76. if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
  77. {
  78. if (NO_INDIVIDUAL_EVENT_P (mode))
  79. list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
  80. requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
  81. (list[cnt]->aio_lio_opcode
  82. | LIO_OPCODE_BASE));
  83. if (requests[cnt] != NULL)
  84. /* Successfully enqueued. */
  85. ++total;
  86. else
  87. /* Signal that we've seen an error. `errno' and the error code
  88. of the aiocb will tell more. */
  89. result = -1;
  90. }
  91. else
  92. requests[cnt] = NULL;
  93. if (total == 0)
  94. {
  95. /* We don't have anything to do except signalling if we work
  96. 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);
  101. if (LIO_MODE (mode) == LIO_NOWAIT)
  102. __aio_notify_only (sig);
  103. return result;
  104. }
  105. else if (LIO_MODE (mode) == LIO_WAIT)
  106. {
  107. #ifndef DONT_NEED_AIO_MISC_COND
  108. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  109. int oldstate;
  110. #endif
  111. struct waitlist waitlist[nent];
  112. total = 0;
  113. for (cnt = 0; cnt < nent; ++cnt)
  114. {
  115. assert (requests[cnt] == NULL || list[cnt] != NULL);
  116. if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
  117. {
  118. #ifndef DONT_NEED_AIO_MISC_COND
  119. waitlist[cnt].cond = &cond;
  120. #endif
  121. waitlist[cnt].result = &result;
  122. waitlist[cnt].next = requests[cnt]->waiting;
  123. waitlist[cnt].counterp = &total;
  124. waitlist[cnt].sigevp = NULL;
  125. requests[cnt]->waiting = &waitlist[cnt];
  126. ++total;
  127. }
  128. }
  129. #ifdef DONT_NEED_AIO_MISC_COND
  130. AIO_MISC_WAIT (result, total, NULL, 0);
  131. #else
  132. /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancellation
  133. points we must be careful. We added entries to the waiting lists
  134. which we must remove. So defer cancellation for now. */
  135. pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
  136. while (total > 0)
  137. pthread_cond_wait (&cond, &__aio_requests_mutex);
  138. /* Now it's time to restore the cancellation state. */
  139. pthread_setcancelstate (oldstate, NULL);
  140. /* Release the conditional variable. */
  141. if (pthread_cond_destroy (&cond) != 0)
  142. /* This must never happen. */
  143. abort ();
  144. #endif
  145. /* If any of the I/O requests failed, return -1 and set errno. */
  146. if (result != 0)
  147. {
  148. __set_errno (result == EINTR ? EINTR : EIO);
  149. result = -1;
  150. }
  151. }
  152. else
  153. {
  154. struct async_waitlist *waitlist;
  155. waitlist = (struct async_waitlist *)
  156. malloc (sizeof (struct async_waitlist)
  157. + (nent * sizeof (struct waitlist)));
  158. if (waitlist == NULL)
  159. {
  160. __set_errno (EAGAIN);
  161. result = -1;
  162. }
  163. else
  164. {
  165. total = 0;
  166. for (cnt = 0; cnt < nent; ++cnt)
  167. {
  168. assert (requests[cnt] == NULL || list[cnt] != NULL);
  169. if (requests[cnt] != NULL
  170. && list[cnt]->aio_lio_opcode != LIO_NOP)
  171. {
  172. #ifndef DONT_NEED_AIO_MISC_COND
  173. waitlist->list[cnt].cond = NULL;
  174. #endif
  175. waitlist->list[cnt].result = NULL;
  176. waitlist->list[cnt].next = requests[cnt]->waiting;
  177. waitlist->list[cnt].counterp = &waitlist->counter;
  178. waitlist->list[cnt].sigevp = &waitlist->sigev;
  179. requests[cnt]->waiting = &waitlist->list[cnt];
  180. ++total;
  181. }
  182. }
  183. waitlist->counter = total;
  184. waitlist->sigev = *sig;
  185. }
  186. }
  187. /* Release the mutex. */
  188. __pthread_mutex_unlock (&__aio_requests_mutex);
  189. return result;
  190. }
  191. #if OTHER_SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
  192. int
  193. attribute_compat_text_section
  194. LIO_LISTIO_OLD (int mode, struct AIOCB *const list[], int nent,
  195. struct sigevent *sig)
  196. {
  197. /* Check arguments. */
  198. if (mode != LIO_WAIT && mode != LIO_NOWAIT)
  199. {
  200. __set_errno (EINVAL);
  201. return -1;
  202. }
  203. return lio_listio_internal (mode | LIO_NO_INDIVIDUAL_EVENT, list, nent, sig);
  204. }
  205. compat_symbol (librt, LIO_LISTIO_OLD, LIO_LISTIO, GLIBC_2_1);
  206. # if __WORDSIZE == 64
  207. compat_symbol (librt, LIO_LISTIO_OLD, lio_listio64, GLIBC_2_1);
  208. # endif
  209. #endif /* OTHER_SHLIB_COMPAT */
  210. int
  211. LIO_LISTIO_NEW (int mode, struct AIOCB *const list[], int nent,
  212. struct sigevent *sig)
  213. {
  214. /* Check arguments. */
  215. if (mode != LIO_WAIT && mode != LIO_NOWAIT)
  216. {
  217. __set_errno (EINVAL);
  218. return -1;
  219. }
  220. return lio_listio_internal (mode, list, nent, sig);
  221. }
  222. #if PTHREAD_IN_LIBC
  223. versioned_symbol (libc, LIO_LISTIO_NEW, LIO_LISTIO, GLIBC_2_34);
  224. # if __WORDSIZE == 64
  225. versioned_symbol (libc, LIO_LISTIO_NEW, lio_listio64, GLIBC_2_34);
  226. # endif
  227. # if OTHER_SHLIB_COMPAT (librt, GLIBC_2_4, GLIBC_2_34)
  228. compat_symbol (librt, LIO_LISTIO_NEW, LIO_LISTIO, GLIBC_2_4);
  229. # if __WORDSIZE == 64
  230. compat_symbol (librt, LIO_LISTIO_NEW, lio_listio64, GLIBC_2_4);
  231. # endif
  232. # endif /* OTHER_SHLIB_COMPAT */
  233. #else /* !PTHREAD_IN_LIBC */
  234. versioned_symbol (librt, LIO_LISTIO_NEW, LIO_LISTIO, GLIBC_2_4);
  235. # if __WORDSIZE == 64
  236. versioned_symbol (librt, LIO_LISTIO_NEW, lio_listio64, GLIBC_2_4);
  237. # endif
  238. #endif /* !PTHREAD_IN_LIBC */