1 | /* Test recursive mutexes. |
2 | Copyright (C) 2000-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #define _GNU_SOURCE |
20 | |
21 | #include <pthread.h> |
22 | #include <assert.h> |
23 | #include <error.h> |
24 | #include <errno.h> |
25 | |
26 | #define THREADS 10 |
27 | |
28 | int foo; |
29 | |
30 | void * |
31 | thr (void *arg) |
32 | { |
33 | int i; |
34 | |
35 | pthread_mutex_lock (mutex: arg); |
36 | |
37 | foo = pthread_self (); |
38 | |
39 | for (i = 0; i < 500; i++) |
40 | pthread_mutex_lock (mutex: arg); |
41 | for (i = 0; i < 500; i++) |
42 | pthread_mutex_unlock (mutex: arg); |
43 | |
44 | assert (foo == pthread_self ()); |
45 | |
46 | pthread_mutex_unlock (mutex: arg); |
47 | |
48 | return 0; |
49 | } |
50 | |
51 | int |
52 | main (int argc, char **argv) |
53 | { |
54 | error_t err; |
55 | int i; |
56 | pthread_t tid[THREADS]; |
57 | pthread_mutexattr_t mattr; |
58 | pthread_mutex_t mutex; |
59 | |
60 | err = pthread_mutexattr_init (attr: &mattr); |
61 | if (err) |
62 | error (status: 1, errnum: err, format: "pthread_mutexattr_init" ); |
63 | |
64 | err = pthread_mutexattr_settype (attr: &mattr, kind: PTHREAD_MUTEX_RECURSIVE); |
65 | if (err) |
66 | error (status: 1, errnum: err, format: "pthread_mutexattr_settype" ); |
67 | |
68 | err = pthread_mutex_init (mutex: &mutex, mutexattr: &mattr); |
69 | if (err) |
70 | error (status: 1, errnum: err, format: "pthread_mutex_init" ); |
71 | |
72 | err = pthread_mutexattr_destroy (attr: &mattr); |
73 | if (err) |
74 | error (status: 1, errnum: err, format: "pthread_mutexattr_destroy" ); |
75 | |
76 | pthread_mutex_lock (mutex: &mutex); |
77 | pthread_mutex_lock (mutex: &mutex); |
78 | pthread_mutex_unlock (mutex: &mutex); |
79 | pthread_mutex_unlock (mutex: &mutex); |
80 | |
81 | for (i = 0; i < THREADS; i++) |
82 | { |
83 | err = pthread_create (newthread: &tid[i], attr: 0, start_routine: thr, arg: &mutex); |
84 | if (err) |
85 | error (status: 1, errnum: err, format: "pthread_create (%d)" , i); |
86 | } |
87 | |
88 | for (i = 0; i < THREADS; i++) |
89 | { |
90 | void *ret; |
91 | |
92 | err = pthread_join (th: tid[i], thread_return: &ret); |
93 | if (err) |
94 | error (status: 1, errnum: err, format: "pthread_join" ); |
95 | |
96 | assert (ret == 0); |
97 | } |
98 | |
99 | err = pthread_mutex_destroy (mutex: &mutex); |
100 | if (err) |
101 | error (status: 1, errnum: err, format: "pthread_mutex_destroy" ); |
102 | |
103 | return 0; |
104 | } |
105 | |