1 /* Helper program for testing the pthread_mutex_t and pthread_mutexattr_t
4 Copyright (C) 2016-2023 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
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.
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.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>. */
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. */
28 #define PRIOCEILING 42
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)
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
);
50 pthread_mutex_t mutex
;
51 pthread_mutexattr_t attr
;
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
)
61 /* Else, one of the pthread_mutex* functions failed. */
66 /* Destroys MUTEX and re-initializes it using ATTR. */
68 mutex_reinit (pthread_mutex_t
*mutex
, const pthread_mutexattr_t
*attr
)
72 if (pthread_mutex_destroy (mutex
) == 0
73 && pthread_mutex_init (mutex
, attr
) == 0)
79 /* Tests setting the mutex type. */
81 test_settype (pthread_mutex_t
*mutex
, pthread_mutexattr_t
*attr
)
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)
96 /* Tests setting whether the mutex is robust. */
98 test_setrobust (pthread_mutex_t
*mutex
, pthread_mutexattr_t
*attr
)
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)
111 /* Tests setting whether the mutex can be shared between processes. */
113 test_setpshared (pthread_mutex_t
*mutex
, pthread_mutexattr_t
*attr
)
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)
126 /* Tests setting the mutex protocol and, for Priority Protect, the Priority
129 test_setprotocol (pthread_mutex_t
*mutex
, pthread_mutexattr_t
*attr
)
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)