1 /* 2 * Copyright (c) 2010, Ngie Cooper. 3 * 4 * Test that pthread_mutex_getprioceiling() fails because: 5 * 6 * [EINVAL] 7 * The protocol attribute of mutex is PTHREAD_PRIO_NONE. 8 * 9 * by explicitly specifying the protocol as PTHREAD_PRIO_NONE. 10 * 11 * Steps: 12 * 1. Initialize a pthread_mutexattr_t object with pthread_mutexattr_init() 13 * 2. Explicitly set the protocol using PTHREAD_PRIO_NONE. 14 * 3. Call pthread_mutex_getprioceiling() to obtain the prioceiling. 15 * 16 */ 17 18 #include <pthread.h> 19 #include <errno.h> 20 #include <sched.h> 21 #include <stdio.h> 22 #include <string.h> 23 #include <unistd.h> 24 #include "posixtest.h" 25 26 int main(void) 27 { 28 #if defined(_SC_PRIORITY_SCHEDULING) 29 30 if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) { 31 printf("PRIORITY_SCHEDULING not supported\n"); 32 return PTS_UNSUPPORTED; 33 } 34 35 pthread_mutexattr_t mutex_attr; 36 pthread_mutex_t mutex; 37 int error, prioceiling; 38 39 error = pthread_mutexattr_init(&mutex_attr); 40 if (error) { 41 printf("pthread_mutexattr_init failed: %s\n", strerror(error)); 42 return PTS_UNRESOLVED; 43 } 44 45 /* 46 * The default protocol is PTHREAD_PRIO_NONE according to 47 * pthread_mutexattr_getprotocol. 48 */ 49 error = pthread_mutexattr_setprotocol(&mutex_attr, PTHREAD_PRIO_NONE); 50 if (error) { 51 printf("pthread_mutexattr_setprotocol failed: %s\n", 52 strerror(error)); 53 return PTS_UNRESOLVED; 54 } 55 56 /* Initialize a mutex object */ 57 error = pthread_mutex_init(&mutex, &mutex_attr); 58 if (error) { 59 printf("pthread_mutex_init failed: %s\n", strerror(error)); 60 return PTS_UNRESOLVED; 61 } 62 63 /* Get the prioceiling of the mutex. */ 64 error = pthread_mutex_getprioceiling(&mutex, &prioceiling); 65 if (error) { 66 if (error == EINVAL) { 67 printf("pthread_mutex_getprioceiling failed as " 68 "expected\n"); 69 } else { 70 printf("pthread_mutex_getprioceiling did not fail as " 71 "expected: %s\n", strerror(error)); 72 } 73 } else 74 printf("pthread_mutex_getprioceiling passed unexpectedly\n"); 75 76 (void)pthread_mutexattr_destroy(&mutex_attr); 77 (void)pthread_mutex_destroy(&mutex); 78 79 return (error == EINVAL ? PTS_PASS : PTS_FAIL); 80 #else 81 printf("pthread_mutex_getprioceiling not supported"); 82 return PTS_UNSUPPORTED; 83 #endif 84 85 } 86