[72f3b05] | 1 | /* |
---|
[50162e0] | 2 | * COPYRIGHT (c) 1989-2013. |
---|
[72f3b05] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 7 | * http://www.rtems.org/license/LICENSE. |
---|
[72f3b05] | 8 | */ |
---|
| 9 | |
---|
| 10 | #if defined(USE_WAIT) |
---|
| 11 | #define TEST_NUMBER "08" |
---|
[50162e0] | 12 | #define TEST_CASE "pthread_cond_wait: blocking" |
---|
[72f3b05] | 13 | #elif defined(USE_TIMEDWAIT_WITH_VALUE) |
---|
| 14 | #define TEST_NUMBER "09" |
---|
[50162e0] | 15 | #define TEST_CASE "pthread_cond_timedwait: blocking" |
---|
[72f3b05] | 16 | #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST) |
---|
| 17 | #define TEST_NUMBER "10" |
---|
[50162e0] | 18 | #define TEST_CASE "pthread_cond_timedwait: time in past error" |
---|
[72f3b05] | 19 | #else |
---|
| 20 | #error "How am I being compiled?" |
---|
| 21 | #endif |
---|
| 22 | |
---|
| 23 | #ifdef HAVE_CONFIG_H |
---|
| 24 | #include "config.h" |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | #include <stdio.h> |
---|
| 28 | #include <time.h> |
---|
| 29 | #include <errno.h> |
---|
[7b369af] | 30 | #include <sched.h> |
---|
[72f3b05] | 31 | #include <timesys.h> |
---|
| 32 | #include <tmacros.h> |
---|
| 33 | #include <rtems/timerdrv.h> |
---|
| 34 | #include "test_support.h" |
---|
| 35 | |
---|
| 36 | #include <pthread.h> |
---|
| 37 | |
---|
[fd46711] | 38 | const char rtems_test_name[] = "PSXTMCOND 08"; |
---|
| 39 | |
---|
[72f3b05] | 40 | /* forward declarations to avoid warnings */ |
---|
| 41 | void *POSIX_Init(void *argument); |
---|
| 42 | void *Middle(void *argument); |
---|
| 43 | void *Low(void *argument); |
---|
| 44 | |
---|
| 45 | pthread_cond_t CondID; |
---|
| 46 | pthread_mutex_t MutexID; |
---|
| 47 | struct timespec sleepTime; |
---|
| 48 | |
---|
| 49 | void *Low( |
---|
| 50 | void *argument |
---|
| 51 | ) |
---|
| 52 | { |
---|
[de549c3] | 53 | uint32_t end_time; |
---|
[72f3b05] | 54 | |
---|
| 55 | end_time = benchmark_timer_read(); |
---|
| 56 | |
---|
| 57 | put_time( |
---|
| 58 | TEST_CASE, |
---|
| 59 | end_time, |
---|
| 60 | OPERATION_COUNT, |
---|
| 61 | 0, |
---|
| 62 | 0 |
---|
| 63 | ); |
---|
| 64 | |
---|
[fd46711] | 65 | TEST_END(); |
---|
[72f3b05] | 66 | |
---|
| 67 | rtems_test_exit( 0 ); |
---|
| 68 | return NULL; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void *Middle( |
---|
| 72 | void *argument |
---|
| 73 | ) |
---|
| 74 | { |
---|
| 75 | int rc; |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | rc = pthread_mutex_lock(&MutexID); |
---|
| 79 | rtems_test_assert( rc == 0 ); |
---|
| 80 | |
---|
| 81 | /* block and switch to another task here */ |
---|
| 82 | |
---|
| 83 | #if defined(USE_WAIT) |
---|
| 84 | rc = pthread_cond_wait( &CondID, &MutexID ); |
---|
| 85 | rtems_test_assert( rc == 0 ); |
---|
| 86 | |
---|
| 87 | #elif defined(USE_TIMEDWAIT_WITH_VALUE) |
---|
| 88 | |
---|
| 89 | rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime ); |
---|
| 90 | rtems_test_assert( rc == 0 ); |
---|
| 91 | |
---|
| 92 | #elif defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST) |
---|
| 93 | { |
---|
| 94 | /* override sleepTime with something obviously in the past */ |
---|
| 95 | sleepTime.tv_sec = 0; |
---|
| 96 | sleepTime.tv_nsec = 5; |
---|
| 97 | |
---|
| 98 | /* this does all the work of timedwait but immediately returns */ |
---|
| 99 | rc = pthread_cond_timedwait( &CondID, &MutexID, &sleepTime ); |
---|
| 100 | rtems_test_assert(rc == ETIMEDOUT); |
---|
[7b369af] | 101 | benchmark_timer_read(); |
---|
[72f3b05] | 102 | } |
---|
| 103 | #endif |
---|
| 104 | |
---|
| 105 | pthread_mutex_unlock(&MutexID); |
---|
| 106 | #if defined(USE_TIMEDWAIT_WAIT_VALUE_IN_PAST) |
---|
| 107 | /* |
---|
| 108 | * In this case, unlock does not switch to another thread. so we need |
---|
| 109 | * to explicitly yield. If we do not yield, then we will measure the |
---|
| 110 | * time required to do an implicit pthread_exit() which is undesirable |
---|
| 111 | * from a measurement viewpoint. |
---|
| 112 | */ |
---|
| 113 | sched_yield(); |
---|
| 114 | #endif |
---|
| 115 | return NULL; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | void *POSIX_Init( |
---|
| 119 | void *argument |
---|
| 120 | ) |
---|
| 121 | { |
---|
| 122 | int i; |
---|
| 123 | int status; |
---|
| 124 | pthread_t threadId; |
---|
| 125 | int rc; |
---|
| 126 | struct timeval tp; |
---|
| 127 | |
---|
[fd46711] | 128 | TEST_BEGIN(); |
---|
[72f3b05] | 129 | |
---|
| 130 | rc = gettimeofday(&tp, NULL); |
---|
| 131 | rtems_test_assert( rc == 0 ); |
---|
| 132 | |
---|
| 133 | /* Convert from timeval to timespec */ |
---|
| 134 | sleepTime.tv_sec = tp.tv_sec; |
---|
| 135 | sleepTime.tv_nsec = tp.tv_usec * 1000; |
---|
| 136 | sleepTime.tv_nsec += 1; |
---|
| 137 | |
---|
| 138 | rc = pthread_cond_init(&CondID, NULL); |
---|
| 139 | rtems_test_assert( rc == 0 ); |
---|
| 140 | |
---|
| 141 | rc = pthread_mutex_init(&MutexID, NULL); |
---|
| 142 | rtems_test_assert( rc == 0 ); |
---|
| 143 | |
---|
| 144 | rc = pthread_mutex_lock(&MutexID); |
---|
| 145 | rtems_test_assert( rc == 0 ); |
---|
| 146 | |
---|
| 147 | for ( i=0 ; i < OPERATION_COUNT - 1 ; i++ ) { |
---|
| 148 | status = pthread_create( &threadId, NULL, Middle, NULL ); |
---|
| 149 | rtems_test_assert( !status ); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | status = pthread_create( &threadId, NULL, Low, NULL ); |
---|
| 153 | rtems_test_assert( !status ); |
---|
| 154 | |
---|
| 155 | /* start the timer and switch through all the other tasks */ |
---|
| 156 | benchmark_timer_initialize(); |
---|
| 157 | |
---|
| 158 | rc = pthread_mutex_unlock(&MutexID); |
---|
| 159 | rtems_test_assert( rc == 0 ); |
---|
| 160 | |
---|
| 161 | /* Should never return. */ |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /* configuration information */ |
---|
| 166 | |
---|
| 167 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
| 168 | #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER |
---|
| 169 | |
---|
| 170 | #define CONFIGURE_MAXIMUM_POSIX_THREADS OPERATION_COUNT + 2 |
---|
| 171 | #define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2 |
---|
| 172 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
| 173 | #define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 2 |
---|
| 174 | #define CONFIGURE_MAXIMUM_POSIX_MUTEXES 2 |
---|
| 175 | |
---|
| 176 | #define CONFIGURE_INIT |
---|
| 177 | |
---|
| 178 | #include <rtems/confdefs.h> |
---|
| 179 | /* end of file */ |
---|