1/*
2 * Copyright (c) 2010, Ngie Cooper.
3 *
4 * Test that pthread_mutex_setprioceiling()
5 *
6 * returns the current prioceiling of the mutex.
7 *
8 * Steps:
9 * 1.  Initialize a pthread_mutexattr_t object with pthread_mutexattr_init()
10 * 2.  Call pthread_mutex_getprioceiling() to obtain the prioceiling.
11 *
12 */
13
14#include <pthread.h>
15#include <errno.h>
16#include <sched.h>
17#include <stdio.h>
18#include <string.h>
19#include <unistd.h>
20#include "posixtest.h"
21
22int main(void)
23{
24#if defined(_SC_PRIORITY_SCHEDULING)
25
26	if (sysconf(_SC_PRIORITY_SCHEDULING) == -1) {
27		printf("PRIORITY_SCHEDULING not supported\n");
28		return PTS_UNSUPPORTED;
29	}
30
31	pthread_mutexattr_t mutex_attr;
32	pthread_mutex_t mutex;
33	int error, prioceiling;
34
35	error = pthread_mutexattr_init(&mutex_attr);
36	if (error) {
37		printf("pthread_mutexattr_init failed: %s\n", strerror(error));
38		return PTS_UNRESOLVED;
39	}
40
41	/*
42	 * Has to be something other than PTHREAD_PRIO_NONE, the default as per
43	 * pthread_mutexattr_getprotocol.
44	 */
45	error = pthread_mutexattr_setprotocol(&mutex_attr,
46					      PTHREAD_PRIO_PROTECT);
47	if (error) {
48		printf("pthread_mutexattr_setprotocol failed: %s\n",
49		       strerror(error));
50		return PTS_UNRESOLVED;
51	}
52
53	/* Initialize a mutex object */
54	error = pthread_mutex_init(&mutex, &mutex_attr);
55	if (error) {
56		printf("pthread_mutex_init failed: %s\n", strerror(error));
57		return PTS_UNRESOLVED;
58	}
59
60	/* Get the prioceiling of the mutex. */
61	error = pthread_mutex_getprioceiling(&mutex, &prioceiling);
62	if (error) {
63		printf("pthread_mutex_getprioceiling failed: %s\n",
64		       strerror(error));
65		return PTS_FAIL;
66	}
67
68	(void)pthread_mutexattr_destroy(&mutex_attr);
69	(void)pthread_mutex_destroy(&mutex);
70
71	printf("Prioceiling returned: %d\n", prioceiling);
72	return PTS_PASS;
73#else
74	printf("pthread_mutex_getprioceiling not supported");
75	return PTS_UNSUPPORTED;
76#endif
77
78}
79