Linux: Assume and consolidate bind wire-up syscall
[glibc.git] / rt / aio_notify.c
bloba5c150c06fe8a851ebf0b3efe087cd8d6b254504
1 /* Notify initiator of AIO request.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <pthreadP.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <aio_misc.h>
24 #include <signal.h>
26 #if !PTHREAD_IN_LIBC
27 # define __pthread_attr_init pthread_attr_init
28 # define __pthread_attr_setdetachstate pthread_attr_setdetachstate
29 #endif
31 #ifndef aio_start_notify_thread
32 # define aio_start_notify_thread() do { } while (0)
33 #endif
35 struct notify_func
37 void (*func) (sigval_t);
38 sigval_t value;
41 static void *
42 notify_func_wrapper (void *arg)
44 aio_start_notify_thread ();
45 struct notify_func *const n = arg;
46 void (*func) (sigval_t) = n->func;
47 sigval_t value = n->value;
48 free (n);
49 (*func) (value);
50 return NULL;
54 int
55 __aio_notify_only (struct sigevent *sigev)
57 int result = 0;
59 /* Send the signal to notify about finished processing of the request. */
60 if (__glibc_unlikely (sigev->sigev_notify == SIGEV_THREAD))
62 /* We have to start a thread. */
63 pthread_t tid;
64 pthread_attr_t attr, *pattr;
66 pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
67 if (pattr == NULL)
69 __pthread_attr_init (&attr);
70 __pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
71 pattr = &attr;
74 /* SIGEV may be freed as soon as we return, so we cannot let the
75 notification thread use that pointer. Even though a sigval_t is
76 only one word and the same size as a void *, we cannot just pass
77 the value through pthread_create as the argument and have the new
78 thread run the user's function directly, because on some machines
79 the calling convention for a union like sigval_t is different from
80 that for a pointer type like void *. */
81 struct notify_func *nf = malloc (sizeof *nf);
82 if (nf == NULL)
83 result = -1;
84 else
86 nf->func = sigev->sigev_notify_function;
87 nf->value = sigev->sigev_value;
88 if (__pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
90 free (nf);
91 result = -1;
95 else if (sigev->sigev_notify == SIGEV_SIGNAL)
97 /* We have to send a signal. */
98 #if _POSIX_REALTIME_SIGNALS > 0
99 /* Note that the standard gives us the option of using a plain
100 non-queuing signal here when SA_SIGINFO is not set for the signal. */
101 if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, getpid ())
102 < 0)
103 result = -1;
104 #else
105 /* There are no queued signals on this system at all. */
106 result = raise (sigev->sigev_signo);
107 #endif
110 return result;
114 void
115 __aio_notify (struct requestlist *req)
117 struct waitlist *waitlist;
118 struct aiocb *aiocbp = &req->aiocbp->aiocb;
120 if (__aio_notify_only (&aiocbp->aio_sigevent) != 0)
122 /* XXX What shall we do if already an error is set by
123 read/write/fsync? */
124 aiocbp->__error_code = errno;
125 aiocbp->__return_value = -1;
128 /* Now also notify possibly waiting threads. */
129 waitlist = req->waiting;
130 while (waitlist != NULL)
132 struct waitlist *next = waitlist->next;
134 if (waitlist->sigevp == NULL)
136 if (waitlist->result != NULL && aiocbp->__return_value == -1)
137 *waitlist->result = -1;
139 #ifdef DONT_NEED_AIO_MISC_COND
140 AIO_MISC_NOTIFY (waitlist);
141 #else
142 /* Decrement the counter. */
143 --*waitlist->counterp;
145 pthread_cond_signal (waitlist->cond);
146 #endif
148 else
149 /* This is part of an asynchronous `lio_listio' operation. If
150 this request is the last one, send the signal. */
151 if (--*waitlist->counterp == 0)
153 __aio_notify_only (waitlist->sigevp);
154 /* This is tricky. See lio_listio.c for the reason why
155 this works. */
156 free ((void *) waitlist->counterp);
159 waitlist = next;