aio_suspend.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Suspend until termination of a 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. /* We use an UGLY hack to prevent gcc from finding us cheating. The
  17. implementations of aio_suspend and aio_suspend64 are identical and so
  18. we want to avoid code duplication by using aliases. But gcc sees
  19. the different parameter lists and prints a warning. We define here
  20. a function so that aio_suspend64 has no prototype. */
  21. #define aio_suspend64 XXX
  22. #include <aio.h>
  23. /* And undo the hack. */
  24. #undef aio_suspend64
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <stdbool.h>
  28. #include <stdlib.h>
  29. #include <sys/time.h>
  30. #include <libc-lock.h>
  31. #include <aio_misc.h>
  32. struct clparam
  33. {
  34. const struct aiocb *const *list;
  35. struct waitlist *waitlist;
  36. struct requestlist **requestlist;
  37. #ifndef DONT_NEED_AIO_MISC_COND
  38. pthread_cond_t *cond;
  39. #endif
  40. int nent;
  41. };
  42. static void
  43. cleanup (void *arg)
  44. {
  45. #ifdef DONT_NEED_AIO_MISC_COND
  46. /* Acquire the mutex. If pthread_cond_*wait is used this would
  47. happen implicitly. */
  48. pthread_mutex_lock (&__aio_requests_mutex);
  49. #endif
  50. const struct clparam *param = (const struct clparam *) arg;
  51. /* Now remove the entry in the waiting list for all requests
  52. which didn't terminate. */
  53. int cnt = param->nent;
  54. while (cnt-- > 0)
  55. if (param->list[cnt] != NULL
  56. && param->list[cnt]->__error_code == EINPROGRESS)
  57. {
  58. struct waitlist **listp;
  59. assert (param->requestlist[cnt] != NULL);
  60. /* There is the chance that we cannot find our entry anymore. This
  61. could happen if the request terminated and restarted again. */
  62. listp = &param->requestlist[cnt]->waiting;
  63. while (*listp != NULL && *listp != &param->waitlist[cnt])
  64. listp = &(*listp)->next;
  65. if (*listp != NULL)
  66. *listp = (*listp)->next;
  67. }
  68. #ifndef DONT_NEED_AIO_MISC_COND
  69. /* Release the conditional variable. */
  70. (void) pthread_cond_destroy (param->cond);
  71. #endif
  72. /* Release the mutex. */
  73. pthread_mutex_unlock (&__aio_requests_mutex);
  74. }
  75. #ifdef DONT_NEED_AIO_MISC_COND
  76. static int
  77. __attribute__ ((noinline))
  78. do_aio_misc_wait (unsigned int *cntr, const struct timespec *timeout)
  79. {
  80. int result = 0;
  81. AIO_MISC_WAIT (result, *cntr, timeout, 1);
  82. return result;
  83. }
  84. #endif
  85. int
  86. aio_suspend (const struct aiocb *const list[], int nent,
  87. const struct timespec *timeout)
  88. {
  89. if (__glibc_unlikely (nent < 0))
  90. {
  91. __set_errno (EINVAL);
  92. return -1;
  93. }
  94. struct waitlist waitlist[nent];
  95. struct requestlist *requestlist[nent];
  96. #ifndef DONT_NEED_AIO_MISC_COND
  97. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  98. #endif
  99. int cnt;
  100. bool any = false;
  101. int result = 0;
  102. unsigned int cntr = 1;
  103. /* Request the mutex. */
  104. pthread_mutex_lock (&__aio_requests_mutex);
  105. /* There is not yet a finished request. Signal the request that
  106. we are working for it. */
  107. for (cnt = 0; cnt < nent; ++cnt)
  108. if (list[cnt] != NULL)
  109. {
  110. if (list[cnt]->__error_code == EINPROGRESS)
  111. {
  112. requestlist[cnt] = __aio_find_req ((aiocb_union *) list[cnt]);
  113. if (requestlist[cnt] != NULL)
  114. {
  115. #ifndef DONT_NEED_AIO_MISC_COND
  116. waitlist[cnt].cond = &cond;
  117. #endif
  118. waitlist[cnt].result = NULL;
  119. waitlist[cnt].next = requestlist[cnt]->waiting;
  120. waitlist[cnt].counterp = &cntr;
  121. waitlist[cnt].sigevp = NULL;
  122. requestlist[cnt]->waiting = &waitlist[cnt];
  123. any = true;
  124. }
  125. else
  126. /* We will never suspend. */
  127. break;
  128. }
  129. else
  130. /* We will never suspend. */
  131. break;
  132. }
  133. /* Only if none of the entries is NULL or finished to be wait. */
  134. if (cnt == nent && any)
  135. {
  136. struct clparam clparam =
  137. {
  138. .list = list,
  139. .waitlist = waitlist,
  140. .requestlist = requestlist,
  141. #ifndef DONT_NEED_AIO_MISC_COND
  142. .cond = &cond,
  143. #endif
  144. .nent = nent
  145. };
  146. pthread_cleanup_push (cleanup, &clparam);
  147. #ifdef DONT_NEED_AIO_MISC_COND
  148. result = do_aio_misc_wait (&cntr, timeout);
  149. #else
  150. if (timeout == NULL)
  151. result = pthread_cond_wait (&cond, &__aio_requests_mutex);
  152. else
  153. {
  154. /* We have to convert the relative timeout value into an
  155. absolute time value with pthread_cond_timedwait expects. */
  156. struct timeval now;
  157. struct timespec abstime;
  158. __gettimeofday (&now, NULL);
  159. abstime.tv_nsec = timeout->tv_nsec + now.tv_usec * 1000;
  160. abstime.tv_sec = timeout->tv_sec + now.tv_sec;
  161. if (abstime.tv_nsec >= 1000000000)
  162. {
  163. abstime.tv_nsec -= 1000000000;
  164. abstime.tv_sec += 1;
  165. }
  166. result = pthread_cond_timedwait (&cond, &__aio_requests_mutex,
  167. &abstime);
  168. }
  169. #endif
  170. pthread_cleanup_pop (0);
  171. }
  172. /* Now remove the entry in the waiting list for all requests
  173. which didn't terminate. */
  174. while (cnt-- > 0)
  175. if (list[cnt] != NULL && list[cnt]->__error_code == EINPROGRESS)
  176. {
  177. struct waitlist **listp;
  178. assert (requestlist[cnt] != NULL);
  179. /* There is the chance that we cannot find our entry anymore. This
  180. could happen if the request terminated and restarted again. */
  181. listp = &requestlist[cnt]->waiting;
  182. while (*listp != NULL && *listp != &waitlist[cnt])
  183. listp = &(*listp)->next;
  184. if (*listp != NULL)
  185. *listp = (*listp)->next;
  186. }
  187. #ifndef DONT_NEED_AIO_MISC_COND
  188. /* Release the conditional variable. */
  189. if (__glibc_unlikely (pthread_cond_destroy (&cond) != 0))
  190. /* This must never happen. */
  191. abort ();
  192. #endif
  193. if (result != 0)
  194. {
  195. #ifndef DONT_NEED_AIO_MISC_COND
  196. /* An error occurred. Possibly it's ETIMEDOUT. We have to translate
  197. the timeout error report of `pthread_cond_timedwait' to the
  198. form expected from `aio_suspend'. */
  199. if (result == ETIMEDOUT)
  200. __set_errno (EAGAIN);
  201. else
  202. #endif
  203. __set_errno (result);
  204. result = -1;
  205. }
  206. /* Release the mutex. */
  207. pthread_mutex_unlock (&__aio_requests_mutex);
  208. return result;
  209. }
  210. weak_alias (aio_suspend, aio_suspend64)