drd/tests/Makefile.am: Add dlopen.stdout.exp to EXTRA_DIST
[valgrind.git] / helgrind / tests / tc03_re_excl.c
blob6fd5a1b09d9749d8bdc05ce8b228529dc12aa591
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 /* Simple test program, no race: parent only modifies x after child
7 has modified it and then joined with the parent. Tests simple
8 thread lifetime segment handling. */
10 /* A simple function to "use" a value, so that gcc can't
11 possibly optimise it into nothing. */
12 static void use ( int x ) {
13 __asm__ __volatile__( "" : : "r"(x) : "cc","memory" );
16 static void* worker_thread ( void* argV )
18 int* arg = (int*)argV;
19 use(arg[5]); /* read access */
20 return NULL;
23 int main ( void )
25 pthread_t thread_id;
26 volatile int* x = malloc(10 * sizeof(int));
27 x[5] = 1;
28 /* x[5] is Excl(parent) */
30 pthread_create(&thread_id, 0, worker_thread, (void*)x);
32 use(x[5]); /* read access */
34 /* Just before the threads join, x[5] is ShR (read by both parent
35 and child) */
36 pthread_join(thread_id, 0);
37 /* x[5] is Excl(parent), because only parent and child accessed it
38 and child has merged to parent. So now it's ok for parent to
39 access it without locking. */
41 x[5] = 0; /* write access */
43 return x[5];