[1b4f2b30] | 1 | /* |
---|
[2e7e636f] | 2 | * COPYRIGHT (c) 1989-2009. |
---|
[7e811af1] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
[98e4ebf5] | 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
[3c48599] | 7 | * http://www.rtems.com/license/LICENSE. |
---|
[7e811af1] | 8 | * |
---|
| 9 | * $Id$ |
---|
| 10 | */ |
---|
| 11 | |
---|
[98843fb8] | 12 | #include <sched.h> |
---|
| 13 | |
---|
[7e811af1] | 14 | #define CONFIGURE_INIT |
---|
| 15 | #include "system.h" |
---|
| 16 | #include <errno.h> |
---|
| 17 | |
---|
[2e7e636f] | 18 | void print_schedparam( |
---|
| 19 | char *prefix, |
---|
| 20 | struct sched_param *schedparam |
---|
| 21 | ); |
---|
| 22 | |
---|
[5abf63d] | 23 | int HIGH_PRIORITY; |
---|
| 24 | int MEDIUM_PRIORITY; |
---|
| 25 | int LOW_PRIORITY; |
---|
| 26 | |
---|
[7e811af1] | 27 | void print_schedparam( |
---|
| 28 | char *prefix, |
---|
| 29 | struct sched_param *schedparam |
---|
| 30 | ) |
---|
| 31 | { |
---|
| 32 | printf( "%ssched priority = %d\n", prefix, schedparam->sched_priority ); |
---|
| 33 | #if defined(_POSIX_SPORADIC_SERVER) |
---|
| 34 | printf( "%sss_low_priority = %d\n", prefix, schedparam->ss_low_priority ); |
---|
| 35 | printf( "%sss_replenish_period = (%ld, %ld)\n", prefix, |
---|
| 36 | schedparam->ss_replenish_period.tv_sec, |
---|
| 37 | schedparam->ss_replenish_period.tv_nsec ); |
---|
| 38 | printf( "%sss_initial_budget = (%ld, %ld)\n", prefix, |
---|
| 39 | schedparam->ss_initial_budget.tv_sec, |
---|
| 40 | schedparam->ss_initial_budget.tv_nsec ); |
---|
| 41 | #else |
---|
| 42 | printf( "%s_POSIX_SPORADIC_SERVER is not defined\n" ); |
---|
| 43 | #endif |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void *POSIX_Init( |
---|
| 47 | void *argument |
---|
| 48 | ) |
---|
| 49 | { |
---|
| 50 | int status; |
---|
[84b03316] | 51 | int passes; |
---|
[7e811af1] | 52 | int schedpolicy; |
---|
| 53 | int priority; |
---|
| 54 | struct sched_param schedparam; |
---|
| 55 | char buffer[ 80 ]; |
---|
| 56 | pthread_mutexattr_t attr; |
---|
[c45ca10] | 57 | time_t start; |
---|
| 58 | time_t now; |
---|
[7e811af1] | 59 | |
---|
| 60 | puts( "\n\n*** POSIX TEST 9 ***" ); |
---|
| 61 | |
---|
| 62 | /* set the time of day, and print our buffer in multiple ways */ |
---|
| 63 | |
---|
| 64 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
| 65 | |
---|
| 66 | /* get id of this thread */ |
---|
| 67 | |
---|
| 68 | Init_id = pthread_self(); |
---|
[39615f4] | 69 | printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id ); |
---|
[1b4f2b30] | 70 | |
---|
[7e811af1] | 71 | /* try to use this thread as a sporadic server */ |
---|
| 72 | |
---|
| 73 | puts( "Init: pthread_getschedparam - SUCCESSFUL" ); |
---|
| 74 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 75 | assert( !status ); |
---|
| 76 | |
---|
| 77 | priority = schedparam.sched_priority; |
---|
| 78 | sprintf( buffer, " - current priority = %d", priority ); |
---|
| 79 | print_current_time( "Init: ", buffer ); |
---|
| 80 | |
---|
| 81 | schedparam.ss_replenish_period.tv_sec = 0; |
---|
| 82 | schedparam.ss_replenish_period.tv_nsec = 500000000; /* 1/2 second */ |
---|
| 83 | schedparam.ss_initial_budget.tv_sec = 0; |
---|
| 84 | schedparam.ss_initial_budget.tv_nsec = 250000000; /* 1/4 second */ |
---|
| 85 | |
---|
[5abf63d] | 86 | schedparam.sched_priority = sched_get_priority_max(SCHED_SPORADIC); |
---|
| 87 | schedparam.ss_low_priority = sched_get_priority_max(SCHED_SPORADIC) - 2; |
---|
[7e811af1] | 88 | |
---|
| 89 | puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" ); |
---|
| 90 | status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam ); |
---|
| 91 | assert( !status ); |
---|
| 92 | |
---|
| 93 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 94 | assert( !status ); |
---|
| 95 | |
---|
| 96 | priority = schedparam.sched_priority; |
---|
| 97 | sprintf( buffer, " - new priority = %d", priority ); |
---|
| 98 | print_current_time( "Init: ", buffer ); |
---|
| 99 | |
---|
| 100 | /* go into a loop consuming CPU time to watch our priority change */ |
---|
| 101 | |
---|
| 102 | for ( passes=0 ; passes <= 3 ; ) { |
---|
| 103 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 104 | assert( !status ); |
---|
[1b4f2b30] | 105 | |
---|
[7e811af1] | 106 | if ( priority != schedparam.sched_priority ) { |
---|
| 107 | priority = schedparam.sched_priority; |
---|
| 108 | sprintf( buffer, " - new priority = %d", priority ); |
---|
| 109 | print_current_time( "Init: ", buffer ); |
---|
| 110 | passes++; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /* now see if this works if we are holding a priority ceiling mutex */ |
---|
| 115 | |
---|
| 116 | empty_line(); |
---|
| 117 | |
---|
| 118 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 119 | assert( !status ); |
---|
| 120 | |
---|
| 121 | schedparam.ss_replenish_period.tv_sec = 0; |
---|
| 122 | schedparam.ss_replenish_period.tv_nsec = 500000000; /* 1/2 second */ |
---|
| 123 | schedparam.ss_initial_budget.tv_sec = 0; |
---|
| 124 | schedparam.ss_initial_budget.tv_nsec = 250000000; /* 1/4 second */ |
---|
| 125 | |
---|
[5abf63d] | 126 | HIGH_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ); |
---|
| 127 | MEDIUM_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ) - 2; |
---|
| 128 | LOW_PRIORITY = sched_get_priority_max( SCHED_SPORADIC ) - 4; |
---|
[7e811af1] | 129 | |
---|
| 130 | schedparam.sched_priority = HIGH_PRIORITY; |
---|
| 131 | schedparam.ss_low_priority = LOW_PRIORITY; |
---|
[1b4f2b30] | 132 | |
---|
[7e811af1] | 133 | puts( "Init: pthread_setschedparam - SUCCESSFUL (sporadic server)" ); |
---|
| 134 | status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam ); |
---|
| 135 | assert( !status ); |
---|
| 136 | |
---|
| 137 | puts( "Init: Initializing mutex attributes for priority ceiling" ); |
---|
| 138 | status = pthread_mutexattr_init( &attr ); |
---|
| 139 | assert( !status ); |
---|
| 140 | |
---|
[c45ca10] | 141 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
[7e811af1] | 142 | assert( !status ); |
---|
[1b4f2b30] | 143 | |
---|
[7e811af1] | 144 | puts( "Init: Creating a mutex" ); |
---|
| 145 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
| 146 | if ( status ) |
---|
| 147 | printf( "status = %d\n", status ); |
---|
| 148 | assert( !status ); |
---|
| 149 | |
---|
| 150 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 151 | assert( !status ); |
---|
[1b4f2b30] | 152 | |
---|
[7e811af1] | 153 | priority = schedparam.sched_priority; |
---|
| 154 | sprintf( buffer, " - new priority = %d", priority ); |
---|
| 155 | print_current_time( "Init: ", buffer ); |
---|
| 156 | |
---|
[c45ca10] | 157 | /* go into a loop consuming CPU time to watch our priority NOT lower */ |
---|
| 158 | |
---|
| 159 | start = time( &start ); |
---|
| 160 | |
---|
| 161 | puts( "Init: pthread_mutex_lock acquire the lock" ); |
---|
| 162 | status = pthread_mutex_lock( &Mutex_id ); |
---|
| 163 | if ( status ) |
---|
| 164 | printf( "status = %d %s\n", status, strerror(status) ); |
---|
| 165 | assert( !status ); |
---|
[7e811af1] | 166 | |
---|
| 167 | for ( ; ; ) { |
---|
| 168 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 169 | assert( !status ); |
---|
| 170 | |
---|
[c45ca10] | 171 | if ( schedparam.sched_priority == LOW_PRIORITY ) { |
---|
| 172 | puts( "ERROR - Init's priority lowered while holding mutex" ); |
---|
| 173 | rtems_test_exit(0); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | now = time( &now ); |
---|
| 177 | if ( now - start > 3 ) |
---|
| 178 | break; |
---|
[7e811af1] | 179 | |
---|
| 180 | priority = schedparam.sched_priority; |
---|
| 181 | sprintf( buffer, " - new priority = %d", priority ); |
---|
| 182 | print_current_time( "Init: ", buffer ); |
---|
| 183 | |
---|
| 184 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 185 | assert( !status ); |
---|
| 186 | |
---|
| 187 | priority = schedparam.sched_priority; |
---|
| 188 | sprintf( buffer, " - new priority = %d", priority ); |
---|
| 189 | print_current_time( "Init: ", buffer ); |
---|
| 190 | |
---|
| 191 | break; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /* with this unlock we should be able to go to low priority */ |
---|
| 195 | |
---|
[1b4f2b30] | 196 | puts( "Init: unlock mutex" ); |
---|
[7e811af1] | 197 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
| 198 | if ( status ) |
---|
| 199 | printf( "status = %d\n", status ); |
---|
| 200 | assert( !status ); |
---|
| 201 | |
---|
| 202 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 203 | assert( !status ); |
---|
| 204 | |
---|
| 205 | priority = schedparam.sched_priority; |
---|
| 206 | sprintf( buffer, " - new priority = %d", priority ); |
---|
| 207 | print_current_time( "Init: ", buffer ); |
---|
| 208 | |
---|
| 209 | for ( ; ; ) { |
---|
| 210 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 211 | assert( !status ); |
---|
[1b4f2b30] | 212 | |
---|
[7e811af1] | 213 | if ( schedparam.sched_priority == LOW_PRIORITY ) |
---|
| 214 | break; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam ); |
---|
| 218 | assert( !status ); |
---|
[1b4f2b30] | 219 | |
---|
[7e811af1] | 220 | priority = schedparam.sched_priority; |
---|
| 221 | sprintf( buffer, " - new priority = %d", priority ); |
---|
| 222 | print_current_time( "Init: ", buffer ); |
---|
| 223 | |
---|
| 224 | puts( "*** END OF POSIX TEST 9 ***" ); |
---|
[d802489] | 225 | rtems_test_exit( 0 ); |
---|
[7e811af1] | 226 | |
---|
| 227 | return NULL; /* just so the compiler thinks we returned something */ |
---|
| 228 | } |
---|