lio_listio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Enqueue and list of read or write requests.
  2. Copyright (C) 1997-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef lio_listio
  17. #include <aio.h>
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <aio_misc.h>
  23. #define LIO_OPCODE_BASE 0
  24. #endif
  25. #include <shlib-compat.h>
  26. /* We need this special structure to handle asynchronous I/O. */
  27. struct async_waitlist
  28. {
  29. unsigned int counter;
  30. struct sigevent sigev;
  31. struct waitlist list[0];
  32. };
  33. /* The code in glibc 2.1 to glibc 2.4 issued only one event when all
  34. requests submitted with lio_listio finished. The existing practice
  35. is to issue events for the individual requests as well. This is
  36. what the new code does. */
  37. #if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
  38. # define LIO_MODE(mode) ((mode) & 127)
  39. # define NO_INDIVIDUAL_EVENT_P(mode) ((mode) & 128)
  40. #else
  41. # define LIO_MODE(mode) mode
  42. # define NO_INDIVIDUAL_EVENT_P(mode) 0
  43. #endif
  44. static int
  45. lio_listio_internal (int mode, struct aiocb *const list[], int nent,
  46. struct sigevent *sig)
  47. {
  48. struct sigevent defsigev;
  49. struct requestlist *requests[nent];
  50. int cnt;
  51. volatile unsigned int total = 0;
  52. int result = 0;
  53. if (sig == NULL)
  54. {
  55. defsigev.sigev_notify = SIGEV_NONE;
  56. sig = &defsigev;
  57. }
  58. /* Request the mutex. */
  59. pthread_mutex_lock (&__aio_requests_mutex);
  60. /* Now we can enqueue all requests. Since we already acquired the
  61. mutex the enqueue function need not do this. */
  62. for (cnt = 0; cnt < nent; ++cnt)
  63. if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
  64. {
  65. if (NO_INDIVIDUAL_EVENT_P (mode))
  66. list[cnt]->aio_sigevent.sigev_notify = SIGEV_NONE;
  67. requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
  68. (list[cnt]->aio_lio_opcode
  69. | LIO_OPCODE_BASE));
  70. if (requests[cnt] != NULL)
  71. /* Successfully enqueued. */
  72. ++total;
  73. else
  74. /* Signal that we've seen an error. `errno' and the error code
  75. of the aiocb will tell more. */
  76. result = -1;
  77. }
  78. else
  79. requests[cnt] = NULL;
  80. if (total == 0)
  81. {
  82. /* We don't have anything to do except signalling if we work
  83. asynchronously. */
  84. /* Release the mutex. We do this before raising a signal since the
  85. signal handler might do a `siglongjmp' and then the mutex is
  86. locked forever. */
  87. pthread_mutex_unlock (&__aio_requests_mutex);
  88. if (LIO_MODE (mode) == LIO_NOWAIT)
  89. __aio_notify_only (sig);
  90. return result;
  91. }
  92. else if (LIO_MODE (mode) == LIO_WAIT)
  93. {
  94. #ifndef DONT_NEED_AIO_MISC_COND
  95. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  96. int oldstate;
  97. #endif
  98. struct waitlist waitlist[nent];
  99. total = 0;
  100. for (cnt = 0; cnt < nent; ++cnt)
  101. {
  102. assert (requests[cnt] == NULL || list[cnt] != NULL);
  103. if (requests[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
  104. {
  105. #ifndef DONT_NEED_AIO_MISC_COND
  106. waitlist[cnt].cond = &cond;
  107. #endif
  108. waitlist[cnt].result = &result;
  109. waitlist[cnt].next = requests[cnt]->waiting;
  110. waitlist[cnt].counterp = &total;
  111. waitlist[cnt].sigevp = NULL;
  112. requests[cnt]->waiting = &waitlist[cnt];
  113. ++total;
  114. }
  115. }
  116. #ifdef DONT_NEED_AIO_MISC_COND
  117. AIO_MISC_WAIT (result, total, NULL, 0);
  118. #else
  119. /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancellation
  120. points we must be careful. We added entries to the waiting lists
  121. which we must remove. So defer cancellation for now. */
  122. pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
  123. while (total > 0)
  124. pthread_cond_wait (&cond, &__aio_requests_mutex);
  125. /* Now it's time to restore the cancellation state. */
  126. pthread_setcancelstate (oldstate, NULL);
  127. /* Release the conditional variable. */
  128. if (pthread_cond_destroy (&cond) != 0)
  129. /* This must never happen. */
  130. abort ();
  131. #endif
  132. /* If any of the I/O requests failed, return -1 and set errno. */
  133. if (result != 0)
  134. {
  135. __set_errno (result == EINTR ? EINTR : EIO);
  136. result = -1;
  137. }
  138. }
  139. else
  140. {
  141. struct async_waitlist *waitlist;
  142. waitlist = (struct async_waitlist *)
  143. malloc (sizeof (struct async_waitlist)
  144. + (nent * sizeof (struct waitlist)));
  145. if (waitlist == NULL)
  146. {
  147. __set_errno (EAGAIN);
  148. result = -1;
  149. }
  150. else
  151. {
  152. total = 0;
  153. for (cnt = 0; cnt < nent; ++cnt)
  154. {
  155. assert (requests[cnt] == NULL || list[cnt] != NULL);
  156. if (requests[cnt] != NULL
  157. && list[cnt]->aio_lio_opcode != LIO_NOP)
  158. {
  159. #ifndef DONT_NEED_AIO_MISC_COND
  160. waitlist->list[cnt].cond = NULL;
  161. #endif
  162. waitlist->list[cnt].result = NULL;
  163. waitlist->list[cnt].next = requests[cnt]->waiting;
  164. waitlist->list[cnt].counterp = &waitlist->counter;
  165. waitlist->list[cnt].sigevp = &waitlist->sigev;
  166. requests[cnt]->waiting = &waitlist->list[cnt];
  167. ++total;
  168. }
  169. }
  170. waitlist->counter = total;
  171. waitlist->sigev = *sig;
  172. }
  173. }
  174. /* Release the mutex. */
  175. pthread_mutex_unlock (&__aio_requests_mutex);
  176. return result;
  177. }
  178. #if SHLIB_COMPAT (librt, GLIBC_2_1, GLIBC_2_4)
  179. int
  180. attribute_compat_text_section
  181. __lio_listio_21 (int mode, struct aiocb *const list[], int nent,
  182. struct sigevent *sig)
  183. {
  184. /* Check arguments. */
  185. if (mode != LIO_WAIT && mode != LIO_NOWAIT)
  186. {
  187. __set_errno (EINVAL);
  188. return -1;
  189. }
  190. return lio_listio_internal (mode | LIO_NO_INDIVIDUAL_EVENT, list, nent, sig);
  191. }
  192. compat_symbol (librt, __lio_listio_21, lio_listio, GLIBC_2_1);
  193. #endif
  194. int
  195. __lio_listio_item_notify (int mode, struct aiocb *const list[], int nent,
  196. struct sigevent *sig)
  197. {
  198. /* Check arguments. */
  199. if (mode != LIO_WAIT && mode != LIO_NOWAIT)
  200. {
  201. __set_errno (EINVAL);
  202. return -1;
  203. }
  204. return lio_listio_internal (mode, list, nent, sig);
  205. }
  206. versioned_symbol (librt, __lio_listio_item_notify, lio_listio, GLIBC_2_4);