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