-> 3.17.0.RC2
[valgrind.git] / helgrind / tests / bar_trivial.c
blob8c221ae8ee6bd0919e41cce82ea7884389b734bb
2 /* This is the most trivial test I could think of that involves
3 barriers. If H fails to notice the pthread_barrier_wait call then
4 it will report a race. Correct behaviour is not to report a race
5 (there isn't one.) */
6 #define _GNU_SOURCE
7 #include <pthread.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <unistd.h>
13 int x = 0;
15 pthread_barrier_t bar;
17 void* child_fn ( void* arg )
19 long r, n = (long)arg;
21 if (n == 1) x++;
23 r = pthread_barrier_wait(&bar);
24 assert(r == 0 || r == PTHREAD_BARRIER_SERIAL_THREAD);
26 if (n == 0) x++;
28 sleep(1); /* ensure both threads get to this point before
29 either exits. */
30 return NULL;
33 #define NTHR 2
35 int main ( void )
37 long i, r;
38 pthread_t thr[NTHR];
40 r = pthread_barrier_init(&bar, NULL, NTHR);
41 assert(!r);
43 for (i = 0; i < NTHR; i++) {
44 r = pthread_create(&thr[i], NULL, child_fn, (void*)i);
45 assert(!r);
48 for (i = 0; i < NTHR; i++) {
49 r = pthread_join(thr[i], NULL);
50 assert(!r);
53 r = pthread_barrier_destroy(&bar); assert(!r);
55 printf("x = %d\n", x);
56 return 0;