2.5-18.1
[glibc.git] / rtkaio / sysdeps / unix / sysv / linux / kaio_cancel.c
blob7d2738d5bc4b5f3df5f10adbc97a29d186c5b8f5
1 /* Cancel requests associated with given file descriptor.
2 Copyright (C) 1997, 1998, 2000, 2002, 2003, 2005 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 /* We use an UGLY hack to prevent gcc from finding us cheating. The
22 implementation of aio_cancel and aio_cancel64 are identical and so
23 we want to avoid code duplication by using aliases. But gcc sees
24 the different parameter lists and prints a warning. We define here
25 a function so that aio_cancel64 has no prototype. */
26 #ifndef aio_cancel
27 #define aio_cancel64 XXX
28 #include <aio.h>
29 /* And undo the hack. */
30 #undef aio_cancel64
31 #endif
33 #include <kaio_misc.h>
35 #ifndef USE_KAIO
36 #include <aio_cancel.c>
37 #else
39 #include <assert.h>
40 #include <errno.h>
42 int
43 aio_cancel (fildes, aiocbp)
44 int fildes;
45 struct aiocb *aiocbp;
47 struct requestlist *req = NULL;
48 int result = AIO_ALLDONE;
50 /* If fildes is invalid, error. */
51 if (fcntl (fildes, F_GETFL) < 0)
53 __set_errno (EBADF);
54 return -1;
57 /* Request the mutex. */
58 pthread_mutex_lock (&__aio_requests_mutex);
60 /* We are asked to cancel a specific AIO request. */
61 if (aiocbp != NULL)
63 /* If the AIO request is not for this descriptor it has no value
64 to look for the request block. */
65 if (aiocbp->aio_fildes != fildes)
67 pthread_mutex_unlock (&__aio_requests_mutex);
68 __set_errno (EINVAL);
69 return -1;
71 else if (aiocbp->__error_code == EINPROGRESS)
73 struct requestlist *last = NULL;
75 req = __aio_find_req_fd (fildes);
77 if (req != NULL)
78 while (req->aiocbp != (aiocb_union *) aiocbp)
80 last = req;
81 req = req->next_prio;
82 if (req == NULL)
83 break;
86 if (req != NULL)
88 /* Don't remove the entry if a thread is already working on
89 it. */
90 if (req->running == allocated)
92 result = AIO_NOTCANCELED;
93 req = NULL;
95 else
97 /* We can remove the entry. */
98 __aio_remove_request (last, req, 0);
100 result = AIO_CANCELED;
102 req->next_prio = NULL;
105 else
107 /* Try kernel requests. */
108 req = __aio_find_req ((aiocb_union *) aiocbp);
110 if (req == NULL)
112 pthread_mutex_unlock (&__aio_requests_mutex);
113 __set_errno (EINVAL);
114 return -1;
117 assert (req->kioctx != KCTX_NONE);
119 /* If kernel is working on it, try cancelling it. */
120 if (req->running == allocated)
122 struct kio_event ev;
123 INTERNAL_SYSCALL_DECL (err);
124 int res;
126 res = INTERNAL_SYSCALL (io_cancel, err, 3, __aio_kioctx,
127 &req->kiocb, &ev);
128 if (INTERNAL_SYSCALL_ERROR_P (res, err))
130 result = AIO_NOTCANCELED;
131 req = NULL;
133 else
134 req->running = queued;
136 if (req != NULL)
138 /* We can remove the entry. */
139 __aio_remove_krequest (req);
140 result = AIO_CANCELED;
141 req->next_prio = NULL;
146 else
148 /* Find the beginning of the list of all requests for this
149 desriptor. */
150 req = __aio_find_req_fd (fildes);
152 /* If any request is worked on by a thread it must be the first.
153 So either we can delete all requests or all but the first. */
154 if (req != NULL)
156 if (req->running == allocated)
158 struct requestlist *old = req;
159 req = req->next_prio;
160 old->next_prio = NULL;
162 result = AIO_NOTCANCELED;
164 if (req != NULL)
165 __aio_remove_request (old, req, 1);
167 else
169 result = AIO_CANCELED;
171 /* We can remove the entry. */
172 __aio_remove_request (NULL, req, 1);
176 if (result != AIO_NOTCANCELED)
178 /* Try to cancel kernel requests. */
179 struct requestlist *kreq = __aio_find_kreq_fd (fildes);
181 while (kreq)
183 struct requestlist *next;
185 /* If kernel is working on it, try cancelling it. */
186 if (kreq->running == allocated)
188 struct kio_event ev;
189 INTERNAL_SYSCALL_DECL (err);
190 int res;
192 res = INTERNAL_SYSCALL (io_cancel, err, 3, __aio_kioctx,
193 &kreq->kiocb, &ev);
194 if (INTERNAL_SYSCALL_ERROR_P (res, err))
196 result = AIO_NOTCANCELED;
197 break;
199 else
200 kreq->running = queued;
202 next = kreq->next_prio;
203 __aio_remove_krequest (kreq);
204 result = AIO_CANCELED;
205 kreq->next_prio = NULL;
206 assert (kreq->running == yes || kreq->running == queued);
207 kreq->aiocbp->aiocb.__error_code = ECANCELED;
208 kreq->aiocbp->aiocb.__return_value = -1;
209 __aio_notify (kreq);
210 __aio_free_request (kreq);
211 kreq = next;
216 /* Mark requests as canceled and send signal. */
217 while (req != NULL)
219 struct requestlist *old = req;
220 assert (req->running == yes || req->running == queued);
221 req->aiocbp->aiocb.__error_code = ECANCELED;
222 req->aiocbp->aiocb.__return_value = -1;
223 __aio_notify (req);
224 req = req->next_prio;
225 __aio_free_request (old);
228 /* Release the mutex. */
229 pthread_mutex_unlock (&__aio_requests_mutex);
231 return result;
234 #ifndef aio_cancel
235 weak_alias (aio_cancel, aio_cancel64)
236 #endif
238 #endif