mips: dl-machine-reject-phdr: Get rid of alloca.
[glibc.git] / htl / tests / test-9.c
blobd849689f8d7ece1aa5cb2a581d3c6f196ad80957
1 /* Test recursive mutexes.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #define _GNU_SOURCE
21 #include <pthread.h>
22 #include <assert.h>
23 #include <error.h>
24 #include <errno.h>
26 #define THREADS 10
28 int foo;
30 void *
31 thr (void *arg)
33 int i;
35 pthread_mutex_lock (arg);
37 foo = pthread_self ();
39 for (i = 0; i < 500; i++)
40 pthread_mutex_lock (arg);
41 for (i = 0; i < 500; i++)
42 pthread_mutex_unlock (arg);
44 assert (foo == pthread_self ());
46 pthread_mutex_unlock (arg);
48 return 0;
51 int
52 main (int argc, char **argv)
54 error_t err;
55 int i;
56 pthread_t tid[THREADS];
57 pthread_mutexattr_t mattr;
58 pthread_mutex_t mutex;
60 err = pthread_mutexattr_init (&mattr);
61 if (err)
62 error (1, err, "pthread_mutexattr_init");
64 err = pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_RECURSIVE);
65 if (err)
66 error (1, err, "pthread_mutexattr_settype");
68 err = pthread_mutex_init (&mutex, &mattr);
69 if (err)
70 error (1, err, "pthread_mutex_init");
72 err = pthread_mutexattr_destroy (&mattr);
73 if (err)
74 error (1, err, "pthread_mutexattr_destroy");
76 pthread_mutex_lock (&mutex);
77 pthread_mutex_lock (&mutex);
78 pthread_mutex_unlock (&mutex);
79 pthread_mutex_unlock (&mutex);
81 for (i = 0; i < THREADS; i++)
83 err = pthread_create (&tid[i], 0, thr, &mutex);
84 if (err)
85 error (1, err, "pthread_create (%d)", i);
88 for (i = 0; i < THREADS; i++)
90 void *ret;
92 err = pthread_join (tid[i], &ret);
93 if (err)
94 error (1, err, "pthread_join");
96 assert (ret == 0);
99 err = pthread_mutex_destroy (&mutex);
100 if (err)
101 error (1, err, "pthread_mutex_destroy");
103 return 0;