configure tweaks, support $libc_add_on_config_subdirs
[glibc/pb-stable.git] / sysdeps / pthread / aio_notify.c
blob3f7f70ef7c1cff2706b712fc06f7034e9fca2b69
1 /* Notify initiator of AIO request.
2 Copyright (C) 1997-2001, 2003, 2004, 2006 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <aio_misc.h>
27 #ifndef aio_start_notify_thread
28 # define aio_start_notify_thread() do { } while (0)
29 #endif
31 struct notify_func
33 void (*func) (sigval_t);
34 sigval_t value;
37 static void *
38 notify_func_wrapper (void *arg)
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;
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
58 int result = 0;
60 /* Send the signal to notify about finished processing of the request. */
61 if (__builtin_expect (sigev->sigev_notify == SIGEV_THREAD, 0))
63 /* We have to start a thread. */
64 pthread_t tid;
65 pthread_attr_t attr, *pattr;
67 pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
68 if (pattr == NULL)
70 pthread_attr_init (&attr);
71 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
72 pattr = &attr;
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
87 nf->func = sigev->sigev_notify_function;
88 nf->value = sigev->sigev_value;
89 if (pthread_create (&tid, pattr, notify_func_wrapper, nf) < 0)
91 free (nf);
92 result = -1;
96 else if (sigev->sigev_notify == SIGEV_SIGNAL)
98 /* We have to send a signal. */
99 #if _POSIX_REALTIME_SIGNALS
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
117 return result;
121 void
122 internal_function
123 __aio_notify (struct requestlist *req)
125 struct waitlist *waitlist;
126 struct aiocb *aiocbp = &req->aiocbp->aiocb;
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
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;
140 /* Now also notify possibly waiting threads. */
141 waitlist = req->waiting;
142 while (waitlist != NULL)
144 struct waitlist *next = waitlist->next;
146 if (waitlist->sigevp == NULL)
148 if (waitlist->result != NULL && aiocbp->__return_value == -1)
149 *waitlist->result = -1;
151 #ifdef DONT_NEED_AIO_MISC_COND
152 AIO_MISC_NOTIFY (waitlist);
153 #else
154 /* Decrement the counter. */
155 --*waitlist->counterp;
157 pthread_cond_signal (waitlist->cond);
158 #endif
160 else
161 /* This is part of a asynchronous `lio_listio' operation. If
162 this request is the last one, send the signal. */
163 if (--*waitlist->counterp == 0)
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);
175 waitlist = next;