1 /*
2 * Test program that triggers pthread_barrier_wait() where each
3 * pthread_barrier_wait() call is invoked by another thread. This is the only
4 * test program that triggers the code guarded by if (q->thread_finished) in
5 * DRD_(barrier_pre_wait)().
6 */
7
8 #define _GNU_SOURCE
9
10 #include <assert.h>
11 #include <pthread.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 static pthread_barrier_t* s_barrier;
17
thread(void * arg)18 static void* thread(void* arg)
19 {
20 write(STDOUT_FILENO, ".", 1);
21 pthread_barrier_wait(s_barrier);
22 return NULL;
23 }
24
main(int argc,char ** argv)25 int main(int argc, char** argv)
26 {
27 pthread_t *tid;
28 int barriers = argc > 1 ? atoi(argv[1]) : 20;
29 int barrier_participants = 2;
30 int thread_count = barriers * barrier_participants;
31 int res, i;
32
33 s_barrier = malloc(sizeof(*s_barrier));
34 res = pthread_barrier_init(s_barrier, NULL, barrier_participants);
35 assert(res == 0);
36
37 tid = malloc(thread_count * sizeof(*tid));
38 assert(tid);
39 for (i = 0; i < thread_count; i++) {
40 res = pthread_create(&tid[i], NULL, thread, NULL);
41 assert(res == 0);
42 }
43 for (i = 0; i < thread_count; i++) {
44 res = pthread_join(tid[i], NULL);
45 assert(res == 0);
46 }
47 free(tid);
48
49 res = pthread_barrier_destroy(s_barrier);
50 assert(res == 0);
51 free(s_barrier);
52 s_barrier = NULL;
53
54 write(STDOUT_FILENO, "\n", 1);
55 fprintf(stderr, "Done.\n");
56
57 return 0;
58 }
59