1 | /* Test 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 <unistd.h> |
24 | #include <error.h> |
25 | #include <errno.h> |
26 | #include <stdio.h> |
27 | |
28 | #define THREADS 500 |
29 | |
30 | void * |
31 | foo (void *arg) |
32 | { |
33 | pthread_mutex_t *mutex = arg; |
34 | pthread_mutex_lock (mutex: mutex); |
35 | pthread_mutex_unlock (mutex: mutex); |
36 | return mutex; |
37 | } |
38 | |
39 | int |
40 | main (int argc, char **argv) |
41 | { |
42 | int i; |
43 | error_t err; |
44 | pthread_t tid[THREADS]; |
45 | pthread_mutex_t mutex[THREADS]; |
46 | |
47 | for (i = 0; i < THREADS; i++) |
48 | { |
49 | pthread_mutex_init (mutex: &mutex[i], mutexattr: 0); |
50 | pthread_mutex_lock (mutex: &mutex[i]); |
51 | err = pthread_create (newthread: &tid[i], attr: 0, start_routine: foo, arg: &mutex[i]); |
52 | if (err) |
53 | error (status: 1, errnum: err, format: "pthread_create" ); |
54 | sched_yield (); |
55 | } |
56 | |
57 | for (i = THREADS - 1; i >= 0; i--) |
58 | { |
59 | void *ret; |
60 | pthread_mutex_unlock (mutex: &mutex[i]); |
61 | err = pthread_join (th: tid[i], thread_return: &ret); |
62 | if (err) |
63 | error (status: 1, errnum: err, format: "pthread_join" ); |
64 | assert (ret == &mutex[i]); |
65 | } |
66 | |
67 | return 0; |
68 | } |
69 | |