S390: Use libc_fe* macros in fe* functions.
[glibc.git] / nptl / tst-cnd-broadcast.c
blobcc971060a3e8c06d962ee39b619472477bca40bf
1 /* C11 threads condition broadcast variable tests.
2 Copyright (C) 2018-2019 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 #include <threads.h>
20 #include <stdbool.h>
21 #include <stdio.h>
22 #include <unistd.h>
24 #include <support/check.h>
26 /* Condition variable where child threads will wait. */
27 static cnd_t cond;
29 /* Mutex to control wait on cond. */
30 static mtx_t mutex;
32 /* Number of threads which have entered the cnd_wait region. */
33 static unsigned int waiting_threads;
35 /* Code executed by each thread. */
36 static int
37 child_wait (void* data)
39 /* Wait until parent thread sends broadcast here. */
40 mtx_lock (&mutex);
41 ++waiting_threads;
42 cnd_wait (&cond, &mutex);
43 mtx_unlock (&mutex);
45 thrd_exit (thrd_success);
48 #define N 5
50 static int
51 do_test (void)
53 thrd_t ids[N];
54 unsigned char i;
56 if (cnd_init (&cond) != thrd_success)
57 FAIL_EXIT1 ("cnd_init failed");
58 if (mtx_init (&mutex, mtx_plain) != thrd_success)
59 FAIL_EXIT1 ("mtx_init failed");
61 /* Create N new threads. */
62 for (i = 0; i < N; ++i)
64 if (thrd_create (&ids[i], child_wait, NULL) != thrd_success)
65 FAIL_EXIT1 ("thrd_create failed");
68 /* Wait for other threads to reach their wait func. */
69 while (true)
71 mtx_lock (&mutex);
72 TEST_VERIFY (waiting_threads <= N);
73 bool done_waiting = waiting_threads == N;
74 mtx_unlock (&mutex);
75 if (done_waiting)
76 break;
77 thrd_sleep (&((struct timespec){.tv_nsec = 100 * 1000 * 1000}), NULL);
80 mtx_lock (&mutex);
81 if (cnd_broadcast (&cond) != thrd_success)
82 FAIL_EXIT1 ("cnd_broadcast failed");
83 mtx_unlock (&mutex);
85 for (i = 0; i < N; ++i)
87 if (thrd_join (ids[i], NULL) != thrd_success)
88 FAIL_EXIT1 ("thrd_join failed");
91 mtx_destroy (&mutex);
92 cnd_destroy (&cond);
94 return 0;
97 #include <support/test-driver.c>