1 /* Helper program for testing the pthread_mutex_t pretty printer.
3 Copyright (C) 2016-2021 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 /* Keep the calls to the pthread_* functions on separate lines to make it easy
21 to advance through the program using the gdb 'next' command. */
30 static int test_status_destroyed (pthread_mutex_t
*mutex
);
31 static int test_status_no_robust (pthread_mutex_t
*mutex
,
32 pthread_mutexattr_t
*attr
);
33 static int test_status_robust (pthread_mutex_t
*mutex
,
34 pthread_mutexattr_t
*attr
);
35 static int test_locking_state_robust (pthread_mutex_t
*mutex
);
36 static void *thread_func (void *arg
);
37 static int test_recursive_locks (pthread_mutex_t
*mutex
,
38 pthread_mutexattr_t
*attr
);
43 pthread_mutex_t mutex
;
44 pthread_mutexattr_t attr
;
47 if (pthread_mutexattr_init (&attr
) == 0
48 && test_status_destroyed (&mutex
) == PASS
49 && test_status_no_robust (&mutex
, &attr
) == PASS
50 && test_status_robust (&mutex
, &attr
) == PASS
51 && test_recursive_locks (&mutex
, &attr
) == PASS
)
53 /* Else, one of the pthread_mutex* functions failed. */
58 /* Initializes MUTEX, then destroys it. */
60 test_status_destroyed (pthread_mutex_t
*mutex
)
64 if (pthread_mutex_init (mutex
, NULL
) == 0
65 && pthread_mutex_destroy (mutex
) == 0)
66 result
= PASS
; /* Test status (destroyed). */
71 /* Tests locking of non-robust mutexes. */
73 test_status_no_robust (pthread_mutex_t
*mutex
, pthread_mutexattr_t
*attr
)
77 if (pthread_mutexattr_setrobust (attr
, PTHREAD_MUTEX_STALLED
) == 0
78 && pthread_mutex_init (mutex
, attr
) == 0
79 && pthread_mutex_lock (mutex
) == 0 /* Test status (non-robust). */
80 && pthread_mutex_unlock (mutex
) == 0
81 && pthread_mutex_destroy (mutex
) == 0)
87 /* Tests locking of robust mutexes. */
89 test_status_robust (pthread_mutex_t
*mutex
, pthread_mutexattr_t
*attr
)
93 if (pthread_mutexattr_setrobust (attr
, PTHREAD_MUTEX_ROBUST
) == 0
94 && pthread_mutex_init (mutex
, attr
) == 0
95 && test_locking_state_robust (mutex
) == PASS
/* Test status (robust). */
96 && pthread_mutex_destroy (mutex
) == 0)
102 /* Tests locking and state corruption of robust mutexes. We'll mark it as
103 inconsistent, then not recoverable. */
105 test_locking_state_robust (pthread_mutex_t
*mutex
)
110 if (pthread_create (&thread
, NULL
, thread_func
, mutex
) == 0 /* Create. */
111 && pthread_join (thread
, NULL
) == 0
112 && pthread_mutex_lock (mutex
) == EOWNERDEAD
/* Test locking (robust). */
113 && pthread_mutex_unlock (mutex
) == 0)
119 /* Function to be called by the child thread when testing robust mutexes. */
121 thread_func (void *arg
)
123 pthread_mutex_t
*mutex
= (pthread_mutex_t
*)arg
;
125 if (pthread_mutex_lock (mutex
) != 0) /* Thread function. */
128 /* Thread terminates without unlocking the mutex, thus marking it as
133 /* Tests locking the mutex multiple times in a row. */
135 test_recursive_locks (pthread_mutex_t
*mutex
, pthread_mutexattr_t
*attr
)
139 if (pthread_mutexattr_settype (attr
, PTHREAD_MUTEX_RECURSIVE
) == 0
140 && pthread_mutex_init (mutex
, attr
) == 0
141 && pthread_mutex_lock (mutex
) == 0
142 && pthread_mutex_lock (mutex
) == 0
143 && pthread_mutex_lock (mutex
) == 0 /* Test recursive locks. */
144 && pthread_mutex_unlock (mutex
) == 0
145 && pthread_mutex_unlock (mutex
) == 0
146 && pthread_mutex_unlock (mutex
) == 0
147 && pthread_mutex_destroy (mutex
) == 0)