* sysdeps/unix/sysv/linux/m68k/sysdep.h (INLINE_SYSCALL): Don't
[glibc.git] / sysdeps / pthread / aio_notify.c
blob4537ca5aab174153f9c97ca6ce8646ac1fb4dd6b
1 /* Notify initiator of AIO request.
2 Copyright (C) 1997,98,99,2000,01 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"
28 static void *
29 notify_func_wrapper (void *arg)
31 struct sigevent *sigev = arg;
32 sigev->sigev_notify_function (sigev->sigev_value);
33 return NULL;
37 int
38 internal_function
39 __aio_notify_only (struct sigevent *sigev, pid_t caller_pid)
41 int result = 0;
43 /* Send the signal to notify about finished processing of the request. */
44 if (sigev->sigev_notify == SIGEV_THREAD)
46 /* We have to start a thread. */
47 pthread_t tid;
48 pthread_attr_t attr, *pattr;
50 pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
51 if (pattr == NULL)
53 pthread_attr_init (&attr);
54 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
55 pattr = &attr;
58 if (pthread_create (&tid, pattr, notify_func_wrapper, sigev) < 0)
59 result = -1;
61 else if (sigev->sigev_notify == SIGEV_SIGNAL)
63 /* We have to send a signal. */
64 #if _POSIX_REALTIME_SIGNALS
65 /* Note that the standard gives us the option of using a plain
66 non-queuing signal here when SA_SIGINFO is not set for the signal. */
67 if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value, caller_pid)
68 < 0)
69 result = -1;
70 #else
71 /* There are no queued signals on this system at all. */
72 result = raise (sigev->sigev_signo);
73 #endif
76 return result;
80 void
81 internal_function
82 __aio_notify (struct requestlist *req)
84 struct waitlist *waitlist;
85 struct aiocb *aiocbp = &req->aiocbp->aiocb;
87 if (__aio_notify_only (&aiocbp->aio_sigevent, req->caller_pid) != 0)
89 /* XXX What shall we do if already an error is set by
90 read/write/fsync? */
91 aiocbp->__error_code = errno;
92 aiocbp->__return_value = -1;
95 /* Now also notify possibly waiting threads. */
96 waitlist = req->waiting;
97 while (waitlist != NULL)
99 struct waitlist *next = waitlist->next;
101 /* Decrement the counter. This is used in both cases. */
102 --*waitlist->counterp;
104 if (waitlist->sigevp == NULL)
105 pthread_cond_signal (waitlist->cond);
106 else
107 /* This is part of a asynchronous `lio_listio' operation. If
108 this request is the last one, send the signal. */
109 if (*waitlist->counterp == 0)
111 __aio_notify_only (waitlist->sigevp, waitlist->caller_pid);
112 /* This is tricky. See lio_listio.c for the reason why
113 this works. */
114 free ((void *) waitlist->counterp);
117 waitlist = next;