Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2013.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <errno.h>
0034 #include <fcntl.h>
0035 #include <semaphore.h>
0036 #include <tmacros.h>
0037 #include <timesys.h>
0038 #include <rtems/btimer.h>
0039 #include "test_support.h"
0040 
0041 const char rtems_test_name[] = "PSXTMSEM 01";
0042 
0043 /* forward declarations to avoid warnings */
0044 void *POSIX_Init(void *argument);
0045 
0046 #define MAX_SEMS  2
0047 
0048 sem_t           sem1;
0049 sem_t           *n_sem1;
0050 sem_t           *n_sem2;
0051 
0052 static void benchmark_sem_init(void)
0053 {
0054   benchmark_timer_t end_time;
0055   int  status;
0056 
0057   benchmark_timer_initialize();
0058     status = sem_init( &sem1, 0, 1 );
0059   end_time = benchmark_timer_read();
0060   rtems_test_assert( status == 0 );
0061 
0062   put_time(
0063     "sem_init: only case",
0064     end_time,
0065     1,        /* Only executed once */
0066     0,
0067     0
0068   );
0069 }
0070 
0071 static void benchmark_sem_destroy(void)
0072 {
0073   benchmark_timer_t end_time;
0074   int  status;
0075 
0076   benchmark_timer_initialize();
0077     status = sem_destroy( &sem1 );
0078   end_time = benchmark_timer_read();
0079   rtems_test_assert( status == 0 );
0080 
0081   put_time(
0082     "sem_destroy: only case",
0083     end_time,
0084     1,        /* Only executed once */
0085     0,
0086     0
0087   );
0088 }
0089 
0090 static void benchmark_sem_open(bool report_time)
0091 {
0092   benchmark_timer_t end_time;
0093 
0094   benchmark_timer_initialize();
0095     n_sem1 = sem_open( "sem1", O_CREAT, 0777, 1 );
0096   end_time = benchmark_timer_read();
0097 
0098   if ( report_time ) {
0099     put_time(
0100       "sem_open: first open O_CREAT",
0101       end_time,
0102       1,        /* Only executed once */
0103       0,
0104       0
0105     );
0106   }
0107 }
0108 
0109 static void benchmark_sem_close(bool report_time)
0110 {
0111   benchmark_timer_t end_time;
0112   int  status;
0113 
0114   benchmark_timer_initialize();
0115     status = sem_close( n_sem1 );
0116   end_time = benchmark_timer_read();
0117   rtems_test_assert( status == 0 );
0118 
0119   if ( report_time ) {
0120     put_time(
0121       "sem_close: named first/nested close",
0122       end_time,
0123       1,        /* Only executed once */
0124       0,
0125       0
0126     );
0127   }
0128 }
0129 
0130 static void benchmark_sem_unlink(const char *message)
0131 {
0132   benchmark_timer_t end_time;
0133   int  status;
0134 
0135   benchmark_timer_initialize();
0136     status = sem_unlink( "sem1" );
0137   end_time = benchmark_timer_read();
0138   rtems_test_assert( status == 0 );
0139 
0140   put_time(
0141     message,
0142     end_time,
0143     1,        /* Only executed once */
0144     0,
0145     0
0146   );
0147 }
0148 
0149 static void benchmark_sem_open_second(void)
0150 {
0151   benchmark_timer_t end_time;
0152 
0153   benchmark_timer_initialize();
0154     n_sem2 = sem_open( "sem1", O_EXCL, 0777, 1 );
0155   end_time = benchmark_timer_read();
0156 
0157   put_time(
0158     "sem_open: second open O_EXCL",
0159     end_time,
0160     1,        /* Only executed once */
0161     0,
0162     0
0163   );
0164 }
0165 
0166 static void benchmark_sem_close_second(void)
0167 {
0168   benchmark_timer_t end_time;
0169   int  status;
0170 
0171   benchmark_timer_initialize();
0172     status = sem_close( n_sem2 );
0173   end_time = benchmark_timer_read();
0174   rtems_test_assert( status == 0 );
0175 
0176   put_time(
0177     "sem_close: named second close",
0178     end_time,
0179     1,        /* Only executed once */
0180     0,
0181     0
0182   );
0183 }
0184 
0185 void *POSIX_Init(void *argument)
0186 {
0187 
0188   TEST_BEGIN();
0189 
0190   /* creating unnamed semaphore */
0191   benchmark_sem_init();
0192   /* destroying unnamed semaphore */
0193   benchmark_sem_destroy();
0194 
0195   /* opening named semaphore first time o_flag = O_CREAT */
0196   benchmark_sem_open(true);
0197   /* opening named semaphore second time o_flag = O_EXCL */
0198   benchmark_sem_open_second();
0199   /* close named semaphore first time  -- does not delete */
0200   benchmark_sem_close(true);
0201   /* unlink named semaphore -- does not delete */
0202   benchmark_sem_unlink("sem_unlink: does not delete");
0203   /*  close semaphore the second time, this actually deletes it */
0204   benchmark_sem_close_second();
0205 
0206   /* recrate named semaphore first time o_flag = O_CREAT */
0207   benchmark_sem_open(false);
0208   benchmark_sem_close(false);
0209   benchmark_sem_unlink("sem_unlink: deletes semaphore");
0210 
0211   TEST_END();
0212 
0213   rtems_test_exit(0);
0214 }
0215 
0216 /* configuration information */
0217 
0218 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0219 #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
0220 
0221 #define CONFIGURE_MAXIMUM_POSIX_THREADS     1
0222 #define CONFIGURE_MAXIMUM_POSIX_SEMAPHORES  MAX_SEMS
0223 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0224 
0225 #define CONFIGURE_INIT
0226 
0227 #include <rtems/confdefs.h>
0228 /* end of file */