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
27 #ifndef aio_start_notify_thread
28 # define aio_start_notify_thread() do { } while (0)
33 void (*func
) (sigval_t
);
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
;
52 #ifdef BROKEN_THREAD_SIGNALS
53 __aio_notify_only (struct sigevent
*sigev
, pid_t caller_pid
)
55 __aio_notify_only (struct sigevent
*sigev
)
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. */
65 pthread_attr_t attr
, *pattr
;
67 pattr
= (pthread_attr_t
*) sigev
->sigev_notify_attributes
;
70 pthread_attr_init (&attr
);
71 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
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
);
87 nf
->func
= sigev
->sigev_notify_function
;
88 nf
->value
= sigev
->sigev_value
;
89 if (pthread_create (&tid
, pattr
, notify_func_wrapper
, nf
) < 0)
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
)
107 if (__aio_sigqueue (sigev
->sigev_signo
, sigev
->sigev_value
, getpid ())
112 /* There are no queued signals on this system at all. */
113 result
= raise (sigev
->sigev_signo
);
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)
131 if (__aio_notify_only (&aiocbp
->aio_sigevent
) != 0)
134 /* XXX What shall we do if already an error is set by
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
);
154 /* Decrement the counter. */
155 --*waitlist
->counterp
;
157 pthread_cond_signal (waitlist
->cond
);
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
);
168 __aio_notify_only (waitlist
->sigevp
);
170 /* This is tricky. See lio_listio.c for the reason why
172 free ((void *) waitlist
->counterp
);