blob: 12253aa21a29fb68ad10a635e2bb7787aecd60df [file] [log] [blame]
sewardjb4112022007-11-09 22:49:28 +00001
2#include <pthread.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6/* Simple test program, no race. Parent and child both modify x and
7 use the hardware bus lock. */
8
9#undef PLAT_x86_linux
10#undef PLAT_amd64_linux
11#undef PLAT_ppc32_linux
12#undef PLAT_ppc64_linux
13#undef PLAT_ppc32_aix5
14#undef PLAT_ppc64_aix5
15
16#if !defined(_AIX) && defined(__i386__)
17# define PLAT_x86_linux 1
18#elif !defined(_AIX) && defined(__x86_64__)
19# define PLAT_amd64_linux 1
20#elif !defined(_AIX) && defined(__powerpc__) && !defined(__powerpc64__)
21# define PLAT_ppc32_linux 1
22#elif !defined(_AIX) && defined(__powerpc__) && defined(__powerpc64__)
23# define PLAT_ppc64_linux 1
24#elif defined(_AIX) && defined(__64BIT__)
25# define PLAT_ppc64_aix5 1
26#elif defined(_AIX) && !defined(__64BIT__)
27# define PLAT_ppc32_aix5 1
28#endif
29
sewardjb4112022007-11-09 22:49:28 +000030#if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux)
sewardj2ee33952007-11-27 11:23:55 +000031# define INC(_lval,_lqual) \
sewardjb4112022007-11-09 22:49:28 +000032 __asm__ __volatile__ ( \
33 "lock ; incl (%0)" : /*out*/ : /*in*/"r"(&(_lval)) : "memory", "cc" )
sewardj2ee33952007-11-27 11:23:55 +000034#elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux) \
35 || defined(PLAT_ppc32_aix5) || defined(PLAT_ppc64_aix5)
36# define INC(_lval,_lqual) \
sewardjb4112022007-11-09 22:49:28 +000037 __asm__ __volatile__( \
sewardj2ee33952007-11-27 11:23:55 +000038 "L1xyzzy1" _lqual ":\n" \
sewardjb4112022007-11-09 22:49:28 +000039 " lwarx 15,0,%0\n" \
40 " addi 15,15,1\n" \
41 " stwcx. 15,0,%0\n" \
sewardj2ee33952007-11-27 11:23:55 +000042 " bne- L1xyzzy1" _lqual \
sewardjb4112022007-11-09 22:49:28 +000043 : /*out*/ : /*in*/ "b"(&(_lval)) \
44 : /*trash*/ "r15", "cr0", "memory" \
45 )
46#else
47# error "Fix Me for this platform"
48#endif
49
50
51int x = 0;
52
53void* child_fn ( void* arg )
54{
sewardj2ee33952007-11-27 11:23:55 +000055 INC(x, "childfn");
sewardjb4112022007-11-09 22:49:28 +000056 return NULL;
57}
58
59int main ( void )
60{
61 pthread_t child;
62
63 if (pthread_create(&child, NULL, child_fn, NULL)) {
64 perror("pthread_create");
65 exit(1);
66 }
67
sewardj2ee33952007-11-27 11:23:55 +000068 INC(x, "main");
sewardjb4112022007-11-09 22:49:28 +000069
70 if (pthread_join(child, NULL)) {
71 perror("pthread join");
72 exit(1);
73 }
74
75 printf("x = %d\n", x);
76 return 0;
77}