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 #if !defined(OPERATION_COUNT)
0030 #define OPERATION_COUNT 100
0031 #endif
0032 
0033 #if   defined(USE_WAIT)
0034   #define TEST_NUMBER "08"
0035   #define TEST_CASE "pthread_cond_wait: blocking"
0036 #elif defined(USE_TIMEDWAIT_WITH_VALUE)
0037   #define TEST_NUMBER "09"
0038   #define TEST_CASE "pthread_cond_timedwait: blocking"
0039 #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
0040   #define TEST_NUMBER "10"
0041   #define TEST_CASE "pthread_cond_timedwait: time in past error"
0042 #else
0043   #error "How am I being compiled?"
0044 #endif
0045 
0046 #ifdef HAVE_CONFIG_H
0047 #include "config.h"
0048 #endif
0049 
0050 #include <sys/time.h>
0051 #include <stdio.h>
0052 #include <time.h>
0053 #include <errno.h>
0054 #include <sched.h>
0055 #include <timesys.h>
0056 #include <tmacros.h>
0057 #include <rtems/btimer.h>
0058 #include "test_support.h"
0059 
0060 #include <pthread.h>
0061 
0062 const char rtems_test_name[] = "PSXTMCOND " TEST_NUMBER;
0063 
0064 /* forward declarations to avoid warnings */
0065 void *POSIX_Init(void *argument);
0066 void *Middle(void *argument);
0067 void *Low(void *argument);
0068 
0069 pthread_cond_t  CondID;
0070 pthread_mutex_t MutexID;
0071 struct timespec sleepTime;
0072 
0073 void *Low(
0074   void *argument
0075 )
0076 {
0077   uint32_t end_time;
0078 
0079   end_time = benchmark_timer_read();
0080 
0081   put_time(
0082     TEST_CASE,
0083     end_time,
0084     OPERATION_COUNT,
0085     0,
0086     0
0087   );
0088 
0089   TEST_END();
0090 
0091   rtems_test_exit( 0 );
0092   return NULL;
0093 }
0094 
0095 void *Middle(
0096   void *argument
0097 )
0098 {
0099   int             rc;
0100 
0101 
0102   rc = pthread_mutex_lock(&MutexID);
0103   rtems_test_assert( rc == 0 );
0104 
0105   /* block and switch to another task here */
0106 
0107   #if   defined(USE_WAIT)
0108     rc = pthread_cond_wait( &CondID, &MutexID );
0109     rtems_test_assert( rc == 0 );
0110 
0111   #elif defined(USE_TIMEDWAIT_WITH_VALUE)
0112     /* adjust sleepTime to get something obviously in the future */
0113     ++sleepTime.tv_sec;
0114 
0115     rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
0116     rtems_test_assert( rc == 0 );
0117 
0118   #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
0119     {
0120       /* override sleepTime with something obviously in the past */
0121       sleepTime.tv_sec = 0;
0122       sleepTime.tv_nsec = 5;
0123 
0124       /* this does all the work of timedwait but immediately returns */
0125       rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime );
0126       rtems_test_assert(rc == ETIMEDOUT);
0127       benchmark_timer_read();
0128     }
0129   #endif
0130 
0131   pthread_mutex_unlock(&MutexID);
0132   #if defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST)
0133     /*
0134      * In this case, unlock does not switch to another thread. so we need
0135      * to explicitly yield. If we do not yield, then we will measure the
0136      * time required to do an implicit pthread_exit() which is undesirable
0137      * from a measurement viewpoint.
0138      */
0139     sched_yield();
0140   #endif
0141   return NULL;
0142 }
0143 
0144 void *POSIX_Init(
0145   void *argument
0146 )
0147 {
0148   int             i;
0149   int             status;
0150   pthread_t       threadId;
0151   int             rc;
0152   struct timeval  tp;
0153 
0154   TEST_BEGIN();
0155 
0156   rc =  gettimeofday(&tp, NULL);
0157   rtems_test_assert( rc == 0 );
0158 
0159   /* Convert from timeval to timespec */
0160   sleepTime.tv_sec  = tp.tv_sec;
0161   sleepTime.tv_nsec = tp.tv_usec * 1000;
0162 
0163   rc = pthread_cond_init(&CondID, NULL);
0164   rtems_test_assert( rc == 0 );
0165 
0166   rc = pthread_mutex_init(&MutexID, NULL);
0167   rtems_test_assert( rc == 0 );
0168 
0169   rc = pthread_mutex_lock(&MutexID);
0170   rtems_test_assert( rc == 0 );
0171 
0172   for ( i=0 ; i < OPERATION_COUNT - 1 ; i++ ) {
0173     status = pthread_create( &threadId, NULL, Middle, NULL );
0174     rtems_test_assert( !status );
0175   }
0176 
0177   status = pthread_create( &threadId, NULL, Low, NULL );
0178   rtems_test_assert( !status );
0179 
0180   /* start the timer and switch through all the other tasks */
0181   benchmark_timer_initialize();
0182 
0183   rc = pthread_mutex_unlock(&MutexID);
0184   rtems_test_assert( rc == 0 );
0185 
0186   /* Should never return. */
0187   return NULL;
0188 }
0189 
0190 /* configuration information */
0191 
0192 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0193 #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER
0194 
0195 #define CONFIGURE_MAXIMUM_POSIX_THREADS     OPERATION_COUNT + 2
0196 #define CONFIGURE_POSIX_INIT_THREAD_TABLE
0197 
0198 #define CONFIGURE_INIT
0199 
0200 #include <rtems/confdefs.h>
0201   /* end of file */