drd: Add a consistency check
[valgrind.git] / drd / tests / pth_barrier_thr_cr.c
blobab14f90481900596e83de56f66c0c33e3854ec0c
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 */
8 #define _GNU_SOURCE
10 #include <assert.h>
11 #include <pthread.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
16 static pthread_barrier_t* s_barrier;
18 static void* thread(void* arg)
20 write(STDOUT_FILENO, ".", 1);
21 pthread_barrier_wait(s_barrier);
22 return NULL;
25 int main(int argc, char** argv)
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;
33 s_barrier = malloc(sizeof(*s_barrier));
34 res = pthread_barrier_init(s_barrier, NULL, barrier_participants);
35 assert(res == 0);
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);
43 for (i = 0; i < thread_count; i++) {
44 res = pthread_join(tid[i], NULL);
45 assert(res == 0);
47 free(tid);
49 res = pthread_barrier_destroy(s_barrier);
50 assert(res == 0);
51 free(s_barrier);
52 s_barrier = NULL;
54 write(STDOUT_FILENO, "\n", 1);
55 fprintf(stderr, "Done.\n");
57 return 0;