]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/helgrind/tests/bar_trivial.c
Some minor fixes.
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / helgrind / tests / bar_trivial.c
1
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>
12
13 int x = 0;
14
15 pthread_barrier_t bar;
16
17 void* child_fn ( void* arg )
18 {
19    long r, n = (long)arg;
20
21    if (n == 1) x++;
22
23    r = pthread_barrier_wait(&bar); 
24    assert(r == 0 || r == PTHREAD_BARRIER_SERIAL_THREAD);
25
26    if (n == 0) x++;
27
28    sleep(1); /* ensure both threads get to this point before
29                 either exits. */
30    return NULL;
31 }
32
33 #define NTHR 2
34
35 int main ( void )
36 {
37    long i, r;
38    pthread_t thr[NTHR];
39
40    r = pthread_barrier_init(&bar, NULL, NTHR);
41    assert(!r);
42
43    for (i = 0; i < NTHR; i++) {
44       r = pthread_create(&thr[i], NULL, child_fn, (void*)i);
45       assert(!r);
46    }
47
48    for (i = 0; i < NTHR; i++) {
49       r = pthread_join(thr[i], NULL);
50       assert(!r);
51    }
52
53    r = pthread_barrier_destroy(&bar); assert(!r);
54
55    printf("x = %d\n", x);
56    return 0;
57 }