Update.
[glibc.git] / rt / aio_cancel.c
blobdcb7d5ef8dafd01d1c64cf0a0c7020b452621585
1 /* Cancel requests associated with given file descriptor.
2 Copyright (C) 1997, 1998, 2000 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 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.
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.
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. */
22 /* We use an UGLY hack to prevent gcc from finding us cheating. The
23 implementation of aio_cancel and aio_cancel64 are identical and so
24 we want to avoid code duplication by using aliases. But gcc sees
25 the different parameter lists and prints a warning. We define here
26 a function so that aio_cancel64 has no prototype. */
27 #define aio_cancel64 XXX
28 #include <aio.h>
29 /* And undo the hack. */
30 #undef aio_cancel64
32 #include <errno.h>
34 #include "aio_misc.h"
37 int
38 aio_cancel (fildes, aiocbp)
39 int fildes;
40 struct aiocb *aiocbp;
42 struct requestlist *req = NULL;
43 int result = AIO_ALLDONE;
45 /* Request the mutex. */
46 pthread_mutex_lock (&__aio_requests_mutex);
48 /* We are asked to cancel a specific AIO request. */
49 if (aiocbp != NULL)
51 /* If the AIO request is not for this descriptor it has no value
52 to look for the request block. */
53 if (aiocbp->aio_fildes == fildes)
55 struct requestlist *last = NULL;
57 req = __aio_find_req_fd (fildes);
59 while (req->aiocbp != (aiocb_union *) aiocbp)
61 last = req;
62 req = req->next_prio;
63 if (req == NULL)
65 pthread_mutex_unlock (&__aio_requests_mutex);
66 __set_errno (EINVAL);
67 return -1;
71 /* Don't remove the entry if a thread is already working on it. */
72 if (req->running == allocated)
73 result = AIO_NOTCANCELED;
74 else if (req->running == yes)
76 /* We can remove the entry. */
77 if (last != NULL)
78 last->next_prio = req->next_prio;
79 else
80 if (req->next_prio == NULL)
82 if (req->last_fd != NULL)
83 req->last_fd->next_fd = req->next_fd;
84 if (req->next_fd != NULL)
85 req->next_fd->last_fd = req->last_fd;
87 else
89 if (req->last_fd != NULL)
90 req->last_fd->next_fd = req->next_prio;
91 if (req->next_fd != NULL)
92 req->next_fd->last_fd = req->next_prio;
93 req->next_prio->last_fd = req->last_fd;
94 req->next_prio->next_fd = req->next_fd;
96 /* Mark this entry as runnable. */
97 req->next_prio->running = yes;
100 result = AIO_CANCELED;
103 req->next_prio = NULL;
106 else
108 /* Find the beginning of the list of all requests for this
109 desriptor. */
110 req = __aio_find_req_fd (fildes);
112 /* If any request is worked on by a thread it must be the first.
113 So either we can delete all requests or all but the first. */
114 if (req != NULL)
116 if (req->running == allocated)
118 struct requestlist *old = req;
119 req = req->next_prio;
120 old->next_prio = NULL;
122 result = AIO_NOTCANCELED;
124 else
126 /* Remove entry from the file descriptor list. */
127 if (req->last_fd != NULL)
128 req->last_fd->next_fd = req->next_fd;
129 if (req->next_fd != NULL)
130 req->next_fd->last_fd = req->last_fd;
132 result = AIO_CANCELED;
137 /* Mark requests as canceled and send signal. */
138 while (req != NULL)
140 struct requestlist *old = req;
141 req->aiocbp->aiocb.__error_code = ECANCELED;
142 req->aiocbp->aiocb.__return_value = -1;
143 __aio_notify (req);
144 req = req->next_prio;
145 __aio_free_request (old);
148 /* Release the mutex. */
149 pthread_mutex_unlock (&__aio_requests_mutex);
151 return result;
154 weak_alias (aio_cancel, aio_cancel64)