1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Cancel Asynchronous I/O Operation |
---|
5 | * @ingroup POSIX_AIO |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright 2010, Alin Rus <alin.codejunkie@gmail.com> |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.org/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <aio.h> |
---|
21 | #include <rtems/posix/aio_misc.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <rtems/system.h> |
---|
25 | #include <rtems/seterr.h> |
---|
26 | |
---|
27 | int aio_cancel(int fildes, struct aiocb *aiocbp) |
---|
28 | { |
---|
29 | rtems_chain_control *idle_req_chain = &aio_request_queue.idle_req; |
---|
30 | rtems_chain_control *work_req_chain = &aio_request_queue.work_req; |
---|
31 | rtems_aio_request_chain *r_chain; |
---|
32 | int result; |
---|
33 | |
---|
34 | pthread_mutex_lock (&aio_request_queue.mutex); |
---|
35 | |
---|
36 | if (fcntl (fildes, F_GETFD) < 0) { |
---|
37 | pthread_mutex_unlock(&aio_request_queue.mutex); |
---|
38 | rtems_set_errno_and_return_minus_one (EBADF); |
---|
39 | } |
---|
40 | |
---|
41 | /* if aiocbp is NULL remove all request for given file descriptor */ |
---|
42 | if (aiocbp == NULL) { |
---|
43 | AIO_printf ("Cancel all requests\n"); |
---|
44 | |
---|
45 | r_chain = rtems_aio_search_fd (work_req_chain, fildes, 0); |
---|
46 | if (r_chain == NULL) { |
---|
47 | AIO_printf ("Request chain not on [WQ]\n"); |
---|
48 | |
---|
49 | if (!rtems_chain_is_empty (idle_req_chain)) { |
---|
50 | r_chain = rtems_aio_search_fd (idle_req_chain, fildes, 0); |
---|
51 | if (r_chain == NULL) { |
---|
52 | pthread_mutex_unlock(&aio_request_queue.mutex); |
---|
53 | return AIO_ALLDONE; |
---|
54 | } |
---|
55 | |
---|
56 | AIO_printf ("Request chain on [IQ]\n"); |
---|
57 | |
---|
58 | rtems_chain_extract (&r_chain->next_fd); |
---|
59 | rtems_aio_remove_fd (r_chain); |
---|
60 | pthread_mutex_destroy (&r_chain->mutex); |
---|
61 | pthread_cond_destroy (&r_chain->mutex); |
---|
62 | free (r_chain); |
---|
63 | |
---|
64 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
65 | return AIO_CANCELED; |
---|
66 | } |
---|
67 | |
---|
68 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
69 | return AIO_ALLDONE; |
---|
70 | } |
---|
71 | |
---|
72 | AIO_printf ("Request chain on [WQ]\n"); |
---|
73 | |
---|
74 | pthread_mutex_lock (&r_chain->mutex); |
---|
75 | rtems_chain_extract (&r_chain->next_fd); |
---|
76 | rtems_aio_remove_fd (r_chain); |
---|
77 | pthread_mutex_unlock (&r_chain->mutex); |
---|
78 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
79 | return AIO_CANCELED; |
---|
80 | } else { |
---|
81 | AIO_printf ("Cancel request\n"); |
---|
82 | |
---|
83 | if (aiocbp->aio_fildes != fildes) { |
---|
84 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
85 | rtems_set_errno_and_return_minus_one (EINVAL); |
---|
86 | } |
---|
87 | |
---|
88 | r_chain = rtems_aio_search_fd (work_req_chain, fildes, 0); |
---|
89 | if (r_chain == NULL) { |
---|
90 | if (!rtems_chain_is_empty (idle_req_chain)) { |
---|
91 | r_chain = rtems_aio_search_fd (idle_req_chain, fildes, 0); |
---|
92 | if (r_chain == NULL) { |
---|
93 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
94 | rtems_set_errno_and_return_minus_one (EINVAL); |
---|
95 | } |
---|
96 | |
---|
97 | AIO_printf ("Request on [IQ]\n"); |
---|
98 | |
---|
99 | result = rtems_aio_remove_req (&r_chain->perfd, aiocbp); |
---|
100 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
101 | return result; |
---|
102 | } else { |
---|
103 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
104 | return AIO_ALLDONE; |
---|
105 | } |
---|
106 | } |
---|
107 | AIO_printf ("Request on [WQ]\n"); |
---|
108 | |
---|
109 | pthread_mutex_lock (&r_chain->mutex); |
---|
110 | result = rtems_aio_remove_req (&r_chain->perfd, aiocbp); |
---|
111 | pthread_mutex_unlock (&r_chain->mutex); |
---|
112 | pthread_mutex_unlock (&aio_request_queue.mutex); |
---|
113 | return result; |
---|
114 | } |
---|
115 | return AIO_ALLDONE; |
---|
116 | } |
---|