]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/valgrind/src/valgrind-3.6.0-svn/helgrind/tests/tc08_hbl2.c
update
[l4.git] / l4 / pkg / valgrind / src / valgrind-3.6.0-svn / helgrind / tests / tc08_hbl2.c
1
2 /* FIXME: this is basically a bad test as it is scheduling-
3    sensitive.  Sometimes the output is:
4
5    child: new value 6
6    child: new value 10
7    done, x = 10
8
9    and sometimes
10
11    child: new value 10
12    done, x = 10
13 */
14
15 #include <pthread.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19
20 /* Simple test program, no race.  Parent writes atomically to a counter
21    whilst child reads it.  When counter reaches a prearranged value, 
22    child joins back to parent.  Parent (writer) uses hardware bus lock;
23    child is only reading and so does not need to use a bus lock. */
24
25 #undef PLAT_ppc64_aix5
26 #undef PLAT_ppc32_aix5
27 #undef PLAT_x86_darwin
28 #undef PLAT_amd64_darwin
29 #undef PLAT_x86_linux
30 #undef PLAT_amd64_linux
31 #undef PLAT_ppc32_linux
32 #undef PLAT_ppc64_linux
33 #undef PLAT_arm_linux
34 #undef PLAT_s390x_linux
35
36 #if defined(_AIX) && defined(__64BIT__)
37 #  define PLAT_ppc64_aix5 1
38 #elif defined(_AIX) && !defined(__64BIT__)
39 #  define PLAT_ppc32_aix5 1
40 #elif defined(__APPLE__) && defined(__i386__)
41 #  define PLAT_x86_darwin 1
42 #elif defined(__APPLE__) && defined(__x86_64__)
43 #  define PLAT_amd64_darwin 1
44 #elif defined(__linux__) && defined(__i386__)
45 #  define PLAT_x86_linux 1
46 #elif defined(__linux__) && defined(__x86_64__)
47 #  define PLAT_amd64_linux 1
48 #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__)
49 #  define PLAT_ppc32_linux 1
50 #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__)
51 #  define PLAT_ppc64_linux 1
52 #elif defined(__linux__) && defined(__arm__)
53 #  define PLAT_arm_linux 1
54 #elif defined(__linux__) && defined(__s390x__)
55 #  define PLAT_s390x_linux 1
56 #endif
57
58
59 #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \
60     || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin)
61 #  define INC(_lval,_lqual)          \
62       __asm__ __volatile__ ( \
63       "lock ; incl (%0)" : /*out*/ : /*in*/"r"(&(_lval)) : "memory", "cc" )
64 #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux) \
65       || defined(PLAT_ppc32_aix5) || defined(PLAT_ppc64_aix5)
66 #  define INC(_lval,_lqual)               \
67    __asm__ __volatile__(                  \
68       "1:\n"                              \
69       "        lwarx 15,0,%0\n"           \
70       "        addi 15,15,1\n"            \
71       "        stwcx. 15,0,%0\n"          \
72       "        bne- 1b\n"                 \
73       : /*out*/ : /*in*/ "b"(&(_lval))    \
74       : /*trash*/ "r15", "cr0", "memory"  \
75    )
76 #elif defined(PLAT_arm_linux)
77 #  define INC(_lval,_lqual) \
78   __asm__ __volatile__( \
79       "1:\n"                                 \
80       "        ldrex r8, [%0, #0]\n"         \
81       "        add   r8, r8, #1\n"           \
82       "        strex r9, r8, [%0, #0]\n"     \
83       "        cmp   r9, #0\n"               \
84       "        bne   1b\n"                   \
85       : /*out*/ : /*in*/ "r"(&(_lval))       \
86       : /*trash*/ "r8", "r9", "cc", "memory" \
87   );
88 #elif defined(PLAT_s390x_linux)
89 #  define INC(_lval,_lqual) \
90    __asm__ __volatile__( \
91       "1: l     0,%0\n"                            \
92       "   lr    1,0\n"                             \
93       "   ahi   1,1\n"                             \
94       "   cs    0,1,%0\n"                          \
95       "   jl    1b\n"                              \
96       : "+m" (_lval) :: "cc", "0","1" \
97    )
98 #else
99 #  error "Fix Me for this platform"
100 #endif
101
102
103
104 #define LIMIT 10
105
106 volatile int x = 0;
107
108 void* child_fn ( void* arg )
109 {
110    int q = 0;
111    int oldx = 0;
112    int ctr = 0;
113    while (1) {
114       q = (x >= LIMIT);
115       if (x != oldx) {
116          oldx = x;
117          printf("child: new value %d\n", oldx);
118          fflush(stdout);
119       }
120       if (q) break;
121       /* Make sure the parent doesn't starve.  Seems to be a problem
122          on very slow machines. */
123       ctr++;
124       if (ctr == 2000000) sleep(1);
125    }
126    return NULL;
127 }
128
129 int main ( void )
130 {
131    pthread_t child;
132    int i;
133
134    if (pthread_create(&child, NULL, child_fn, NULL)) {
135       perror("pthread_create");
136       exit(1);
137    }
138
139    for (i = 0; i < LIMIT; i++) {
140       INC(x, "main");
141       if (i == 5) sleep(1); /* make sure child doesn't starve */
142    }
143
144    if (pthread_join(child, NULL)) {
145       perror("pthread join");
146       exit(1);
147    }
148
149    printf("done, x = %d\n", x);
150
151    return 0;
152 }