1 /* Cancel requests associated with given file descriptor.
2 Copyright (C) 1997-2017 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, see
18 <http://www.gnu.org/licenses/>. */
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. */
27 #define aio_cancel64 XXX
29 /* And undo the hack. */
41 aio_cancel (int fildes
, struct aiocb
*aiocbp
)
43 struct requestlist
*req
= NULL
;
44 int result
= AIO_ALLDONE
;
46 /* If fildes is invalid, error. */
47 if (fcntl (fildes
, F_GETFL
) < 0)
53 /* Request the mutex. */
54 pthread_mutex_lock (&__aio_requests_mutex
);
56 /* We are asked to cancel a specific AIO request. */
59 /* If the AIO request is not for this descriptor it has no value
60 to look for the request block. */
61 if (aiocbp
->aio_fildes
!= fildes
)
63 pthread_mutex_unlock (&__aio_requests_mutex
);
67 else if (aiocbp
->__error_code
== EINPROGRESS
)
69 struct requestlist
*last
= NULL
;
71 req
= __aio_find_req_fd (fildes
);
76 pthread_mutex_unlock (&__aio_requests_mutex
);
81 while (req
->aiocbp
!= (aiocb_union
*) aiocbp
)
89 /* Don't remove the entry if a thread is already working on it. */
90 if (req
->running
== allocated
)
92 result
= AIO_NOTCANCELED
;
97 /* We can remove the entry. */
98 __aio_remove_request (last
, req
, 0);
100 result
= AIO_CANCELED
;
102 req
->next_prio
= NULL
;
108 /* Find the beginning of the list of all requests for this
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. */
116 if (req
->running
== allocated
)
118 struct requestlist
*old
= req
;
119 req
= req
->next_prio
;
120 old
->next_prio
= NULL
;
122 result
= AIO_NOTCANCELED
;
125 __aio_remove_request (old
, req
, 1);
129 result
= AIO_CANCELED
;
131 /* We can remove the entry. */
132 __aio_remove_request (NULL
, req
, 1);
137 /* Mark requests as canceled and send signal. */
140 struct requestlist
*old
= req
;
141 assert (req
->running
== yes
|| req
->running
== queued
);
142 req
->aiocbp
->aiocb
.__error_code
= ECANCELED
;
143 req
->aiocbp
->aiocb
.__return_value
= -1;
145 req
= req
->next_prio
;
146 __aio_free_request (old
);
149 /* Release the mutex. */
150 pthread_mutex_unlock (&__aio_requests_mutex
);
156 weak_alias (aio_cancel
, aio_cancel64
)