Support cancellation in librt.
[glibc.git] / sysdeps / pthread / lio_listio64.c
blob165ce05b48748fd19df4a42ac34ab9402354e558
1 /* Enqueue and list of read or write requests, 64bit offset version.
2 Copyright (C) 1997, 1998, 1999, 2003 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 <aio.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #include "aio_misc.h"
29 /* We need this special structure to handle asynchronous I/O. */
30 struct async_waitlist
32 int counter;
33 struct sigevent sigev;
34 struct waitlist list[0];
38 int
39 lio_listio64 (mode, list, nent, sig)
40 int mode;
41 struct aiocb64 *const list[];
42 int nent;
43 struct sigevent *sig;
45 struct sigevent defsigev;
46 struct requestlist *requests[nent];
47 int cnt;
48 volatile int total = 0;
49 int result = 0;
51 /* Check arguments. */
52 if (mode != LIO_WAIT && mode != LIO_NOWAIT)
54 __set_errno (EINVAL);
55 return -1;
58 if (sig == NULL)
60 defsigev.sigev_notify = SIGEV_NONE;
61 sig = &defsigev;
64 /* Request the mutex. */
65 pthread_mutex_lock (&__aio_requests_mutex);
67 /* Now we can enqueue all requests. Since we already acquired the
68 mutex the enqueue function need not do this. */
69 for (cnt = 0; cnt < nent; ++cnt)
70 if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP)
72 requests[cnt] = __aio_enqueue_request ((aiocb_union *) list[cnt],
73 (list[cnt]->aio_lio_opcode
74 | 128));
75 if (requests[cnt] != NULL)
76 /* Successfully enqueued. */
77 ++total;
78 else
79 /* Signal that we've seen an error. `errno' and the error code
80 of the aiocb will tell more. */
81 result = -1;
84 if (total == 0)
86 /* We don't have anything to do except signalling if we work
87 asynchronously. */
89 /* Release the mutex. We do this before raising a signal since the
90 signal handler might do a `siglongjmp' and then the mutex is
91 locked forever. */
92 pthread_mutex_unlock (&__aio_requests_mutex);
94 if (mode == LIO_NOWAIT)
95 __aio_notify_only (sig,
96 sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0);
98 return result;
100 else if (mode == LIO_WAIT)
102 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
103 struct waitlist waitlist[nent];
104 int oldstate;
106 total = 0;
107 for (cnt = 0; cnt < nent; ++cnt)
108 if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP
109 && requests[cnt] != NULL)
111 waitlist[cnt].cond = &cond;
112 waitlist[cnt].next = requests[cnt]->waiting;
113 waitlist[cnt].counterp = &total;
114 waitlist[cnt].sigevp = NULL;
115 waitlist[cnt].caller_pid = 0; /* Not needed. */
116 requests[cnt]->waiting = &waitlist[cnt];
117 ++total;
120 /* Since `pthread_cond_wait'/`pthread_cond_timedwait' are cancelation
121 points we must be careful. We added entries to the waiting lists
122 which we must remove. So defer cancelation for now. */
123 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
125 while (total > 0)
126 pthread_cond_wait (&cond, &__aio_requests_mutex);
128 /* Now it's time to restore the cancelation state. */
129 pthread_setcancelstate (oldstate, NULL);
131 /* Release the conditional variable. */
132 if (pthread_cond_destroy (&cond) != 0)
133 /* This must never happen. */
134 abort ();
136 else
138 struct async_waitlist *waitlist;
140 waitlist = (struct async_waitlist *)
141 malloc (sizeof (struct async_waitlist)
142 + (nent * sizeof (struct waitlist)));
144 if (waitlist == NULL)
146 __set_errno (EAGAIN);
147 result = -1;
149 else
151 pid_t caller_pid = sig->sigev_notify == SIGEV_SIGNAL ? getpid () : 0;
152 total = 0;
154 for (cnt = 0; cnt < nent; ++cnt)
155 if (list[cnt] != NULL && list[cnt]->aio_lio_opcode != LIO_NOP
156 && requests[cnt] != NULL)
158 waitlist->list[cnt].cond = NULL;
159 waitlist->list[cnt].next = requests[cnt]->waiting;
160 waitlist->list[cnt].counterp = &waitlist->counter;
161 waitlist->list[cnt].sigevp = &waitlist->sigev;
162 waitlist->list[cnt].caller_pid = caller_pid;
163 requests[cnt]->waiting = &waitlist->list[cnt];
164 ++total;
167 waitlist->counter = total;
168 waitlist->sigev = *sig;
172 /* Release the mutex. */
173 pthread_mutex_unlock (&__aio_requests_mutex);
175 return result;