aio_notify.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Notify initiator of AIO request.
  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. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <aio_misc.h>
  21. #include <signal.h>
  22. #ifndef aio_start_notify_thread
  23. # define aio_start_notify_thread() do { } while (0)
  24. #endif
  25. struct notify_func
  26. {
  27. void (*func) (sigval_t);
  28. sigval_t value;
  29. };
  30. static void *
  31. notify_func_wrapper (void *arg)
  32. {
  33. aio_start_notify_thread ();
  34. struct notify_func *const n = arg;
  35. void (*func) (sigval_t) = n->func;
  36. sigval_t value = n->value;
  37. free (n);
  38. (*func) (value);
  39. return NULL;
  40. }
  41. int
  42. __aio_notify_only (struct sigevent *sigev)
  43. {
  44. int result = 0;
  45. /* Send the signal to notify about finished processing of the request. */
  46. if (__glibc_unlikely (sigev->sigev_notify == SIGEV_THREAD))
  47. {
  48. /* We have to start a thread. */
  49. pthread_t tid;
  50. pthread_attr_t attr, *pattr;
  51. pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
  52. if (pattr == NULL)
  53. {
  54. pthread_attr_init (&attr);
  55. pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  56. pattr = &attr;
  57. }
  58. /* SIGEV may be freed as soon as we return, so we cannot let the
  59. notification thread use that pointer. Even though a sigval_t is
  60. only one word and the same size as a void *, we cannot just pass
  61. the value through pthread_create as the argument and have the new
  62. thread run the user's function directly, because on some machines
  63. the calling convention for a union like sigval_t is different from
  64. that for a pointer type like void *. */
  65. struct notify_func *nf = malloc (sizeof *nf);
  66. if (nf == NULL)
  67. result = -1;
  68. else
  69. {
  70. nf->func = sigev->sigev_notify_function;
  71. nf->value = sigev->sigev_value;
  72. if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
  73. {
  74. free (nf);
  75. result = -1;
  76. }
  77. }
  78. }
  79. else if (sigev->sigev_notify == SIGEV_SIGNAL)
  80. {
  81. /* We have to send a signal. */
  82. #if _POSIX_REALTIME_SIGNALS > 0
  83. /* Note that the standard gives us the option of using a plain
  84. non-queuing signal here when SA_SIGINFO is not set for the signal. */
  85. if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, getpid ())
  86. < 0)
  87. result = -1;
  88. #else
  89. /* There are no queued signals on this system at all. */
  90. result = raise (sigev->sigev_signo);
  91. #endif
  92. }
  93. return result;
  94. }
  95. void
  96. __aio_notify (struct requestlist *req)
  97. {
  98. struct waitlist *waitlist;
  99. struct aiocb *aiocbp = &req->aiocbp->aiocb;
  100. if (__aio_notify_only (&aiocbp->aio_sigevent) != 0)
  101. {
  102. /* XXX What shall we do if already an error is set by
  103. read/write/fsync? */
  104. aiocbp->__error_code = errno;
  105. aiocbp->__return_value = -1;
  106. }
  107. /* Now also notify possibly waiting threads. */
  108. waitlist = req->waiting;
  109. while (waitlist != NULL)
  110. {
  111. struct waitlist *next = waitlist->next;
  112. if (waitlist->sigevp == NULL)
  113. {
  114. if (waitlist->result != NULL && aiocbp->__return_value == -1)
  115. *waitlist->result = -1;
  116. #ifdef DONT_NEED_AIO_MISC_COND
  117. AIO_MISC_NOTIFY (waitlist);
  118. #else
  119. /* Decrement the counter. */
  120. --*waitlist->counterp;
  121. pthread_cond_signal (waitlist->cond);
  122. #endif
  123. }
  124. else
  125. /* This is part of an asynchronous `lio_listio' operation. If
  126. this request is the last one, send the signal. */
  127. if (--*waitlist->counterp == 0)
  128. {
  129. __aio_notify_only (waitlist->sigevp);
  130. /* This is tricky. See lio_listio.c for the reason why
  131. this works. */
  132. free ((void *) waitlist->counterp);
  133. }
  134. waitlist = next;
  135. }
  136. }