[c6d15c2] | 1 | /* |
---|
[60b791ad] | 2 | * COPYRIGHT (c) 1989-1998. |
---|
[c6d15c2] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
[03f2154e] | 4 | * Copyright assigned to U.S. Government, 1994. |
---|
[c6d15c2] | 5 | * |
---|
[98e4ebf5] | 6 | * The license and distribution terms for this file may be |
---|
| 7 | * found in the file LICENSE in this distribution or at |
---|
[03f2154e] | 8 | * http://www.OARcorp.com/rtems/license.html. |
---|
[c6d15c2] | 9 | * |
---|
| 10 | * $Id$ |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | #define CONFIGURE_INIT |
---|
| 14 | #include "system.h" |
---|
| 15 | #include <errno.h> |
---|
| 16 | |
---|
[51f5d64c] | 17 | #define MUTEX_BAD_ID 0xfffffffe |
---|
[d06e9ca] | 18 | |
---|
[c6d15c2] | 19 | void Print_mutexattr( |
---|
| 20 | char *msg, |
---|
| 21 | pthread_mutexattr_t *attr |
---|
| 22 | ) |
---|
| 23 | { |
---|
| 24 | int status; |
---|
| 25 | int protocol; |
---|
| 26 | int prioceiling; |
---|
| 27 | int pshared; |
---|
| 28 | |
---|
| 29 | /* protocol */ |
---|
| 30 | |
---|
| 31 | status = pthread_mutexattr_getprotocol( attr, &protocol ); |
---|
| 32 | assert( !status ); |
---|
| 33 | |
---|
| 34 | printf( "%smutex protocol is (%d) -- ", msg, protocol ); |
---|
| 35 | switch ( protocol ) { |
---|
| 36 | case PTHREAD_PRIO_NONE: |
---|
[9f1a034e] | 37 | puts( "PTHREAD_PRIO_NONE" ); |
---|
[c6d15c2] | 38 | break; |
---|
| 39 | case PTHREAD_PRIO_INHERIT: |
---|
[9f1a034e] | 40 | puts( "PTHREAD_PRIO_INHERIT" ); |
---|
[c6d15c2] | 41 | break; |
---|
| 42 | case PTHREAD_PRIO_PROTECT: |
---|
[9f1a034e] | 43 | puts( "PTHREAD_PRIO_PROTECT" ); |
---|
[c6d15c2] | 44 | break; |
---|
| 45 | default: |
---|
[9f1a034e] | 46 | puts( "UNKNOWN" ); |
---|
[c6d15c2] | 47 | assert( 0 ); |
---|
| 48 | break; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /* priority ceiling */ |
---|
| 52 | |
---|
| 53 | status = pthread_mutexattr_getprioceiling( attr, &prioceiling ); |
---|
| 54 | assert( !status ); |
---|
| 55 | printf( "%smutex priority ceiling is %d\n", msg, prioceiling ); |
---|
| 56 | |
---|
| 57 | /* process shared */ |
---|
| 58 | |
---|
| 59 | status = pthread_mutexattr_getpshared( attr, &pshared ); |
---|
| 60 | assert( !status ); |
---|
| 61 | printf( "%smutex process shared is (%d) -- ", msg, pshared ); |
---|
| 62 | switch ( pshared ) { |
---|
| 63 | case PTHREAD_PROCESS_PRIVATE: |
---|
[9f1a034e] | 64 | puts( "PTHREAD_PROCESS_PRIVATE" ); |
---|
[c6d15c2] | 65 | break; |
---|
| 66 | case PTHREAD_PROCESS_SHARED: |
---|
[9f1a034e] | 67 | puts( "PTHREAD_PROCESS_SHARED" ); |
---|
[c6d15c2] | 68 | break; |
---|
| 69 | default: |
---|
[9f1a034e] | 70 | puts( "UNKNOWN" ); |
---|
[c6d15c2] | 71 | assert( 0 ); |
---|
| 72 | break; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | void *POSIX_Init( |
---|
| 77 | void *argument |
---|
| 78 | ) |
---|
| 79 | { |
---|
| 80 | int status; |
---|
| 81 | pthread_mutexattr_t attr; |
---|
[13adea0] | 82 | pthread_mutexattr_t destroyed_attr; |
---|
[ce78b894] | 83 | struct timespec times; |
---|
[9f1a034e] | 84 | struct sched_param param; |
---|
[da2e539] | 85 | int pshared; |
---|
[9f1a034e] | 86 | int policy; |
---|
[d06e9ca] | 87 | int protocol; |
---|
[9f1a034e] | 88 | int ceiling; |
---|
| 89 | int old_ceiling; |
---|
[c6d15c2] | 90 | |
---|
[d06e9ca] | 91 | assert( MUTEX_BAD_ID != PTHREAD_MUTEX_INITIALIZER ); |
---|
| 92 | Mutex_bad_id = MUTEX_BAD_ID; |
---|
| 93 | |
---|
[c6d15c2] | 94 | puts( "\n\n*** POSIX TEST 5 ***" ); |
---|
| 95 | |
---|
| 96 | /* set the time of day, and print our buffer in multiple ways */ |
---|
| 97 | |
---|
| 98 | set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
| 99 | |
---|
| 100 | /* get id of this thread */ |
---|
| 101 | |
---|
| 102 | Init_id = pthread_self(); |
---|
| 103 | printf( "Init's ID is 0x%08x\n", Init_id ); |
---|
| 104 | |
---|
[13adea0] | 105 | /* tes pthread_mutex_attr_init */ |
---|
[c6d15c2] | 106 | |
---|
[c03aeaf] | 107 | puts( "Init: pthread_mutexattr_init - EINVAL (NULL attr)" ); |
---|
| 108 | status = pthread_mutexattr_init( NULL ); |
---|
| 109 | assert( status == EINVAL ); |
---|
| 110 | |
---|
[76117f2b] | 111 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
[c6d15c2] | 112 | status = pthread_mutexattr_init( &attr ); |
---|
| 113 | assert( !status ); |
---|
| 114 | |
---|
| 115 | Print_mutexattr( "Init: ", &attr ); |
---|
| 116 | |
---|
[13adea0] | 117 | /* create an "uninitialized" attribute structure */ |
---|
| 118 | |
---|
| 119 | status = pthread_mutexattr_init( &destroyed_attr ); |
---|
| 120 | assert( !status ); |
---|
| 121 | |
---|
| 122 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
| 123 | status = pthread_mutexattr_destroy( &destroyed_attr ); |
---|
| 124 | assert( !status ); |
---|
| 125 | |
---|
| 126 | puts( "Init: pthread_mutexattr_destroy - EINVAL (NULL attr)" ); |
---|
| 127 | status = pthread_mutexattr_destroy( NULL ); |
---|
| 128 | assert( status == EINVAL ); |
---|
| 129 | |
---|
| 130 | puts( "Init: pthread_mutexattr_destroy - EINVAL (not initialized)" ); |
---|
| 131 | status = pthread_mutexattr_destroy( &destroyed_attr ); |
---|
| 132 | assert( status == EINVAL ); |
---|
| 133 | |
---|
[d06e9ca] | 134 | /* error cases for set and get pshared attribute */ |
---|
| 135 | |
---|
| 136 | empty_line(); |
---|
[da2e539] | 137 | |
---|
| 138 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL attr)" ); |
---|
| 139 | status = pthread_mutexattr_getpshared( NULL, &pshared ); |
---|
| 140 | assert( status == EINVAL ); |
---|
| 141 | |
---|
[d06e9ca] | 142 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (NULL pshared)" ); |
---|
[da2e539] | 143 | status = pthread_mutexattr_getpshared( &attr, NULL ); |
---|
| 144 | assert( status == EINVAL ); |
---|
| 145 | |
---|
| 146 | puts( "Init: pthread_mutexattr_getpshared - EINVAL (not initialized)" ); |
---|
| 147 | status = pthread_mutexattr_getpshared( &destroyed_attr, &pshared ); |
---|
| 148 | assert( status == EINVAL ); |
---|
| 149 | |
---|
[d06e9ca] | 150 | pshared = PTHREAD_PROCESS_PRIVATE; |
---|
| 151 | puts( "Init: pthread_mutexattr_setpshared - EINVAL (NULL attr)" ); |
---|
| 152 | status = pthread_mutexattr_setpshared( NULL, pshared ); |
---|
| 153 | assert( status == EINVAL ); |
---|
| 154 | |
---|
| 155 | pshared = PTHREAD_PROCESS_PRIVATE; |
---|
| 156 | puts( "Init: pthread_mutexattr_setpshared - EINVAL (not initialized)" ); |
---|
| 157 | status = pthread_mutexattr_setpshared( &destroyed_attr, pshared ); |
---|
| 158 | assert( status == EINVAL ); |
---|
| 159 | |
---|
| 160 | /* error cases for set and get protocol attribute */ |
---|
| 161 | |
---|
| 162 | empty_line(); |
---|
| 163 | |
---|
| 164 | puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL attr)" ); |
---|
| 165 | status = pthread_mutexattr_getprotocol( NULL, &protocol ); |
---|
| 166 | assert( status == EINVAL ); |
---|
| 167 | |
---|
| 168 | puts( "Init: pthread_mutexattr_getprotocol - EINVAL (NULL protocol)" ); |
---|
| 169 | status = pthread_mutexattr_getprotocol( &attr, NULL ); |
---|
| 170 | assert( status == EINVAL ); |
---|
| 171 | |
---|
| 172 | puts( "Init: pthread_mutexattr_getprotocol - EINVAL (not initialized)" ); |
---|
| 173 | status = pthread_mutexattr_getprotocol( &destroyed_attr, &protocol ); |
---|
| 174 | assert( status == EINVAL ); |
---|
| 175 | |
---|
| 176 | puts( "Init: pthread_mutexattr_setprotocol - EINVAL (NULL attr)" ); |
---|
| 177 | status = pthread_mutexattr_setprotocol( NULL, PTHREAD_PRIO_NONE ); |
---|
| 178 | assert( status == EINVAL ); |
---|
| 179 | |
---|
| 180 | puts( "Init: pthread_mutexattr_setprotocol - EINVAL (invalid protocol)" ); |
---|
| 181 | status = pthread_mutexattr_setprotocol( &attr, -1 ); |
---|
| 182 | assert( status == EINVAL ); |
---|
| 183 | |
---|
| 184 | puts( "Init: pthread_mutexattr_setprotocol - EINVAL (not initialized)" ); |
---|
| 185 | status = pthread_mutexattr_setprotocol( &destroyed_attr, -1 ); |
---|
| 186 | assert( status == EINVAL ); |
---|
| 187 | |
---|
| 188 | /* error cases for set and get prioceiling attribute */ |
---|
| 189 | |
---|
| 190 | empty_line(); |
---|
| 191 | |
---|
| 192 | puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL attr)" ); |
---|
| 193 | status = pthread_mutexattr_getprioceiling( NULL, &ceiling ); |
---|
| 194 | assert( status == EINVAL ); |
---|
| 195 | |
---|
| 196 | puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (NULL prioceiling)" ); |
---|
| 197 | status = pthread_mutexattr_getprioceiling( &attr, NULL ); |
---|
| 198 | assert( status == EINVAL ); |
---|
| 199 | |
---|
| 200 | puts( "Init: pthread_mutexattr_getprioceiling - EINVAL (not initialized)" ); |
---|
| 201 | status = pthread_mutexattr_getprioceiling( &destroyed_attr, &ceiling ); |
---|
| 202 | assert( status == EINVAL ); |
---|
| 203 | |
---|
| 204 | puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (NULL attr)" ); |
---|
| 205 | status = pthread_mutexattr_setprioceiling( NULL, 128 ); |
---|
| 206 | assert( status == EINVAL ); |
---|
| 207 | |
---|
| 208 | puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (invalid priority)" ); |
---|
| 209 | status = pthread_mutexattr_setprioceiling( &attr, 512 ); |
---|
| 210 | if ( status != EINVAL ) |
---|
| 211 | printf( "status = %d\n", status ); |
---|
| 212 | assert( status == EINVAL ); |
---|
| 213 | |
---|
| 214 | puts( "Init: pthread_mutexattr_setprioceiling - EINVAL (not initialized)" ); |
---|
| 215 | status = pthread_mutexattr_setprioceiling( &destroyed_attr, -1 ); |
---|
| 216 | assert( status == EINVAL ); |
---|
| 217 | |
---|
| 218 | /* create a thread */ |
---|
| 219 | |
---|
| 220 | status = pthread_create( &Task_id, NULL, Task_1, NULL ); |
---|
| 221 | assert( !status ); |
---|
| 222 | |
---|
| 223 | /* now try some basic mutex operations */ |
---|
| 224 | |
---|
| 225 | empty_line(); |
---|
| 226 | |
---|
[256a462] | 227 | puts( "Init: pthread_mutex_init - EINVAL (NULL mutex_id)" ); |
---|
| 228 | status = pthread_mutex_init( NULL, &attr ); |
---|
| 229 | assert( status == EINVAL ); |
---|
| 230 | |
---|
[d06e9ca] | 231 | puts( "Init: pthread_mutex_init - EINVAL (not initialized attr)" ); |
---|
| 232 | status = pthread_mutex_init( &Mutex_id, &destroyed_attr ); |
---|
| 233 | assert( status == EINVAL ); |
---|
| 234 | |
---|
| 235 | /* must get around error checks in attribute set routines */ |
---|
| 236 | attr.protocol = -1; |
---|
| 237 | |
---|
| 238 | puts( "Init: pthread_mutex_init - EINVAL (bad protocol)" ); |
---|
| 239 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
| 240 | assert( status == EINVAL ); |
---|
| 241 | |
---|
| 242 | /* must get around error checks in attribute set routines */ |
---|
| 243 | attr.protocol = PTHREAD_PRIO_INHERIT; |
---|
| 244 | attr.prio_ceiling = -1; |
---|
| 245 | |
---|
| 246 | puts( "Init: pthread_mutex_init - EINVAL (bad priority ceiling)" ); |
---|
| 247 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
| 248 | assert( status == EINVAL ); |
---|
| 249 | |
---|
| 250 | /* now set up for a success pthread_mutex_init */ |
---|
| 251 | |
---|
| 252 | puts( "Init: Resetting mutex attributes" ); |
---|
| 253 | status = pthread_mutexattr_init( &attr ); |
---|
| 254 | assert( !status ); |
---|
[13adea0] | 255 | |
---|
[9f1a034e] | 256 | puts( "Init: Changing mutex attributes" ); |
---|
[c6d15c2] | 257 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
| 258 | assert( !status ); |
---|
| 259 | |
---|
| 260 | status = pthread_mutexattr_setprioceiling( &attr, 128 ); |
---|
| 261 | assert( !status ); |
---|
| 262 | |
---|
| 263 | status = pthread_mutexattr_setpshared( &attr, PTHREAD_PROCESS_SHARED ); |
---|
| 264 | assert( !status ); |
---|
| 265 | |
---|
| 266 | Print_mutexattr( "Init: ", &attr ); |
---|
[d06e9ca] | 267 | |
---|
[9f1a034e] | 268 | puts( "Init: Resetting mutex attributes" ); |
---|
[c6d15c2] | 269 | status = pthread_mutexattr_init( &attr ); |
---|
| 270 | assert( !status ); |
---|
| 271 | |
---|
[76117f2b] | 272 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
[c6d15c2] | 273 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
| 274 | if ( status ) |
---|
| 275 | printf( "status = %d\n", status ); |
---|
| 276 | assert( !status ); |
---|
| 277 | |
---|
[256a462] | 278 | puts( "Init: pthread_mutex_init - EBUSY (attempt to initialize an existing mutex)" ); |
---|
| 279 | status = pthread_mutex_init( &Mutex_id, &attr ); |
---|
| 280 | if ( !status ) |
---|
| 281 | printf( "status = %d\n", status ); |
---|
| 282 | assert( status == EBUSY ); |
---|
| 283 | |
---|
[d06e9ca] | 284 | puts( "Init: pthread_mutex_trylock - EINVAL (illegal ID)" ); |
---|
| 285 | status = pthread_mutex_trylock( &Mutex_bad_id ); |
---|
| 286 | if ( status != EINVAL ) |
---|
| 287 | printf( "status = %d\n", status ); |
---|
| 288 | assert( status == EINVAL ); |
---|
| 289 | |
---|
[76117f2b] | 290 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
[c6d15c2] | 291 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
| 292 | if ( status ) |
---|
| 293 | printf( "status = %d\n", status ); |
---|
| 294 | assert( !status ); |
---|
| 295 | |
---|
[76117f2b] | 296 | puts( "Init: pthread_mutex_trylock - EDEADLK (already locked)" ); |
---|
[c6d15c2] | 297 | status = pthread_mutex_trylock( &Mutex_id ); |
---|
| 298 | if ( status != EDEADLK ) |
---|
| 299 | printf( "status = %d\n", status ); |
---|
| 300 | assert( status == EDEADLK ); |
---|
| 301 | |
---|
[76117f2b] | 302 | puts( "Init: pthread_mutex_lock - EDEADLK (already locked)" ); |
---|
[c6d15c2] | 303 | status = pthread_mutex_lock( &Mutex_id ); |
---|
| 304 | if ( status != EDEADLK ) |
---|
| 305 | printf( "status = %d\n", status ); |
---|
| 306 | assert( status == EDEADLK ); |
---|
| 307 | |
---|
[9f1a034e] | 308 | puts( "Init: Sleep 1 second" ); |
---|
[c6d15c2] | 309 | |
---|
| 310 | sleep( 1 ); |
---|
| 311 | |
---|
| 312 | /* switch to task 1 */ |
---|
| 313 | |
---|
[d06e9ca] | 314 | puts( "Init: pthread_mutex_unlock - EINVAL (invalid id)" ); |
---|
| 315 | status = pthread_mutex_unlock( &Mutex_bad_id ); |
---|
| 316 | if ( status != EINVAL ) |
---|
| 317 | printf( "status = %d\n", status ); |
---|
| 318 | assert( status == EINVAL ); |
---|
| 319 | |
---|
[76117f2b] | 320 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
[c6d15c2] | 321 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
| 322 | if ( status ) |
---|
| 323 | printf( "status = %d\n", status ); |
---|
| 324 | assert( !status ); |
---|
| 325 | |
---|
[76117f2b] | 326 | puts( "Init: pthread_mutex_unlock - EPERM (not owner)" ); |
---|
[c6d15c2] | 327 | status = pthread_mutex_unlock( &Mutex_id ); |
---|
| 328 | if ( status != EPERM ) |
---|
| 329 | printf( "status = %d\n", status ); |
---|
| 330 | assert( status == EPERM ); |
---|
| 331 | |
---|
[ce78b894] | 332 | times.tv_sec = 0; |
---|
| 333 | times.tv_nsec = 500000000; |
---|
[76117f2b] | 334 | puts( "Init: pthread_mutex_timedlock - time out in 1/2 second" ); |
---|
[ce78b894] | 335 | status = pthread_mutex_timedlock( &Mutex_id, × ); |
---|
| 336 | if ( status != EAGAIN ) |
---|
| 337 | printf( "status = %d\n", status ); |
---|
| 338 | assert( status == EAGAIN ); |
---|
| 339 | |
---|
| 340 | /* switch to idle */ |
---|
| 341 | |
---|
[76117f2b] | 342 | puts( "Init: pthread_mutex_timedlock - EAGAIN (timeout)" ); |
---|
[9f1a034e] | 343 | |
---|
| 344 | /* destroy a mutex */ |
---|
| 345 | |
---|
| 346 | empty_line(); |
---|
| 347 | |
---|
[76117f2b] | 348 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
[9f1a034e] | 349 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
| 350 | if ( status ) |
---|
| 351 | printf( "status = %d\n", status ); |
---|
| 352 | assert( !status ); |
---|
| 353 | |
---|
[fb7d080] | 354 | puts( "Init: pthread_mutex_init - EAGAIN (too many)" ); |
---|
[d06e9ca] | 355 | status = pthread_mutex_init( &Mutex3_id, &attr ); |
---|
[fb7d080] | 356 | assert( status == EAGAIN ); |
---|
[d06e9ca] | 357 | |
---|
[76117f2b] | 358 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
[9f1a034e] | 359 | status = pthread_mutexattr_destroy( &attr ); |
---|
| 360 | assert( !status ); |
---|
| 361 | |
---|
[76117f2b] | 362 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
[9f1a034e] | 363 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
| 364 | assert( !status ); |
---|
| 365 | |
---|
[d06e9ca] | 366 | puts( "Init: pthread_mutex_destroy - EINVAL (invalid id)" ); |
---|
| 367 | status = pthread_mutex_destroy( &Mutex_bad_id ); |
---|
| 368 | assert( status == EINVAL ); |
---|
| 369 | |
---|
[9f1a034e] | 370 | /* destroy a busy mutex */ |
---|
| 371 | |
---|
| 372 | empty_line(); |
---|
| 373 | |
---|
[76117f2b] | 374 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
[9f1a034e] | 375 | status = pthread_mutexattr_init( &attr ); |
---|
| 376 | assert( !status ); |
---|
| 377 | |
---|
[76117f2b] | 378 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
[9f1a034e] | 379 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
| 380 | assert( !status ); |
---|
| 381 | |
---|
[76117f2b] | 382 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
[9f1a034e] | 383 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
| 384 | if ( status ) |
---|
| 385 | printf( "status = %d\n", status ); |
---|
| 386 | assert( !status ); |
---|
| 387 | |
---|
[76117f2b] | 388 | puts( "Init: pthread_mutex_destroy - EBUSY (already locked)" ); |
---|
[9f1a034e] | 389 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
| 390 | if ( status != EBUSY ) |
---|
| 391 | printf( "status = %d\n", status ); |
---|
| 392 | assert( status == EBUSY ); |
---|
| 393 | |
---|
[76117f2b] | 394 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
[9f1a034e] | 395 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
| 396 | assert( !status ); |
---|
| 397 | |
---|
[76117f2b] | 398 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
[9f1a034e] | 399 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
| 400 | assert( !status ); |
---|
| 401 | |
---|
| 402 | /* priority inherit mutex */ |
---|
| 403 | |
---|
| 404 | empty_line(); |
---|
| 405 | |
---|
[76117f2b] | 406 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
[9f1a034e] | 407 | status = pthread_mutexattr_init( &attr ); |
---|
| 408 | assert( !status ); |
---|
| 409 | |
---|
[76117f2b] | 410 | puts( |
---|
| 411 | "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_INHERIT)" |
---|
| 412 | ); |
---|
[9f1a034e] | 413 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT ); |
---|
| 414 | assert( !status ); |
---|
| 415 | |
---|
[76117f2b] | 416 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
[9f1a034e] | 417 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
| 418 | assert( !status ); |
---|
| 419 | |
---|
[76117f2b] | 420 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
[9f1a034e] | 421 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
| 422 | assert( !status ); |
---|
| 423 | |
---|
| 424 | /* create a thread at a lower priority */ |
---|
| 425 | |
---|
| 426 | status = pthread_create( &Task2_id, NULL, Task_2, NULL ); |
---|
| 427 | assert( !status ); |
---|
| 428 | |
---|
| 429 | /* set priority of Task2 to highest priority */ |
---|
| 430 | |
---|
[1d61610] | 431 | param.sched_priority = 254; |
---|
[9f1a034e] | 432 | |
---|
[76117f2b] | 433 | puts( "Init: pthread_setschedparam - Setting Task2 priority to highest" ); |
---|
[9f1a034e] | 434 | status = pthread_setschedparam( Task2_id, SCHED_FIFO, ¶m ); |
---|
| 435 | assert( !status ); |
---|
| 436 | |
---|
| 437 | /* switching to Task2 */ |
---|
| 438 | |
---|
| 439 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
| 440 | assert( !status ); |
---|
[76117f2b] | 441 | printf( "Init: pthread_getschedparam - priority = %d\n", param.sched_priority); |
---|
[9f1a034e] | 442 | |
---|
[76117f2b] | 443 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
[9f1a034e] | 444 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
| 445 | assert( !status ); |
---|
| 446 | |
---|
[76117f2b] | 447 | puts( "Init: pthread_mutexattr_destroy - SUCCESSFUL" ); |
---|
[9f1a034e] | 448 | status = pthread_mutexattr_destroy( &attr ); |
---|
| 449 | assert( !status ); |
---|
| 450 | |
---|
[76117f2b] | 451 | puts( "Init: pthread_mutex_destroy - SUCCESSFUL" ); |
---|
[9f1a034e] | 452 | status = pthread_mutex_destroy( &Mutex2_id ); |
---|
| 453 | assert( !status ); |
---|
| 454 | |
---|
| 455 | /* priority ceiling mutex */ |
---|
| 456 | |
---|
| 457 | empty_line(); |
---|
| 458 | |
---|
[76117f2b] | 459 | puts( "Init: pthread_mutexattr_init - SUCCESSFUL" ); |
---|
[9f1a034e] | 460 | status = pthread_mutexattr_init( &attr ); |
---|
| 461 | assert( !status ); |
---|
| 462 | |
---|
[76117f2b] | 463 | puts( |
---|
| 464 | "Init: pthread_mutexattr_setprotocol - SUCCESSFUL (PTHREAD_PRIO_PROTECT)" |
---|
| 465 | ); |
---|
[9f1a034e] | 466 | status = pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_PROTECT ); |
---|
| 467 | assert( !status ); |
---|
| 468 | |
---|
[76117f2b] | 469 | puts( "Init: pthread_mutex_init - SUCCESSFUL" ); |
---|
[9f1a034e] | 470 | status = pthread_mutex_init( &Mutex2_id, &attr ); |
---|
| 471 | assert( !status ); |
---|
| 472 | |
---|
[d06e9ca] | 473 | puts( "Init: pthread_mutex_getprioceiling - EINVAL (invalid id)" ); |
---|
| 474 | status = pthread_mutex_getprioceiling( &Mutex_bad_id, &ceiling ); |
---|
| 475 | assert( status == EINVAL ); |
---|
| 476 | |
---|
| 477 | puts( "Init: pthread_mutex_getprioceiling - EINVAL (NULL ceiling)" ); |
---|
| 478 | status = pthread_mutex_getprioceiling( &Mutex2_id, NULL ); |
---|
| 479 | assert( status == EINVAL ); |
---|
| 480 | |
---|
[9f1a034e] | 481 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
| 482 | assert( !status ); |
---|
[76117f2b] | 483 | printf( "Init: pthread_mutex_getprioceiling - %d\n", ceiling ); |
---|
[9f1a034e] | 484 | |
---|
[d06e9ca] | 485 | puts( "Init: pthread_mutex_setprioceiling - EINVAL (invalid id)" ); |
---|
| 486 | status = pthread_mutex_setprioceiling( &Mutex_bad_id, 200, &old_ceiling ); |
---|
| 487 | assert( status == EINVAL ); |
---|
| 488 | |
---|
| 489 | puts( "Init: pthread_mutex_setprioceiling - EINVAL (illegal priority)" ); |
---|
| 490 | status = pthread_mutex_setprioceiling( &Mutex2_id, 512, &old_ceiling ); |
---|
| 491 | assert( status == EINVAL ); |
---|
| 492 | |
---|
| 493 | puts( "Init: pthread_mutex_setprioceiling - EINVAL (NULL ceiling)" ); |
---|
| 494 | status = pthread_mutex_setprioceiling( &Mutex2_id, 128, NULL ); |
---|
| 495 | assert( status == EINVAL ); |
---|
| 496 | |
---|
| 497 | /* normal cases of set priority ceiling */ |
---|
| 498 | |
---|
[76117f2b] | 499 | puts( "Init: pthread_mutex_setprioceiling - new ceiling = 200" ); |
---|
[9f1a034e] | 500 | status = pthread_mutex_setprioceiling( &Mutex2_id, 200, &old_ceiling ); |
---|
| 501 | assert( !status ); |
---|
[76117f2b] | 502 | printf( |
---|
| 503 | "Init: pthread_mutex_setprioceiling - old ceiling = %d\n",old_ceiling |
---|
| 504 | ); |
---|
[9f1a034e] | 505 | |
---|
| 506 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
| 507 | assert( !status ); |
---|
[76117f2b] | 508 | printf( |
---|
| 509 | "Init: pthread_getschedparam - priority = %d\n", param.sched_priority |
---|
| 510 | ); |
---|
[9f1a034e] | 511 | |
---|
[76117f2b] | 512 | puts( "Init: pthread_mutex_trylock - SUCCESSFUL" ); |
---|
[9f1a034e] | 513 | status = pthread_mutex_trylock( &Mutex2_id ); |
---|
| 514 | assert( !status ); |
---|
| 515 | |
---|
| 516 | status = pthread_getschedparam( pthread_self(), &policy, ¶m ); |
---|
| 517 | assert( !status ); |
---|
[76117f2b] | 518 | printf( |
---|
| 519 | "Init: pthread_getschedparam - priority = %d\n", param.sched_priority |
---|
| 520 | ); |
---|
[9f1a034e] | 521 | |
---|
| 522 | /* create a thread at a higher priority */ |
---|
| 523 | |
---|
| 524 | status = pthread_create( &Task3_id, NULL, Task_3, NULL ); |
---|
| 525 | assert( !status ); |
---|
| 526 | |
---|
| 527 | /* set priority of Task3 to highest priority */ |
---|
| 528 | |
---|
| 529 | param.sched_priority = 199; |
---|
| 530 | |
---|
| 531 | status = pthread_setschedparam( Task3_id, SCHED_FIFO, ¶m ); |
---|
| 532 | assert( !status ); |
---|
[76117f2b] | 533 | puts( "Init: pthread_setschedparam - set Task3 priority to highest" ); |
---|
[9f1a034e] | 534 | |
---|
| 535 | /* DOES NOT SWITCH to Task3 */ |
---|
| 536 | |
---|
| 537 | puts( "Init: Sleep 1 second" ); |
---|
| 538 | assert( !status ); |
---|
| 539 | sleep( 1 ); |
---|
| 540 | |
---|
| 541 | /* switch to task 3 */ |
---|
| 542 | |
---|
[76117f2b] | 543 | puts( "Init: pthread_mutex_unlock - SUCCESSFUL" ); |
---|
[9f1a034e] | 544 | status = pthread_mutex_unlock( &Mutex2_id ); |
---|
| 545 | assert( !status ); |
---|
| 546 | |
---|
| 547 | status = pthread_mutex_getprioceiling( &Mutex2_id, &ceiling ); |
---|
| 548 | assert( !status ); |
---|
[76117f2b] | 549 | printf( "Init: pthread_mutex_getprioceiling- ceiling = %d\n", ceiling ); |
---|
[9f1a034e] | 550 | |
---|
| 551 | /* set priority of Init to highest priority */ |
---|
| 552 | |
---|
[1d61610] | 553 | param.sched_priority = 254; |
---|
[9f1a034e] | 554 | |
---|
| 555 | status = pthread_setschedparam( Init_id, SCHED_FIFO, ¶m ); |
---|
| 556 | assert( !status ); |
---|
[76117f2b] | 557 | puts( "Init: pthread_setschedparam - set Init priority to highest" ); |
---|
[9f1a034e] | 558 | |
---|
[76117f2b] | 559 | puts( "Init: pthread_mutex_lock - EINVAL (priority ceiling violation)" ); |
---|
[9f1a034e] | 560 | status = pthread_mutex_lock( &Mutex2_id ); |
---|
[da2e539] | 561 | if ( status != EINVAL ) |
---|
[9f1a034e] | 562 | printf( "status = %d\n", status ); |
---|
| 563 | assert( status == EINVAL ); |
---|
[ce78b894] | 564 | |
---|
[c6d15c2] | 565 | puts( "*** END OF POSIX TEST 5 ***" ); |
---|
| 566 | exit( 0 ); |
---|
| 567 | |
---|
| 568 | return NULL; /* just so the compiler thinks we returned something */ |
---|
| 569 | } |
---|