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 <pwd.h> 21 #include <sched.h> 22 #include <stdio.h> 23 #include <string.h> 24 #include <unistd.h> 25 #include "posixtest.h" 26 27 int main(void) 28 { 29 #if defined(_SC_PRIORITY_SCHEDULING) 30 31 if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) { 32 printf("PRIORITY_SCHEDULING not supported\n"); 33 return PTS_UNSUPPORTED; 34 } 35 36 pthread_mutexattr_t mutex_attr; 37 pthread_mutex_t mutex; 38 int error, prioceiling; 39 40 error = pthread_mutexattr_init(&mutex_attr); 41 if (error) { 42 printf("pthread_mutexattr_init failed: %s\n", strerror(error)); 43 return PTS_UNRESOLVED; 44 } 45 46 /* 47 * The default protocol is PTHREAD_PRIO_NONE according to 48 * pthread_mutexattr_getprotocol. 49 */ 50 error = pthread_mutexattr_setprotocol(&mutex_attr, 51 PTHREAD_PRIO_INHERIT); 52 if (error) { 53 printf("pthread_mutexattr_setprotocol failed: %s\n", 54 strerror(error)); 55 return PTS_UNRESOLVED; 56 } 57 58 /* Initialize a mutex object */ 59 error = pthread_mutex_init(&mutex, &mutex_attr); 60 if (error) { 61 printf("pthread_mutex_init failed: %s\n", strerror(error)); 62 return PTS_UNRESOLVED; 63 } 64 65 /* Get the prioceiling of the mutex. */ 66 error = pthread_mutex_getprioceiling(&mutex, &prioceiling); 67 if (error) { 68 if (error == EINVAL) { 69 printf("pthread_mutex_getprioceiling failed as " 70 "expected\n"); 71 } else { 72 printf("pthread_mutex_getprioceiling did not fail as " 73 "expected: %s\n", strerror(error)); 74 } 75 } else 76 printf("pthread_mutex_getprioceiling passed unexpectedly\n"); 77 78 (void)pthread_mutexattr_destroy(&mutex_attr); 79 (void)pthread_mutex_destroy(&mutex); 80 81 return (error == EINVAL ? PTS_PASS : PTS_UNRESOLVED); 82 #else 83 printf("pthread_mutex_getprioceiling not supported"); 84 return PTS_UNSUPPORTED; 85 #endif 86 87 } 88