]>
Commit | Line | Data |
---|---|---|
23b5cae1 MG |
1 | /* Helper program for testing the pthread_mutex_t and pthread_mutexattr_t |
2 | pretty printers. | |
3 | ||
2b778ceb | 4 | Copyright (C) 2016-2021 Free Software Foundation, Inc. |
23b5cae1 MG |
5 | This file is part of the GNU C Library. |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
8 | modify it under the terms of the GNU Lesser General Public | |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
11 | ||
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
18 | License along with the GNU C Library; if not, see | |
5a82c748 | 19 | <https://www.gnu.org/licenses/>. */ |
23b5cae1 MG |
20 | |
21 | /* Keep the calls to the pthread_* functions on separate lines to make it easy | |
22 | to advance through the program using the gdb 'next' command. */ | |
23 | ||
24 | #include <pthread.h> | |
25 | ||
26 | #define PASS 0 | |
27 | #define FAIL 1 | |
28 | #define PRIOCEILING 42 | |
29 | ||
30 | /* Need these so we don't have lines longer than 79 chars. */ | |
31 | #define SET_TYPE(attr, type) pthread_mutexattr_settype (attr, type) | |
32 | #define SET_ROBUST(attr, robust) pthread_mutexattr_setrobust (attr, robust) | |
33 | #define SET_SHARED(attr, shared) pthread_mutexattr_setpshared (attr, shared) | |
34 | #define SET_PROTOCOL(attr, protocol) \ | |
35 | pthread_mutexattr_setprotocol (attr, protocol) | |
36 | #define SET_PRIOCEILING(mutex, prioceiling, old_ceiling) \ | |
37 | pthread_mutex_setprioceiling (mutex, prioceiling, old_ceiling) | |
38 | ||
39 | static int mutex_reinit (pthread_mutex_t *mutex, | |
40 | const pthread_mutexattr_t *attr); | |
41 | static int test_settype (pthread_mutex_t *mutex, pthread_mutexattr_t *attr); | |
42 | static int test_setrobust (pthread_mutex_t *mutex, pthread_mutexattr_t *attr); | |
43 | static int test_setpshared (pthread_mutex_t *mutex, pthread_mutexattr_t *attr); | |
44 | static int test_setprotocol (pthread_mutex_t *mutex, | |
45 | pthread_mutexattr_t *attr); | |
46 | ||
47 | int | |
48 | main (void) | |
49 | { | |
50 | pthread_mutex_t mutex; | |
51 | pthread_mutexattr_t attr; | |
52 | int result = FAIL; | |
53 | ||
54 | if (pthread_mutexattr_init (&attr) == 0 | |
55 | && pthread_mutex_init (&mutex, NULL) == 0 | |
56 | && test_settype (&mutex, &attr) == PASS | |
57 | && test_setrobust (&mutex, &attr) == PASS | |
58 | && test_setpshared (&mutex, &attr) == PASS | |
59 | && test_setprotocol (&mutex, &attr) == PASS) | |
60 | result = PASS; | |
61 | /* Else, one of the pthread_mutex* functions failed. */ | |
62 | ||
63 | return result; | |
64 | } | |
65 | ||
66 | /* Destroys MUTEX and re-initializes it using ATTR. */ | |
67 | static int | |
68 | mutex_reinit (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) | |
69 | { | |
70 | int result = FAIL; | |
71 | ||
72 | if (pthread_mutex_destroy (mutex) == 0 | |
73 | && pthread_mutex_init (mutex, attr) == 0) | |
74 | result = PASS; | |
75 | ||
76 | return result; | |
77 | } | |
78 | ||
79 | /* Tests setting the mutex type. */ | |
80 | static int | |
81 | test_settype (pthread_mutex_t *mutex, pthread_mutexattr_t *attr) | |
82 | { | |
83 | int result = FAIL; | |
84 | ||
85 | if (SET_TYPE (attr, PTHREAD_MUTEX_ERRORCHECK) == 0 /* Set type. */ | |
86 | && mutex_reinit (mutex, attr) == 0 | |
87 | && SET_TYPE (attr, PTHREAD_MUTEX_RECURSIVE) == 0 | |
88 | && mutex_reinit (mutex, attr) == 0 | |
89 | && SET_TYPE (attr, PTHREAD_MUTEX_NORMAL) == 0 | |
90 | && mutex_reinit (mutex, attr) == 0) | |
91 | result = PASS; | |
92 | ||
93 | return result; | |
94 | } | |
95 | ||
96 | /* Tests setting whether the mutex is robust. */ | |
97 | static int | |
98 | test_setrobust (pthread_mutex_t *mutex, pthread_mutexattr_t *attr) | |
99 | { | |
100 | int result = FAIL; | |
101 | ||
102 | if (SET_ROBUST (attr, PTHREAD_MUTEX_ROBUST) == 0 /* Set robust. */ | |
103 | && mutex_reinit (mutex, attr) == 0 | |
104 | && SET_ROBUST (attr, PTHREAD_MUTEX_STALLED) == 0 | |
105 | && mutex_reinit (mutex, attr) == 0) | |
106 | result = PASS; | |
107 | ||
108 | return result; | |
109 | } | |
110 | ||
111 | /* Tests setting whether the mutex can be shared between processes. */ | |
112 | static int | |
113 | test_setpshared (pthread_mutex_t *mutex, pthread_mutexattr_t *attr) | |
114 | { | |
115 | int result = FAIL; | |
116 | ||
117 | if (SET_SHARED (attr, PTHREAD_PROCESS_SHARED) == 0 /* Set shared. */ | |
118 | && mutex_reinit (mutex, attr) == 0 | |
119 | && SET_SHARED (attr, PTHREAD_PROCESS_PRIVATE) == 0 | |
120 | && mutex_reinit (mutex, attr) == 0) | |
121 | result = PASS; | |
122 | ||
123 | return result; | |
124 | } | |
125 | ||
126 | /* Tests setting the mutex protocol and, for Priority Protect, the Priority | |
127 | Ceiling. */ | |
128 | static int | |
129 | test_setprotocol (pthread_mutex_t *mutex, pthread_mutexattr_t *attr) | |
130 | { | |
131 | int result = FAIL; | |
132 | int old_prioceiling; | |
133 | ||
134 | if (SET_PROTOCOL (attr, PTHREAD_PRIO_INHERIT) == 0 /* Set protocol. */ | |
135 | && mutex_reinit (mutex, attr) == 0 | |
136 | && SET_PROTOCOL (attr, PTHREAD_PRIO_PROTECT) == 0 | |
137 | && mutex_reinit (mutex, attr) == 0 | |
138 | && SET_PRIOCEILING(mutex, PRIOCEILING, &old_prioceiling) == 0 | |
139 | && SET_PROTOCOL (attr, PTHREAD_PRIO_NONE) == 0 | |
140 | && mutex_reinit (mutex, attr) == 0) | |
141 | result = PASS; | |
142 | ||
143 | return result; | |
144 | } |