[a87d1b1] | 1 | /* |
---|
[50162e0] | 2 | * COPYRIGHT (c) 1989-2013. |
---|
[a87d1b1] | 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. |
---|
[a87d1b1] | 8 | */ |
---|
| 9 | |
---|
| 10 | #ifdef HAVE_CONFIG_H |
---|
| 11 | #include "config.h" |
---|
| 12 | #endif |
---|
| 13 | |
---|
| 14 | #include <coverhd.h> |
---|
| 15 | #include <tmacros.h> |
---|
| 16 | #include <timesys.h> |
---|
| 17 | #include "test_support.h" |
---|
| 18 | #include <pthread.h> |
---|
| 19 | #include <sched.h> |
---|
[8fbe2e6] | 20 | #include <rtems/btimer.h> |
---|
[a87d1b1] | 21 | |
---|
[fd46711] | 22 | const char rtems_test_name[] = "PSXTMMUTEX 06"; |
---|
| 23 | |
---|
[991a3cc1] | 24 | /* forward declarations to avoid warnings */ |
---|
| 25 | void *POSIX_Init(void *argument); |
---|
| 26 | void *Middle(void *argument); |
---|
| 27 | void *Low(void *argument); |
---|
| 28 | |
---|
[a87d1b1] | 29 | pthread_mutex_t MutexId; |
---|
| 30 | |
---|
| 31 | void *Low( |
---|
| 32 | void *argument |
---|
| 33 | ) |
---|
| 34 | { |
---|
| 35 | int status; |
---|
[add2977] | 36 | benchmark_timer_t end_time; |
---|
[a87d1b1] | 37 | |
---|
| 38 | status = pthread_mutex_lock( &MutexId ); |
---|
| 39 | end_time = benchmark_timer_read(); |
---|
| 40 | |
---|
| 41 | rtems_test_assert( status == 0 ); |
---|
| 42 | |
---|
| 43 | put_time( |
---|
[50162e0] | 44 | "pthread_mutex_unlock: unblocking preempt", |
---|
[a87d1b1] | 45 | end_time, |
---|
| 46 | OPERATION_COUNT, |
---|
| 47 | 0, |
---|
| 48 | 0 |
---|
| 49 | ); |
---|
| 50 | |
---|
[fd46711] | 51 | TEST_END(); |
---|
[a87d1b1] | 52 | rtems_test_exit( 0 ); |
---|
| 53 | return NULL; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void *Middle( |
---|
| 57 | void *argument |
---|
| 58 | ) |
---|
| 59 | { |
---|
| 60 | int status; |
---|
| 61 | |
---|
| 62 | status = pthread_mutex_lock( &MutexId ); |
---|
| 63 | rtems_test_assert( status == 0 ); |
---|
| 64 | |
---|
| 65 | /* thread switch occurs */ |
---|
| 66 | |
---|
| 67 | status = pthread_mutex_unlock( &MutexId ); |
---|
| 68 | rtems_test_assert( status == 0 ); |
---|
| 69 | |
---|
| 70 | /* thread switch occurs */ |
---|
| 71 | |
---|
| 72 | return NULL; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void *POSIX_Init( |
---|
| 76 | void *argument |
---|
| 77 | ) |
---|
| 78 | { |
---|
| 79 | int i; |
---|
| 80 | int status; |
---|
| 81 | pthread_t threadId; |
---|
| 82 | pthread_attr_t attr; |
---|
| 83 | struct sched_param param; |
---|
| 84 | |
---|
[fd46711] | 85 | TEST_BEGIN(); |
---|
[a87d1b1] | 86 | |
---|
| 87 | /* |
---|
| 88 | * Deliberately create the mutex BEFORE the threads. This way the |
---|
| 89 | * threads should preempt this thread and block as they are created. |
---|
| 90 | */ |
---|
| 91 | status = pthread_mutex_init( &MutexId, NULL ); |
---|
| 92 | rtems_test_assert( status == 0 ); |
---|
| 93 | |
---|
| 94 | /* |
---|
| 95 | * Lock the mutex so the threads will block. |
---|
| 96 | */ |
---|
| 97 | status = pthread_mutex_lock( &MutexId ); |
---|
| 98 | rtems_test_assert( status == 0 ); |
---|
| 99 | |
---|
| 100 | /* |
---|
| 101 | * Now lower our priority |
---|
| 102 | */ |
---|
| 103 | status = pthread_attr_init( &attr ); |
---|
| 104 | rtems_test_assert( status == 0 ); |
---|
| 105 | |
---|
| 106 | status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED ); |
---|
| 107 | rtems_test_assert( status == 0 ); |
---|
| 108 | |
---|
| 109 | status = pthread_attr_setschedpolicy( &attr, SCHED_RR ); |
---|
| 110 | rtems_test_assert( status == 0 ); |
---|
| 111 | |
---|
| 112 | param.sched_priority = 2; |
---|
| 113 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
| 114 | rtems_test_assert( status == 0 ); |
---|
| 115 | |
---|
| 116 | /* |
---|
| 117 | * And create rest of threads as more important than we are. They |
---|
| 118 | * will preempt us as they are created and block. |
---|
| 119 | */ |
---|
| 120 | for ( i=0 ; i < OPERATION_COUNT ; i++ ) { |
---|
| 121 | |
---|
| 122 | param.sched_priority = 3 + i; |
---|
| 123 | status = pthread_attr_setschedparam( &attr, ¶m ); |
---|
| 124 | rtems_test_assert( status == 0 ); |
---|
| 125 | |
---|
| 126 | status = pthread_create( |
---|
| 127 | &threadId, |
---|
| 128 | &attr, |
---|
| 129 | (i == OPERATION_COUNT - 1) ? Low : Middle, |
---|
| 130 | NULL |
---|
| 131 | ); |
---|
| 132 | rtems_test_assert( status == 0 ); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /* |
---|
| 136 | * Now start the timer which will be stopped in Low |
---|
| 137 | */ |
---|
| 138 | benchmark_timer_initialize(); |
---|
| 139 | |
---|
| 140 | status = pthread_mutex_unlock( &MutexId ); |
---|
| 141 | rtems_test_assert( status == 0 ); |
---|
| 142 | |
---|
| 143 | /* thread switch occurs */ |
---|
| 144 | return NULL; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /* configuration information */ |
---|
| 148 | |
---|
| 149 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
| 150 | #define CONFIGURE_APPLICATION_NEEDS_TIMER_DRIVER |
---|
| 151 | |
---|
| 152 | #define CONFIGURE_MAXIMUM_POSIX_THREADS OPERATION_COUNT + 2 |
---|
| 153 | #define CONFIGURE_MAXIMUM_POSIX_MUTEXES 1 |
---|
| 154 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
| 155 | |
---|
| 156 | #define CONFIGURE_INIT |
---|
| 157 | |
---|
| 158 | #include <rtems/confdefs.h> |
---|
| 159 | /* end of file */ |
---|