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/>. */
27 # define __pthread_attr_init pthread_attr_init
28 # define __pthread_attr_setdetachstate pthread_attr_setdetachstate
31 #ifndef aio_start_notify_thread
32 # define aio_start_notify_thread() do { } while (0)
37 void (*func
) (sigval_t
);
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
;
55 __aio_notify_only (struct sigevent
*sigev
)
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. */
64 pthread_attr_t attr
, *pattr
;
66 pattr
= (pthread_attr_t
*) sigev
->sigev_notify_attributes
;
69 __pthread_attr_init (&attr
);
70 __pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
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
);
86 nf
->func
= sigev
->sigev_notify_function
;
87 nf
->value
= sigev
->sigev_value
;
88 if (__pthread_create (&tid
, pattr
, notify_func_wrapper
, nf
) < 0)
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 ())
105 /* There are no queued signals on this system at all. */
106 result
= raise (sigev
->sigev_signo
);
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
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
);
142 /* Decrement the counter. */
143 --*waitlist
->counterp
;
145 pthread_cond_signal (waitlist
->cond
);
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
156 free ((void *) waitlist
->counterp
);