Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:21

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup POSIX_AIO
0007  *
0008  * @brief Cancel Asynchronous I/O Operation.
0009  */
0010 
0011 /*
0012  *  Copyright 2010, Alin Rus <alin.codejunkie@gmail.com>
0013  *  Copyright 2024, Alessandro Nardin <ale.daluch@gmail.com>
0014  * 
0015  *  COPYRIGHT (c) 1989-2011.
0016  *  On-Line Applications Research Corporation (OAR).
0017  *
0018  * Redistribution and use in source and binary forms, with or without
0019  * modification, are permitted provided that the following conditions
0020  * are met:
0021  * 1. Redistributions of source code must retain the above copyright
0022  *    notice, this list of conditions and the following disclaimer.
0023  * 2. Redistributions in binary form must reproduce the above copyright
0024  *    notice, this list of conditions and the following disclaimer in the
0025  *    documentation and/or other materials provided with the distribution.
0026  *
0027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0037  * POSSIBILITY OF SUCH DAMAGE.
0038  */
0039 
0040 #ifdef HAVE_CONFIG_H
0041 #include "config.h"
0042 #endif
0043 
0044 #include <aio.h>
0045 #include <rtems/posix/aio_misc.h>
0046 #include <errno.h>
0047 #include <stdlib.h>
0048 #include <rtems/seterr.h>
0049 
0050 int aio_cancel( int fildes, struct aiocb  *aiocbp )
0051 {
0052   rtems_chain_control *idle_req_chain = &aio_request_queue.idle_req;
0053   rtems_chain_control *work_req_chain = &aio_request_queue.work_req;
0054   rtems_aio_request_chain *r_chain;
0055   int result;
0056 
0057   pthread_mutex_lock( &aio_request_queue.mutex );
0058 
0059   if ( fcntl( fildes, F_GETFD ) < 0 ) {
0060     pthread_mutex_unlock( &aio_request_queue.mutex );
0061     rtems_set_errno_and_return_minus_one( EBADF );
0062   }
0063 
0064   /* if aiocbp is NULL remove all request for given file descriptor */
0065   if ( aiocbp == NULL ) {
0066     AIO_printf( "Cancel all requests\n" );
0067 
0068     r_chain = rtems_aio_search_fd( work_req_chain, fildes, 0 );
0069     if ( r_chain == NULL ) {
0070       AIO_printf( "Request chain not on [WQ]\n" );
0071 
0072       if ( !rtems_chain_is_empty( idle_req_chain ) ) {
0073         r_chain = rtems_aio_search_fd( idle_req_chain, fildes, 0 );
0074         if ( r_chain == NULL ) {
0075           pthread_mutex_unlock( &aio_request_queue.mutex );
0076           return AIO_ALLDONE;
0077         }
0078 
0079         AIO_printf( "Request chain on [IQ]\n" );
0080 
0081         rtems_chain_extract( &r_chain->next_fd );
0082         rtems_aio_remove_fd( r_chain );
0083         pthread_mutex_destroy( &r_chain->mutex );
0084         pthread_cond_destroy( &r_chain->cond );
0085         free( r_chain );
0086 
0087         pthread_mutex_unlock( &aio_request_queue.mutex );
0088         return AIO_CANCELED;
0089       }
0090 
0091       pthread_mutex_unlock( &aio_request_queue.mutex );
0092       return AIO_ALLDONE;
0093     }
0094 
0095     AIO_printf( "Request chain on [WQ]\n" );
0096 
0097     pthread_mutex_lock( &r_chain->mutex );
0098     rtems_chain_extract( &r_chain->next_fd );
0099     rtems_aio_remove_fd( r_chain );
0100     pthread_mutex_unlock( &r_chain->mutex );
0101     pthread_mutex_unlock( &aio_request_queue.mutex );
0102     return AIO_CANCELED;
0103   } else {
0104     AIO_printf( "Cancel request\n" );
0105 
0106     if( aiocbp->aio_fildes != fildes ) {
0107       pthread_mutex_unlock( &aio_request_queue.mutex );
0108       rtems_set_errno_and_return_minus_one( EINVAL );
0109     }
0110       
0111     r_chain = rtems_aio_search_fd( work_req_chain, fildes, 0 );
0112     if ( r_chain == NULL ) {
0113       if ( !rtems_chain_is_empty( idle_req_chain ) ) {
0114         r_chain = rtems_aio_search_fd( idle_req_chain, fildes, 0 );
0115         if (r_chain == NULL) { 
0116           pthread_mutex_unlock( &aio_request_queue.mutex );
0117           rtems_set_errno_and_return_minus_one( EINVAL );
0118         }
0119 
0120         AIO_printf( "Request on [IQ]\n" );
0121 
0122         result = rtems_aio_remove_req( &r_chain->perfd, aiocbp );
0123         pthread_mutex_unlock( &aio_request_queue.mutex );
0124         return result;
0125       } else {
0126         pthread_mutex_unlock( &aio_request_queue.mutex );
0127         return AIO_ALLDONE;
0128       }
0129     }
0130 
0131     AIO_printf( "Request on [WQ]\n" );
0132 
0133     pthread_mutex_lock( &r_chain->mutex );
0134     result = rtems_aio_remove_req( &r_chain->perfd, aiocbp );
0135     pthread_mutex_unlock( &r_chain->mutex );
0136     pthread_mutex_unlock( &aio_request_queue.mutex );
0137     return result;
0138   }
0139   return AIO_ALLDONE;
0140 }
0141