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 <timesys.h>
0035 #include <rtems/btimer.h>
0036 #include <pthread.h>
0037 #include "test_support.h"
0038 
0039 const char rtems_test_name[] = "PSXTMRWLOCK 01";
0040 
0041 /* forward declarations to avoid warnings */
0042 void *POSIX_Init(void *argument);
0043 
0044 pthread_rwlock_t     rwlock;
0045 
0046 static void benchmark_pthread_rwlock_init(void)
0047 {
0048   benchmark_timer_t end_time;
0049   int  status;
0050   pthread_rwlockattr_t attr;
0051 
0052   pthread_rwlockattr_init( &attr );
0053   benchmark_timer_initialize();
0054     status = pthread_rwlock_init( &rwlock, &attr );
0055   end_time = benchmark_timer_read();
0056   rtems_test_assert( status == 0 );
0057 
0058   put_time(
0059     "pthread_rwlock_init: only case",
0060     end_time,
0061     1,        /* Only executed once */
0062     0,
0063     0
0064   );
0065 
0066 }
0067 
0068 static void benchmark_pthread_rwlock_rdlock(void)
0069 {
0070   benchmark_timer_t end_time;
0071   int  status;
0072 
0073   benchmark_timer_initialize();
0074     status = pthread_rwlock_rdlock(&rwlock);
0075   end_time = benchmark_timer_read();
0076   rtems_test_assert( status == 0 );
0077 
0078   put_time(
0079     "pthread_rwlock_rdlock: available",
0080     end_time,
0081     1,        /* Only executed once */
0082     0,
0083     0
0084   );
0085 
0086 }
0087 
0088 static void benchmark_pthread_rwlock_unlock(int print)
0089 {
0090   benchmark_timer_t end_time;
0091   int  status;
0092 
0093   benchmark_timer_initialize();
0094     status = pthread_rwlock_unlock(&rwlock);
0095   end_time = benchmark_timer_read();
0096   rtems_test_assert( status == 0 );
0097   if ( print == 1 ){
0098     put_time(
0099       "pthread_rwlock_unlock: available",
0100       end_time,
0101       1,        /* Only executed once */
0102       0,
0103       0
0104     );
0105   }
0106 }
0107 
0108 static void benchmark_pthread_rwlock_tryrdlock(void)
0109 {
0110   benchmark_timer_t end_time;
0111   int  status;
0112 
0113   benchmark_timer_initialize();
0114     status = pthread_rwlock_tryrdlock(&rwlock);
0115   end_time = benchmark_timer_read();
0116   rtems_test_assert( status == 0 || status == EBUSY );
0117   if (status == EBUSY) {
0118     put_time(
0119       "pthread_rwlock_tryrdlock: not available",
0120       end_time,
0121       1,        /* Only executed once */
0122       0,
0123       0
0124     );
0125   } else if (status == 0) {
0126     put_time(
0127       "pthread_rwlock_tryrdlock: available",
0128       end_time,
0129       1,        /* Only executed once */
0130       0,
0131       0
0132     );
0133   }
0134 }
0135 
0136 static void benchmark_pthread_rwlock_timedrdlock(void)
0137 {
0138   benchmark_timer_t end_time;
0139   int  status;
0140 
0141   benchmark_timer_initialize();
0142     status = pthread_rwlock_timedrdlock(&rwlock, 0);
0143   end_time = benchmark_timer_read();
0144   rtems_test_assert( status == 0 );
0145 
0146   put_time(
0147     "pthread_rwlock_timedrdlock: available",
0148     end_time,
0149     1,        /* Only executed once */
0150     0,
0151     0
0152   );
0153 
0154 }
0155 
0156 static void benchmark_pthread_rwlock_wrlock(void)
0157 {
0158   benchmark_timer_t end_time;
0159   int  status;
0160 
0161   benchmark_timer_initialize();
0162     status = pthread_rwlock_wrlock(&rwlock);
0163   end_time = benchmark_timer_read();
0164   rtems_test_assert( status == 0 );
0165 
0166   put_time(
0167     "pthread_rwlock_wrlock: available",
0168     end_time,
0169     1,        /* Only executed once */
0170     0,
0171     0
0172   );
0173 
0174 }
0175 
0176 static void benchmark_pthread_rwlock_trywrlock(void)
0177 {
0178   benchmark_timer_t end_time;
0179   int  status;
0180 
0181   benchmark_timer_initialize();
0182     status = pthread_rwlock_trywrlock(&rwlock);
0183   end_time = benchmark_timer_read();
0184 
0185   rtems_test_assert( status == 0 || status == EBUSY );
0186   if ( status == EBUSY ) {
0187     put_time(
0188       "pthread_rwlock_trywrlock: not available ",
0189       end_time,
0190       1,        /* Only executed once */
0191       0,
0192       0
0193     );
0194   } else if ( status == 0 ) {
0195     put_time(
0196       "pthread_rwlock_trywrlock: available",
0197       end_time,
0198       1,        /* Only executed once */
0199       0,
0200       0
0201     );
0202   }
0203 }
0204 
0205 static void benchmark_pthread_rwlock_timedwrlock(void)
0206 {
0207   benchmark_timer_t end_time;
0208   int  status;
0209 
0210   benchmark_timer_initialize();
0211     status = pthread_rwlock_timedwrlock(&rwlock,0);
0212   end_time = benchmark_timer_read();
0213   rtems_test_assert( status == 0 );
0214 
0215   put_time(
0216     "pthread_rwlock_timedwrlock: available",
0217     end_time,
0218     1,        /* Only executed once */
0219     0,
0220     0
0221   );
0222 }
0223 
0224 static void benchmark_pthread_rwlock_destroy(void)
0225 {
0226   benchmark_timer_t end_time;
0227   int  status;
0228 
0229   benchmark_timer_initialize();
0230     status = pthread_rwlock_destroy(&rwlock);
0231   end_time = benchmark_timer_read();
0232   rtems_test_assert( status == 0 );
0233 
0234   put_time(
0235     "pthread_rwlock_destroy: only case",
0236     end_time,
0237     1,        /* Only executed once */
0238     0,
0239     0
0240   );
0241 }
0242 
0243 void *POSIX_Init(
0244   void *argument
0245 )
0246 {
0247 
0248   TEST_BEGIN();
0249 
0250   /* initializing rwlock */
0251   benchmark_pthread_rwlock_init();
0252   /* applying a read lock */
0253   benchmark_pthread_rwlock_rdlock();
0254   /* unlocking rwlock */
0255   benchmark_pthread_rwlock_unlock(0);
0256   /* trying to apply a read lock when it is available*/
0257   benchmark_pthread_rwlock_tryrdlock();
0258   /* unlocking rwlock */
0259   benchmark_pthread_rwlock_unlock(0);
0260   /* applying a timed read lock */
0261   benchmark_pthread_rwlock_timedrdlock();
0262   /* unlocking rwlock */
0263   benchmark_pthread_rwlock_unlock(0);
0264   /* applying a write lock */
0265   benchmark_pthread_rwlock_wrlock();
0266   /* trying to get read lock, when is not available*/
0267   benchmark_pthread_rwlock_tryrdlock();
0268   /* unlocking rwlock */
0269   benchmark_pthread_rwlock_unlock(0);
0270   /* trying to apply a write lock, when it is available*/
0271   benchmark_pthread_rwlock_trywrlock();
0272   /* trying to get write lock, when it is not available*/
0273   benchmark_pthread_rwlock_trywrlock();
0274   /* unlocking rwlock */
0275   benchmark_pthread_rwlock_unlock(0);
0276   /* applying a timed write lock */
0277   benchmark_pthread_rwlock_timedwrlock();
0278   /* unlocking rwlock */
0279   benchmark_pthread_rwlock_unlock(1);
0280   /* destroying rwlock */
0281   benchmark_pthread_rwlock_destroy();
0282 
0283   TEST_END();
0284 
0285   rtems_test_exit(0);
0286 }
0287 
0288 /* configuration information */
0289 
0290 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0291 #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
0292 
0293 #define CONFIGURE_MAXIMUM_POSIX_THREADS     1
0294 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0295 
0296 #define CONFIGURE_INIT
0297 
0298 #include <rtems/confdefs.h>
0299 /* end of file */