]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/pthread/aio_cancel.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / pthread / aio_cancel.c
CommitLineData
cbdee279 1/* Cancel requests associated with given file descriptor.
f7a9f785 2 Copyright (C) 1997-2016 Free Software Foundation, Inc.
cbdee279
UD
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
41bdb6e2
AJ
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.
cbdee279
UD
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
41bdb6e2 14 Lesser General Public License for more details.
cbdee279 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
cbdee279
UD
19
20
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. */
5fe14d96 26#ifndef aio_cancel
cbdee279
UD
27#define aio_cancel64 XXX
28#include <aio.h>
29/* And undo the hack. */
30#undef aio_cancel64
5fe14d96 31#endif
cbdee279 32
56ddf355 33#include <assert.h>
cbdee279 34#include <errno.h>
12e5c3b9 35#include <fcntl.h>
cbdee279 36
8f480b4b 37#include <aio_misc.h>
cbdee279
UD
38
39
cbdee279 40int
9dd346ff 41aio_cancel (int fildes, struct aiocb *aiocbp)
cbdee279 42{
d71b808a 43 struct requestlist *req = NULL;
cbdee279
UD
44 int result = AIO_ALLDONE;
45
8b8cc76f
UD
46 /* If fildes is invalid, error. */
47 if (fcntl (fildes, F_GETFL) < 0)
48 {
49 __set_errno (EBADF);
50 return -1;
51 }
52
d71b808a
UD
53 /* Request the mutex. */
54 pthread_mutex_lock (&__aio_requests_mutex);
cbdee279 55
d71b808a
UD
56 /* We are asked to cancel a specific AIO request. */
57 if (aiocbp != NULL)
cbdee279 58 {
d71b808a
UD
59 /* If the AIO request is not for this descriptor it has no value
60 to look for the request block. */
7b787f85
UD
61 if (aiocbp->aio_fildes != fildes)
62 {
63 pthread_mutex_unlock (&__aio_requests_mutex);
64 __set_errno (EINVAL);
65 return -1;
66 }
67 else if (aiocbp->__error_code == EINPROGRESS)
cbdee279 68 {
d71b808a
UD
69 struct requestlist *last = NULL;
70
71 req = __aio_find_req_fd (fildes);
72
950c1194
UD
73 if (req == NULL)
74 {
75 not_found:
76 pthread_mutex_unlock (&__aio_requests_mutex);
77 __set_errno (EINVAL);
78 return -1;
79 }
80
d71b808a 81 while (req->aiocbp != (aiocb_union *) aiocbp)
cbdee279 82 {
d71b808a
UD
83 last = req;
84 req = req->next_prio;
a3bfd999 85 if (req == NULL)
950c1194 86 goto not_found;
cbdee279 87 }
cbdee279 88
d71b808a
UD
89 /* Don't remove the entry if a thread is already working on it. */
90 if (req->running == allocated)
56ddf355
UD
91 {
92 result = AIO_NOTCANCELED;
93 req = NULL;
94 }
95 else
cbdee279 96 {
d71b808a 97 /* We can remove the entry. */
950c1194 98 __aio_remove_request (last, req, 0);
d71b808a
UD
99
100 result = AIO_CANCELED;
cbdee279 101
56ddf355
UD
102 req->next_prio = NULL;
103 }
cbdee279
UD
104 }
105 }
d71b808a
UD
106 else
107 {
108 /* Find the beginning of the list of all requests for this
109 desriptor. */
110 req = __aio_find_req_fd (fildes);
111
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)
7ef90c15
UD
115 {
116 if (req->running == allocated)
117 {
118 struct requestlist *old = req;
119 req = req->next_prio;
120 old->next_prio = NULL;
d71b808a 121
7ef90c15 122 result = AIO_NOTCANCELED;
56ddf355
UD
123
124 if (req != NULL)
125 __aio_remove_request (old, req, 1);
7ef90c15
UD
126 }
127 else
56ddf355
UD
128 {
129 result = AIO_CANCELED;
7ef90c15 130
56ddf355
UD
131 /* We can remove the entry. */
132 __aio_remove_request (NULL, req, 1);
133 }
7ef90c15 134 }
d71b808a
UD
135 }
136
137 /* Mark requests as canceled and send signal. */
138 while (req != NULL)
139 {
140 struct requestlist *old = req;
56ddf355 141 assert (req->running == yes || req->running == queued);
d71b808a
UD
142 req->aiocbp->aiocb.__error_code = ECANCELED;
143 req->aiocbp->aiocb.__return_value = -1;
144 __aio_notify (req);
145 req = req->next_prio;
146 __aio_free_request (old);
147 }
cbdee279 148
d71b808a
UD
149 /* Release the mutex. */
150 pthread_mutex_unlock (&__aio_requests_mutex);
cbdee279
UD
151
152 return result;
153}
154
5fe14d96 155#ifndef aio_cancel
cbdee279 156weak_alias (aio_cancel, aio_cancel64)
5fe14d96 157#endif