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