]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tst-mtx-basic.c
Rename sys/ucontext.h to bits/ucontext.h.
[thirdparty/glibc.git] / nptl / tst-mtx-basic.c
CommitLineData
0a07288b 1/* C11 threads basic mutex tests.
d614a753 2 Copyright (C) 2018-2020 Free Software Foundation, Inc.
0a07288b
AZ
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
5a82c748 17 <https://www.gnu.org/licenses/>. */
0a07288b
AZ
18
19#include <threads.h>
20#include <stdio.h>
21#include <unistd.h>
22
23#include <support/check.h>
24
25/* Shared mutex between child and parent. */
26static mtx_t mutex;
27
28/* Shared counter to check possible race conditions. */
29static int counter;
30
31static int
32child_add (void *arg)
33{
34 if (mtx_lock (&mutex) != thrd_success)
35 FAIL_EXIT1 ("mtx_lock failed");
36
37 counter++;
38
39 if (mtx_unlock (&mutex) != thrd_success)
40 FAIL_EXIT1 ("mtx_unlock failed");
41
42 thrd_exit (thrd_success);
43}
44
45static int
46do_test (void)
47{
48 mtx_init (&mutex, mtx_plain);
49
50 thrd_t id;
51 if (thrd_create (&id, child_add, NULL) != thrd_success)
52 FAIL_EXIT1 ("thrd_create failed");
53
54 if (mtx_lock (&mutex) != thrd_success)
55 FAIL_EXIT1 ("mtx_lock failed");
56
57 counter++;
58
59 if (mtx_unlock (&mutex) != thrd_success)
60 FAIL_EXIT1 ("mtx_unlock failed");
61
62 if (thrd_join (id, NULL) != thrd_success)
63 FAIL_EXIT1 ("thrd_join failed");
64
65 if (counter != 2)
66 FAIL_EXIT1 ("counter (%d) != 2", counter);
67
68 mtx_destroy (&mutex);
69
70 return 0;
71}
72
73#include <support/test-driver.c>