]> git.ipfire.org Git - thirdparty/glibc.git/blob - htl/tests/test-9.c
20acc38b6cba428043d21e16b0349ee2ea3003d8
[thirdparty/glibc.git] / htl / tests / test-9.c
1 /* Test recursive mutexes.
2 Copyright (C) 2000-2019 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 <http://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 (arg);
36
37 foo = pthread_self ();
38
39 for (i = 0; i < 500; i++)
40 pthread_mutex_lock (arg);
41 for (i = 0; i < 500; i++)
42 pthread_mutex_unlock (arg);
43
44 assert (foo == pthread_self ());
45
46 pthread_mutex_unlock (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 (&mattr);
61 if (err)
62 error (1, err, "pthread_mutexattr_init");
63
64 err = pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_RECURSIVE);
65 if (err)
66 error (1, err, "pthread_mutexattr_settype");
67
68 err = pthread_mutex_init (&mutex, &mattr);
69 if (err)
70 error (1, err, "pthread_mutex_init");
71
72 err = pthread_mutexattr_destroy (&mattr);
73 if (err)
74 error (1, err, "pthread_mutexattr_destroy");
75
76 pthread_mutex_lock (&mutex);
77 pthread_mutex_lock (&mutex);
78 pthread_mutex_unlock (&mutex);
79 pthread_mutex_unlock (&mutex);
80
81 for (i = 0; i < THREADS; i++)
82 {
83 err = pthread_create (&tid[i], 0, thr, &mutex);
84 if (err)
85 error (1, err, "pthread_create (%d)", i);
86 }
87
88 for (i = 0; i < THREADS; i++)
89 {
90 void *ret;
91
92 err = pthread_join (tid[i], &ret);
93 if (err)
94 error (1, err, "pthread_join");
95
96 assert (ret == 0);
97 }
98
99 err = pthread_mutex_destroy (&mutex);
100 if (err)
101 error (1, err, "pthread_mutex_destroy");
102
103 return 0;
104 }