1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @brief Cancel Asynchronous I/O Operation |
---|
7 | * @ingroup POSIX_AIO |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * Copyright 2010, Alin Rus <alin.codejunkie@gmail.com> |
---|
12 | * |
---|
13 | * Redistribution and use in source and binary forms, with or without |
---|
14 | * modification, are permitted provided that the following conditions |
---|
15 | * are met: |
---|
16 | * 1. Redistributions of source code must retain the above copyright |
---|
17 | * notice, this list of conditions and the following disclaimer. |
---|
18 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer in the |
---|
20 | * documentation and/or other materials provided with the distribution. |
---|
21 | * |
---|
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
32 | * POSSIBILITY OF SUCH DAMAGE. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifdef HAVE_CONFIG_H |
---|
36 | #include "config.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #include <aio.h> |
---|
40 | #include <rtems/posix/aio_misc.h> |
---|
41 | #include <errno.h> |
---|
42 | #include <stdlib.h> |
---|
43 | #include <rtems/seterr.h> |
---|
44 | |
---|
45 | int aio_cancel(int fildes, struct aiocb *aiocbp) |
---|
46 | { |
---|
47 | rtems_chain_control *idle_req_chain = &aio_request_queue.idle_req; |
---|
48 | rtems_chain_control *work_req_chain = &aio_request_queue.work_req; |
---|
49 | rtems_aio_request_chain *r_chain; |
---|
50 | int result; |
---|
51 | |
---|
52 | pthread_mutex_lock (&aio_request_queue.mutex); |
---|
53 | |
---|
54 | if (fcntl (fildes, F_GETFD) < 0) { |
---|
55 | pthread_mutex_unlock(&aio_request_queue.mutex); |
---|
56 | rtems_set_errno_and_return_minus_one (EBADF); |
---|
57 | } |
---|
58 | |
---|
59 | /* if aiocbp is NULL remove all request for given file descriptor */ |
---|
60 | if (aiocbp == NULL) { |
---|
61 | AIO_printf ("Cancel all requests\n"); |
---|
62 | |
---|
63 | r_chain = rtems_aio_search_fd (work_req_chain, fildes, 0); |
---|
64 | if (r_chain == NULL) { |
---|
65 | AIO_printf ("Request chain not on [WQ]\n"); |
---|
66 | |
---|
67 | if (!rtems_chain_is_empty (idle_req_chain)) { |
---|
68 | r_chain = rtems_aio_search_fd (idle_req_chain, fildes, 0); |
---|
69 | if (r_chain == NULL) { |
---|
70 | pthread_mutex_unlock(&aio_request_queue.mutex); |
---|
71 | return AIO_ALLDONE; |
---|
72 | } |
---|
73 | |
---|
74 | AIO_printf ("Request chain on [IQ]\n"); |
---|
75 | |
---|
76 | rtems_chain_extract (&r_chain->next_fd); |
---|
77 | rtems_aio_remove_fd (r_chain); |
---|
78 | pthread_mutex_destroy (&r_chain->mutex); |
---|
79 | pthread_cond_destroy (&r_chain->cond); |
---|
80 | free (r_chain); |
---|
81 | |
---|
82 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
83 | return AIO_CANCELED; |
---|
84 | } |
---|
85 | |
---|
86 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
87 | return AIO_ALLDONE; |
---|
88 | } |
---|
89 | |
---|
90 | AIO_printf ("Request chain on [WQ]\n"); |
---|
91 | |
---|
92 | pthread_mutex_lock (&r_chain->mutex); |
---|
93 | rtems_chain_extract (&r_chain->next_fd); |
---|
94 | rtems_aio_remove_fd (r_chain); |
---|
95 | pthread_mutex_unlock (&r_chain->mutex); |
---|
96 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
97 | return AIO_CANCELED; |
---|
98 | } else { |
---|
99 | AIO_printf ("Cancel request\n"); |
---|
100 | |
---|
101 | if (aiocbp->aio_fildes != fildes) { |
---|
102 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
103 | rtems_set_errno_and_return_minus_one (EINVAL); |
---|
104 | } |
---|
105 | |
---|
106 | r_chain = rtems_aio_search_fd (work_req_chain, fildes, 0); |
---|
107 | if (r_chain == NULL) { |
---|
108 | if (!rtems_chain_is_empty (idle_req_chain)) { |
---|
109 | r_chain = rtems_aio_search_fd (idle_req_chain, fildes, 0); |
---|
110 | if (r_chain == NULL) { |
---|
111 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
112 | rtems_set_errno_and_return_minus_one (EINVAL); |
---|
113 | } |
---|
114 | |
---|
115 | AIO_printf ("Request on [IQ]\n"); |
---|
116 | |
---|
117 | result = rtems_aio_remove_req (&r_chain->perfd, aiocbp); |
---|
118 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
119 | return result; |
---|
120 | } else { |
---|
121 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
122 | return AIO_ALLDONE; |
---|
123 | } |
---|
124 | } |
---|
125 | AIO_printf ("Request on [WQ]\n"); |
---|
126 | |
---|
127 | pthread_mutex_lock (&r_chain->mutex); |
---|
128 | result = rtems_aio_remove_req (&r_chain->perfd, aiocbp); |
---|
129 | pthread_mutex_unlock (&r_chain->mutex); |
---|
130 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
131 | return result; |
---|
132 | } |
---|
133 | return AIO_ALLDONE; |
---|
134 | } |
---|