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.
4
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
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.
14
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/>. */
18
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>
25
26#if !PTHREAD_IN_LIBC
27# define __pthread_attr_init pthread_attr_init
28# define __pthread_attr_setdetachstate pthread_attr_setdetachstate
29#endif
30
31#ifndef aio_start_notify_thread
32# define aio_start_notify_thread() do { } while (0)
33#endif
34
35struct notify_func
36 {
37 void (*func) (sigval_t);
38 sigval_t value;
39 };
40
41static void *
42notify_func_wrapper (void *arg)
43{
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 (ptr: n);
49 (*func) (value);
50 return NULL;
51}
52
53
54int
55__aio_notify_only (struct sigevent *sigev)
56{
57 int result = 0;
58
59 /* Send the signal to notify about finished processing of the request. */
60 if (__glibc_unlikely (sigev->sigev_notify == SIGEV_THREAD))
61 {
62 /* We have to start a thread. */
63 pthread_t tid;
64 pthread_attr_t attr, *pattr;
65
66 pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
67 if (pattr == NULL)
68 {
69 __pthread_attr_init (&attr);
70 __pthread_attr_setdetachstate (attr: &attr, PTHREAD_CREATE_DETACHED);
71 pattr = &attr;
72 }
73
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 (size: sizeof *nf);
82 if (nf == NULL)
83 result = -1;
84 else
85 {
86 nf->func = sigev->sigev_notify_function;
87 nf->value = sigev->sigev_value;
88 if (__pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
89 {
90 free (ptr: nf);
91 result = -1;
92 }
93 }
94 }
95 else if (sigev->sigev_notify == SIGEV_SIGNAL)
96 {
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 (sig: sigev->sigev_signo, val: sigev->sigev_value, caller_pid: 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
108 }
109
110 return result;
111}
112
113
114void
115__aio_notify (struct requestlist *req)
116{
117 struct waitlist *waitlist;
118 struct aiocb *aiocbp = &req->aiocbp->aiocb;
119
120 if (__aio_notify_only (sigev: &aiocbp->aio_sigevent) != 0)
121 {
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;
126 }
127
128 /* Now also notify possibly waiting threads. */
129 waitlist = req->waiting;
130 while (waitlist != NULL)
131 {
132 struct waitlist *next = waitlist->next;
133
134 if (waitlist->sigevp == NULL)
135 {
136 if (waitlist->result != NULL && aiocbp->__return_value == -1)
137 *waitlist->result = -1;
138
139#ifdef DONT_NEED_AIO_MISC_COND
140 AIO_MISC_NOTIFY (waitlist);
141#else
142 /* Decrement the counter. */
143 --*waitlist->counterp;
144
145 pthread_cond_signal (waitlist->cond);
146#endif
147 }
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)
152 {
153 __aio_notify_only (sigev: waitlist->sigevp);
154 /* This is tricky. See lio_listio.c for the reason why
155 this works. */
156 free (ptr: (void *) waitlist->counterp);
157 }
158
159 waitlist = next;
160 }
161}
162

source code of glibc/rt/aio_notify.c