-> 3.17.0.RC2
[valgrind.git] / helgrind / tests / hg03_inherit.c
blob7c429f838ff31dedcdfc89de249f4f584de31bbe
2 /* I don't think this is a very good test .. all this
3 sleepery is highly confusing. */
5 /* test child thread inheriting data */
7 #include <stdio.h>
8 #include <pthread.h>
9 #include <unistd.h>
11 static volatile int shared[2];
13 static void *t1(void *v)
15 volatile int *ip = (int *)v;
16 if (0) printf("ta W\n");
17 *ip += 44;
18 *ip *= 2;
19 sleep(1);
20 return 0;
23 static void *t2(void *v)
25 volatile int *ip = (int *)v;
26 sleep(2);
27 if (0) printf("tb W\n");
28 *ip += 88;
29 *ip *= 3;
30 sleep(1);
31 return 0;
34 int main(void)
36 pthread_t a, b;
37 volatile int ret = 0;
39 sleep(0);
41 shared[0] = 22;
42 shared[1] = 77;
44 pthread_create(&a, NULL, t1, (void *)&shared[0]);
45 // a steals shared[0] from root thread, so is excl(a)
46 pthread_create(&b, NULL, t2, (void *)&shared[1]);
47 // b steals shared[1] from root thread, so is excl(b)
49 pthread_join(a, NULL);
50 // b's stuff (shared[1]) still belongs to b, so is excl(b)
52 // ret is excl(root), and shared[0] is re-acquired as excl(root)
53 // since a joined to root
54 if (0) printf("r R1\n");
55 ret += shared[0]; /* no error - a is finished */
57 // but shared[1] is excl(b); hence we're reading excl(b)
58 // without a lock and without a dependency edge
59 if (0) printf("r R2\n");
60 ret += shared[1]; /* expect error - b has not finished,
61 so we can't touch shared[1] yet */
63 pthread_join(b, NULL);
66 return ret;