]> git.ipfire.org Git - thirdparty/glibc.git/blob - rt/aio_notify.c
Update.
[thirdparty/glibc.git] / rt / aio_notify.c
1 /* Notify initiator of AIO request.
2 Copyright (C) 1997 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include "aio_misc.h"
24
25 int
26 __aio_notify_only (struct sigevent *sigev)
27 {
28 int result = 0;
29
30 /* Send the signal to notify about finished processing of the request. */
31 if (sigev->sigev_notify == SIGEV_THREAD)
32 {
33 /* We have to start a thread. */
34 pthread_t tid;
35 pthread_attr_t attr, *pattr;
36
37 pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
38 if (pattr == NULL)
39 {
40 pthread_attr_init (&attr);
41 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
42 pattr = &attr;
43 }
44
45 if (pthread_create (&tid, pattr,
46 (void *(*) (void *)) sigev->sigev_notify_function,
47 sigev->sigev_value.sival_ptr) < 0)
48 result = -1;
49 }
50 else if (sigev->sigev_notify == SIGEV_SIGNAL)
51 /* We have to send a signal. */
52 if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value) < 0)
53 result = -1;
54
55 return result;
56 }
57
58
59 void
60 __aio_notify (struct requestlist *req)
61 {
62 struct waitlist *waitlist;
63 struct aiocb *aiocbp = &req->aiocbp->aiocb;
64
65 if (__aio_notify_only (&aiocbp->aio_sigevent) != 0)
66 {
67 /* XXX What shall we do if already an error is set by
68 read/write/fsync? */
69 aiocbp->__error_code = errno;
70 aiocbp->__return_value = -1;
71 }
72
73 /* Now also notify possibly waiting threads. */
74 waitlist = req->waiting;
75 while (waitlist != NULL)
76 {
77 struct waitlist *next = waitlist->next;
78
79 /* Decrement the counter. This is used in both cases. */
80 --*waitlist->counterp;
81
82 if (waitlist->sigevp == NULL)
83 pthread_cond_signal (waitlist->cond);
84 else
85 /* This is part of a asynchronous `lio_listio' operation. If
86 this request is the last one, send the signal. */
87 if (*waitlist->counterp == 0)
88 {
89 __aio_notify_only (waitlist->sigevp);
90 /* This is tricky. See lio_listio.c for the reason why
91 this works. */
92 free ((void *) waitlist->counterp);
93 }
94
95 waitlist = next;
96 }
97 }