8c57d72e9bc0a9d515ef0321a2399b0f9a401680
[platform/upstream/glibc.git] / rt / aio_notify.c
1 /* Notify initiator of AIO request.
2    Copyright (C) 1997, 1998 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 <errno.h>
22 #include <pthread.h>
23 #include <stdlib.h>
24 #include "aio_misc.h"
25
26 int
27 internal_function
28 __aio_notify_only (struct sigevent *sigev)
29 {
30   int result = 0;
31
32   /* Send the signal to notify about finished processing of the request.  */
33   if (sigev->sigev_notify == SIGEV_THREAD)
34     {
35       /* We have to start a thread.  */
36       pthread_t tid;
37       pthread_attr_t attr, *pattr;
38
39       pattr = (pthread_attr_t *) sigev->sigev_notify_attributes;
40       if (pattr == NULL)
41         {
42           pthread_attr_init (&attr);
43           pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
44           pattr = &attr;
45         }
46
47       if (pthread_create (&tid, pattr,
48                           (void *(*) (void *)) sigev->sigev_notify_function,
49                           sigev->sigev_value.sival_ptr) < 0)
50         result = -1;
51     }
52   else if (sigev->sigev_notify == SIGEV_SIGNAL)
53     /* We have to send a signal.  */
54     if (__aio_sigqueue (sigev->sigev_signo, sigev->sigev_value) < 0)
55       result = -1;
56
57   return result;
58 }
59
60
61 void
62 internal_function
63 __aio_notify (struct requestlist *req)
64 {
65   struct waitlist *waitlist;
66   struct aiocb *aiocbp = &req->aiocbp->aiocb;
67
68   if (__aio_notify_only (&aiocbp->aio_sigevent) != 0)
69     {
70       /* XXX What shall we do if already an error is set by
71          read/write/fsync?  */
72       aiocbp->__error_code = errno;
73       aiocbp->__return_value = -1;
74     }
75
76   /* Now also notify possibly waiting threads.  */
77   waitlist = req->waiting;
78   while (waitlist != NULL)
79     {
80       struct waitlist *next = waitlist->next;
81
82       /* Decrement the counter.  This is used in both cases.  */
83       --*waitlist->counterp;
84
85       if (waitlist->sigevp == NULL)
86         pthread_cond_signal (waitlist->cond);
87       else
88         /* This is part of a asynchronous `lio_listio' operation.  If
89            this request is the last one, send the signal.  */
90         if (*waitlist->counterp == 0)
91           {
92             __aio_notify_only (waitlist->sigevp);
93             /* This is tricky.  See lio_listio.c for the reason why
94                this works.  */
95             free ((void *) waitlist->counterp);
96           }
97
98       waitlist = next;
99     }
100 }