Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / pthread / aio_notify.c
blob302f1a785bcbe53b2076949e1759cdc8f81d869d
1 /* Notify initiator of AIO request.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
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.
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.
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/>. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <aio_misc.h>
26 #ifndef aio_start_notify_thread
27 # define aio_start_notify_thread() do { } while (0)
28 #endif
30 struct notify_func
32 void (*func) (sigval_t);
33 sigval_t value;
36 static void *
37 notify_func_wrapper (void *arg)
39 aio_start_notify_thread ();
40 struct notify_func *const n = arg;
41 void (*func) (sigval_t) = n->func;
42 sigval_t value = n->value;
43 free (n);
44 (*func) (value);
45 return NULL;
49 int
50 internal_function
51 #ifdef BROKEN_THREAD_SIGNALS
52 __aio_notify_only (struct sigevent *sigev, pid_t caller_pid)
53 #else
54 __aio_notify_only (struct sigevent *sigev)
55 #endif
57 int result = 0;
59 /* Send the signal to notify about finished processing of the request. */
60 if (__builtin_expect (sigev->sigev_notify == SIGEV_THREAD, 0))
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 # ifdef BROKEN_THREAD_SIGNALS
102 if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid)
103 < 0)
104 result = -1;
105 # else
106 if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, getpid ())
107 < 0)
108 result = -1;
109 # endif
110 #else
111 /* There are no queued signals on this system at all. */
112 result = raise (sigev->sigev_signo);
113 #endif
116 return result;
120 void
121 internal_function
122 __aio_notify (struct requestlist *req)
124 struct waitlist *waitlist;
125 struct aiocb *aiocbp = &req->aiocbp->aiocb;
127 #ifdef BROKEN_THREAD_SIGNALS
128 if (__aio_notify_only (&aiocbp->aio_sigevent, req->caller_pid) != 0)
129 #else
130 if (__aio_notify_only (&aiocbp->aio_sigevent) != 0)
131 #endif
133 /* XXX What shall we do if already an error is set by
134 read/write/fsync? */
135 aiocbp->__error_code = errno;
136 aiocbp->__return_value = -1;
139 /* Now also notify possibly waiting threads. */
140 waitlist = req->waiting;
141 while (waitlist != NULL)
143 struct waitlist *next = waitlist->next;
145 if (waitlist->sigevp == NULL)
147 if (waitlist->result != NULL && aiocbp->__return_value == -1)
148 *waitlist->result = -1;
150 #ifdef DONT_NEED_AIO_MISC_COND
151 AIO_MISC_NOTIFY (waitlist);
152 #else
153 /* Decrement the counter. */
154 --*waitlist->counterp;
156 pthread_cond_signal (waitlist->cond);
157 #endif
159 else
160 /* This is part of an asynchronous `lio_listio' operation. If
161 this request is the last one, send the signal. */
162 if (--*waitlist->counterp == 0)
164 #ifdef BROKEN_THREAD_SIGNALS
165 __aio_notify_only (waitlist->sigevp, waitlist->caller_pid);
166 #else
167 __aio_notify_only (waitlist->sigevp);
168 #endif
169 /* This is tricky. See lio_listio.c for the reason why
170 this works. */
171 free ((void *) waitlist->counterp);
174 waitlist = next;