1 | /* Notify initiator of AIO request. |
2 | Copyright (C) 1997-2016 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 | <http://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 | |
31 | struct notify_func |
32 | { |
33 | void (*func) (sigval_t); |
34 | sigval_t value; |
35 | }; |
36 | |
37 | static void * |
38 | notify_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 | |
50 | int |
51 | internal_function |
52 | #ifdef BROKEN_THREAD_SIGNALS |
53 | __aio_notify_only (struct sigevent *sigev, pid_t caller_pid) |
54 | #else |
55 | __aio_notify_only (struct sigevent *sigev) |
56 | #endif |
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 | # ifdef BROKEN_THREAD_SIGNALS |
103 | if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid) |
104 | < 0) |
105 | result = -1; |
106 | # else |
107 | if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, getpid ()) |
108 | < 0) |
109 | result = -1; |
110 | # endif |
111 | #else |
112 | /* There are no queued signals on this system at all. */ |
113 | result = raise (sigev->sigev_signo); |
114 | #endif |
115 | } |
116 | |
117 | return result; |
118 | } |
119 | |
120 | |
121 | void |
122 | internal_function |
123 | __aio_notify (struct requestlist *req) |
124 | { |
125 | struct waitlist *waitlist; |
126 | struct aiocb *aiocbp = &req->aiocbp->aiocb; |
127 | |
128 | #ifdef BROKEN_THREAD_SIGNALS |
129 | if (__aio_notify_only (&aiocbp->aio_sigevent, req->caller_pid) != 0) |
130 | #else |
131 | if (__aio_notify_only (&aiocbp->aio_sigevent) != 0) |
132 | #endif |
133 | { |
134 | /* XXX What shall we do if already an error is set by |
135 | read/write/fsync? */ |
136 | aiocbp->__error_code = errno; |
137 | aiocbp->__return_value = -1; |
138 | } |
139 | |
140 | /* Now also notify possibly waiting threads. */ |
141 | waitlist = req->waiting; |
142 | while (waitlist != NULL) |
143 | { |
144 | struct waitlist *next = waitlist->next; |
145 | |
146 | if (waitlist->sigevp == NULL) |
147 | { |
148 | if (waitlist->result != NULL && aiocbp->__return_value == -1) |
149 | *waitlist->result = -1; |
150 | |
151 | #ifdef DONT_NEED_AIO_MISC_COND |
152 | AIO_MISC_NOTIFY (waitlist); |
153 | #else |
154 | /* Decrement the counter. */ |
155 | --*waitlist->counterp; |
156 | |
157 | pthread_cond_signal (waitlist->cond); |
158 | #endif |
159 | } |
160 | else |
161 | /* This is part of an asynchronous `lio_listio' operation. If |
162 | this request is the last one, send the signal. */ |
163 | if (--*waitlist->counterp == 0) |
164 | { |
165 | #ifdef BROKEN_THREAD_SIGNALS |
166 | __aio_notify_only (waitlist->sigevp, waitlist->caller_pid); |
167 | #else |
168 | __aio_notify_only (waitlist->sigevp); |
169 | #endif |
170 | /* This is tricky. See lio_listio.c for the reason why |
171 | this works. */ |
172 | free ((void *) waitlist->counterp); |
173 | } |
174 | |
175 | waitlist = next; |
176 | } |
177 | } |
178 | |