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/>. */
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
);
52 main (int argc
, char **argv
)
56 pthread_t tid
[THREADS
];
57 pthread_mutexattr_t mattr
;
58 pthread_mutex_t mutex
;
60 err
= pthread_mutexattr_init (&mattr
);
62 error (1, err
, "pthread_mutexattr_init");
64 err
= pthread_mutexattr_settype (&mattr
, PTHREAD_MUTEX_RECURSIVE
);
66 error (1, err
, "pthread_mutexattr_settype");
68 err
= pthread_mutex_init (&mutex
, &mattr
);
70 error (1, err
, "pthread_mutex_init");
72 err
= pthread_mutexattr_destroy (&mattr
);
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
);
85 error (1, err
, "pthread_create (%d)", i
);
88 for (i
= 0; i
< THREADS
; i
++)
92 err
= pthread_join (tid
[i
], &ret
);
94 error (1, err
, "pthread_join");
99 err
= pthread_mutex_destroy (&mutex
);
101 error (1, err
, "pthread_mutex_destroy");