blob: ab14f90481900596e83de56f66c0c33e3854ec0c [file] [log] [blame]
bartb56542a2011-07-29 12:32:53 +00001/*
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
bart6c73cf62011-07-30 09:28:13 +000016static pthread_barrier_t* s_barrier;
bartb56542a2011-07-29 12:32:53 +000017
18static void* thread(void* arg)
19{
20 write(STDOUT_FILENO, ".", 1);
bart6c73cf62011-07-30 09:28:13 +000021 pthread_barrier_wait(s_barrier);
bartb56542a2011-07-29 12:32:53 +000022 return NULL;
23}
24
25int main(int argc, char** argv)
26{
27 pthread_t *tid;
bart6c73cf62011-07-30 09:28:13 +000028 int barriers = argc > 1 ? atoi(argv[1]) : 20;
bartb56542a2011-07-29 12:32:53 +000029 int barrier_participants = 2;
30 int thread_count = barriers * barrier_participants;
bart6c73cf62011-07-30 09:28:13 +000031 int res, i;
bartb56542a2011-07-29 12:32:53 +000032
bart6c73cf62011-07-30 09:28:13 +000033 s_barrier = malloc(sizeof(*s_barrier));
34 res = pthread_barrier_init(s_barrier, NULL, barrier_participants);
35 assert(res == 0);
bartb56542a2011-07-29 12:32:53 +000036
37 tid = malloc(thread_count * sizeof(*tid));
38 assert(tid);
bart6c73cf62011-07-30 09:28:13 +000039 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 }
bartb56542a2011-07-29 12:32:53 +000047 free(tid);
48
bart6c73cf62011-07-30 09:28:13 +000049 res = pthread_barrier_destroy(s_barrier);
50 assert(res == 0);
51 free(s_barrier);
52 s_barrier = NULL;
bartb56542a2011-07-29 12:32:53 +000053
54 write(STDOUT_FILENO, "\n", 1);
55 fprintf(stderr, "Done.\n");
56
57 return 0;
58}