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 02";
0042 
0043 /* forward declarations to avoid warnings */
0044 void *POSIX_Init(void *argument);
0045 
0046 sem_t           sem1;
0047 sem_t           *n_sem1;
0048 
0049 static void benchmark_sem_getvalue(void)
0050 {
0051   benchmark_timer_t end_time;
0052   int  status;
0053   int  value;
0054 
0055   benchmark_timer_initialize();
0056     status = sem_getvalue(&sem1, &value);
0057   end_time = benchmark_timer_read();
0058   rtems_test_assert( status == 0 );
0059 
0060   put_time(
0061     "sem_getvalue: only case",
0062     end_time,
0063     1,        /* Only executed once */
0064     0,
0065     0
0066   );
0067 }
0068 
0069 static void benchmark_sem_wait(void)
0070 {
0071   benchmark_timer_t end_time;
0072   int  status;
0073 
0074   benchmark_timer_initialize();
0075     status = sem_wait(&sem1);
0076   end_time = benchmark_timer_read();
0077   rtems_test_assert( status == 0 );
0078 
0079   put_time(
0080     "sem_wait: available",
0081     end_time,
0082     1,        /* Only executed once */
0083     0,
0084     0
0085   );
0086 }
0087 
0088 static void benchmark_sem_post(void)
0089 {
0090   benchmark_timer_t end_time;
0091   int  status;
0092 
0093   benchmark_timer_initialize();
0094     status = sem_post(&sem1);
0095   end_time = benchmark_timer_read();
0096   rtems_test_assert( status == 0 );
0097 
0098   put_time(
0099     "sem_post: no threads waiting",
0100     end_time,
0101     1,        /* Only executed once */
0102     0,
0103     0
0104   );
0105 }
0106 
0107 static void benchmark_sem_trywait_available(void)
0108 {
0109   benchmark_timer_t end_time;
0110   int  status;
0111 
0112   benchmark_timer_initialize();
0113   status = sem_trywait(&sem1);
0114   end_time = benchmark_timer_read();
0115   rtems_test_assert( status == 0 );
0116 
0117   put_time(
0118     "sem_trywait: available",
0119     end_time,
0120     1,        /* Only executed once */
0121     0,
0122     0
0123   );
0124 }
0125 
0126 static void benchmark_sem_trywait_not_available(void)
0127 {
0128   benchmark_timer_t end_time;
0129   int  status;
0130 
0131   benchmark_timer_initialize();
0132     status = sem_trywait(&sem1);
0133   end_time = benchmark_timer_read();
0134   /*it must be non avalible, so status should be non zero*/
0135   rtems_test_assert( status != 0 );
0136 
0137   put_time(
0138     "sem_trywait: not available",
0139     end_time,
0140     1,        /* Only executed once */
0141     0,
0142     0
0143   );
0144 }
0145 
0146 void *POSIX_Init(void *argument)
0147 {
0148   int status;
0149   TEST_BEGIN();
0150 
0151   /* create the semaphore */
0152   status = sem_init( &sem1, 0, 1 );
0153   rtems_test_assert( status == 0 );
0154 
0155   /* obtain the actual semaphore value */
0156   benchmark_sem_getvalue();
0157   /* lock the semaphore */
0158   benchmark_sem_wait();
0159   /* unlock the semaphore */
0160   benchmark_sem_post();
0161   /* try to lock the semaphore - available */
0162   benchmark_sem_trywait_available();
0163   /* try to lock the semaphore, not available */
0164   benchmark_sem_trywait_not_available();
0165 
0166   TEST_END();
0167 
0168   /*Destroying the semaphore*/
0169   status = sem_destroy(&sem1);
0170   rtems_test_assert( status == 0 );
0171 
0172   rtems_test_exit(0);
0173 }
0174 
0175 /* configuration information */
0176 
0177 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0178 #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
0179 
0180 #define CONFIGURE_MAXIMUM_POSIX_THREADS     1
0181 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0182 
0183 #define CONFIGURE_INIT
0184 #include <rtems/confdefs.h>
0185 /* end of file */