Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright 2011, Alin Rus <alin.codejunkie@gmail.com>
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  *
0027  */
0028 
0029 #if !defined( OPERATION_COUNT )
0030 #define OPERATION_COUNT 100
0031 #endif
0032 
0033 #ifdef HAVE_CONFIG_H
0034 #include "config.h"
0035 #endif
0036 
0037 #define CONFIGURE_INIT
0038 #include "system.h"
0039 #include <rtems.h>
0040 #include "tmacros.h"
0041 #include <rtems/posix/aio_misc.h>
0042 #include <aio.h>
0043 #include <stdlib.h>
0044 #include <unistd.h>
0045 #include <stdio.h>
0046 #include <sched.h>
0047 #include <fcntl.h>
0048 #include <rtems/chain.h>
0049 
0050 const char rtems_test_name[] = "PSXAIO 3";
0051 
0052 /* forward declarations to avoid warnings */
0053 static struct aiocb *create_aiocb( int fd );
0054 static void free_aiocb( struct aiocb *aiocbp );
0055 
0056 #define FD_COUNT 6
0057 #define BUFSIZE 128
0058 
0059 static struct aiocb *create_aiocb( int fd )
0060 {
0061   struct aiocb *aiocbp;
0062 
0063   aiocbp = malloc( sizeof( struct aiocb ) );
0064   memset( aiocbp, 0, sizeof( struct aiocb ) );
0065   aiocbp->aio_buf = malloc( BUFSIZE * sizeof( char ) );
0066   aiocbp->aio_nbytes = BUFSIZE;
0067   aiocbp->aio_offset = 0;
0068   aiocbp->aio_reqprio = 0;
0069   aiocbp->aio_fildes = fd;
0070   aiocbp->aio_sigevent.sigev_notify = SIGEV_NONE;
0071   return aiocbp;
0072 }
0073 
0074 static void free_aiocb( struct aiocb *aiocbp )
0075 {
0076   free( (void*) aiocbp->aio_buf );
0077   free( aiocbp );
0078 }
0079 
0080 void *POSIX_Init( void *argument )
0081 {
0082   int fd[FD_COUNT];
0083   struct aiocb *aiocbp[FD_COUNT+1];
0084   int status, i, policy = SCHED_FIFO;
0085   char filename[BUFSIZE];
0086   struct sched_param param;
0087 
0088   status = rtems_aio_init();
0089   rtems_test_assert( status == 0 );
0090 
0091   param.sched_priority = 30;
0092   status = pthread_setschedparam( 
0093     pthread_self(), 
0094     policy, 
0095     &param 
0096   );
0097   rtems_test_assert( status == 0 );
0098 
0099   status = mkdir( "/tmp", S_IRWXU );
0100   rtems_test_assert( !status );
0101 
0102   TEST_BEGIN();
0103 
0104   for ( i=0; i<FD_COUNT; i++ )
0105     {
0106       sprintf( filename, "/tmp/aio_fildes%d",i );
0107       fd[i] = open( 
0108         filename, 
0109         O_RDWR|O_CREAT, 
0110         S_IRWXU|S_IRWXG|S_IRWXO 
0111       );
0112       rtems_test_assert( fd[i] != -1 );
0113     }
0114 
0115   aiocbp[0] = create_aiocb( fd[0] );
0116   status = aio_write( aiocbp[0] );
0117   rtems_test_assert( status != -1 );
0118 
0119   aiocbp[1] = create_aiocb( fd[1] );
0120   status = aio_write( aiocbp[1] );
0121   rtems_test_assert( status != -1 );
0122 
0123   aiocbp[2] = create_aiocb( fd[1] );
0124   status = aio_read( aiocbp[2] );
0125   rtems_test_assert( status != -1 );
0126 
0127   aiocbp[3] = create_aiocb( fd[2] );
0128   status = aio_write( aiocbp[3] );
0129   rtems_test_assert( status != -1 );
0130 
0131   aiocbp[4] = create_aiocb( fd[3] );
0132   status = aio_write( aiocbp[4] );
0133   rtems_test_assert( status != -1 );
0134 
0135   aiocbp[5] = create_aiocb( fd[4] );
0136   status = aio_write( aiocbp[5] );
0137   rtems_test_assert( status != -1 );
0138 
0139   aiocbp[6] = create_aiocb( fd[5] );
0140   status = aio_read( aiocbp[6] );
0141   rtems_test_assert( status != -1 );
0142 
0143   sleep( 5 );
0144   sleep( 5 );
0145   sleep( 5 );
0146 
0147   TEST_END();
0148 
0149   for( i = 0; i < FD_COUNT; i++ )
0150     {
0151       close( fd[i] );
0152       free_aiocb( aiocbp[i] );
0153     }
0154   free_aiocb( aiocbp[i] );
0155   rtems_test_exit( 0 );
0156 
0157   return NULL;
0158 }