sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 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 | |
sewardj | 8777f73 | 2010-01-02 10:43:23 +0000 | [diff] [blame] | 25 | #undef PLAT_x86_darwin |
| 26 | #undef PLAT_amd64_darwin |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 27 | #undef PLAT_x86_linux |
| 28 | #undef PLAT_amd64_linux |
| 29 | #undef PLAT_ppc32_linux |
| 30 | #undef PLAT_ppc64_linux |
sewardj | 8777f73 | 2010-01-02 10:43:23 +0000 | [diff] [blame] | 31 | #undef PLAT_arm_linux |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 32 | #undef PLAT_s390x_linux |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 33 | |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 34 | #if defined(__APPLE__) && defined(__i386__) |
sewardj | 8777f73 | 2010-01-02 10:43:23 +0000 | [diff] [blame] | 35 | # define PLAT_x86_darwin 1 |
| 36 | #elif defined(__APPLE__) && defined(__x86_64__) |
| 37 | # define PLAT_amd64_darwin 1 |
| 38 | #elif defined(__linux__) && defined(__i386__) |
| 39 | # define PLAT_x86_linux 1 |
| 40 | #elif defined(__linux__) && defined(__x86_64__) |
| 41 | # define PLAT_amd64_linux 1 |
| 42 | #elif defined(__linux__) && defined(__powerpc__) && !defined(__powerpc64__) |
| 43 | # define PLAT_ppc32_linux 1 |
| 44 | #elif defined(__linux__) && defined(__powerpc__) && defined(__powerpc64__) |
| 45 | # define PLAT_ppc64_linux 1 |
| 46 | #elif defined(__linux__) && defined(__arm__) |
| 47 | # define PLAT_arm_linux 1 |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 48 | #elif defined(__linux__) && defined(__s390x__) |
| 49 | # define PLAT_s390x_linux 1 |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
sewardj | 8777f73 | 2010-01-02 10:43:23 +0000 | [diff] [blame] | 52 | |
sewardj | f5b5f84 | 2010-01-04 10:47:25 +0000 | [diff] [blame] | 53 | #if defined(PLAT_amd64_linux) || defined(PLAT_x86_linux) \ |
| 54 | || defined(PLAT_amd64_darwin) || defined(PLAT_x86_darwin) |
sewardj | 2ee3395 | 2007-11-27 11:23:55 +0000 | [diff] [blame] | 55 | # define INC(_lval,_lqual) \ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 56 | __asm__ __volatile__ ( \ |
| 57 | "lock ; incl (%0)" : /*out*/ : /*in*/"r"(&(_lval)) : "memory", "cc" ) |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 58 | #elif defined(PLAT_ppc32_linux) || defined(PLAT_ppc64_linux) |
sewardj | 2ee3395 | 2007-11-27 11:23:55 +0000 | [diff] [blame] | 59 | # define INC(_lval,_lqual) \ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 60 | __asm__ __volatile__( \ |
sewardj | d12c4a5 | 2011-03-28 08:37:19 +0000 | [diff] [blame] | 61 | "1:\n" \ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 62 | " lwarx 15,0,%0\n" \ |
| 63 | " addi 15,15,1\n" \ |
| 64 | " stwcx. 15,0,%0\n" \ |
sewardj | d12c4a5 | 2011-03-28 08:37:19 +0000 | [diff] [blame] | 65 | " bne- 1b\n" \ |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 66 | : /*out*/ : /*in*/ "b"(&(_lval)) \ |
| 67 | : /*trash*/ "r15", "cr0", "memory" \ |
| 68 | ) |
sewardj | 8777f73 | 2010-01-02 10:43:23 +0000 | [diff] [blame] | 69 | #elif defined(PLAT_arm_linux) |
| 70 | # define INC(_lval,_lqual) \ |
| 71 | __asm__ __volatile__( \ |
sewardj | d12c4a5 | 2011-03-28 08:37:19 +0000 | [diff] [blame] | 72 | "1:\n" \ |
sewardj | 8777f73 | 2010-01-02 10:43:23 +0000 | [diff] [blame] | 73 | " ldrex r8, [%0, #0]\n" \ |
| 74 | " add r8, r8, #1\n" \ |
| 75 | " strex r9, r8, [%0, #0]\n" \ |
| 76 | " cmp r9, #0\n" \ |
sewardj | d12c4a5 | 2011-03-28 08:37:19 +0000 | [diff] [blame] | 77 | " bne 1b\n" \ |
sewardj | 8777f73 | 2010-01-02 10:43:23 +0000 | [diff] [blame] | 78 | : /*out*/ : /*in*/ "r"(&(_lval)) \ |
| 79 | : /*trash*/ "r8", "r9", "cc", "memory" \ |
| 80 | ); |
sewardj | b5b8740 | 2011-03-07 16:05:35 +0000 | [diff] [blame] | 81 | #elif defined(PLAT_s390x_linux) |
| 82 | # define INC(_lval,_lqual) \ |
| 83 | __asm__ __volatile__( \ |
| 84 | "1: l 0,%0\n" \ |
| 85 | " lr 1,0\n" \ |
| 86 | " ahi 1,1\n" \ |
| 87 | " cs 0,1,%0\n" \ |
| 88 | " jl 1b\n" \ |
| 89 | : "+m" (_lval) :: "cc", "0","1" \ |
| 90 | ) |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 91 | #else |
| 92 | # error "Fix Me for this platform" |
| 93 | #endif |
| 94 | |
| 95 | |
| 96 | |
| 97 | #define LIMIT 10 |
| 98 | |
| 99 | volatile int x = 0; |
| 100 | |
| 101 | void* child_fn ( void* arg ) |
| 102 | { |
| 103 | int q = 0; |
| 104 | int oldx = 0; |
| 105 | int ctr = 0; |
| 106 | while (1) { |
| 107 | q = (x >= LIMIT); |
| 108 | if (x != oldx) { |
| 109 | oldx = x; |
| 110 | printf("child: new value %d\n", oldx); |
| 111 | fflush(stdout); |
| 112 | } |
| 113 | if (q) break; |
| 114 | /* Make sure the parent doesn't starve. Seems to be a problem |
| 115 | on very slow machines. */ |
| 116 | ctr++; |
| 117 | if (ctr == 2000000) sleep(1); |
| 118 | } |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | int main ( void ) |
| 123 | { |
| 124 | pthread_t child; |
| 125 | int i; |
| 126 | |
| 127 | if (pthread_create(&child, NULL, child_fn, NULL)) { |
| 128 | perror("pthread_create"); |
| 129 | exit(1); |
| 130 | } |
| 131 | |
| 132 | for (i = 0; i < LIMIT; i++) { |
sewardj | 2ee3395 | 2007-11-27 11:23:55 +0000 | [diff] [blame] | 133 | INC(x, "main"); |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 134 | if (i == 5) sleep(1); /* make sure child doesn't starve */ |
| 135 | } |
| 136 | |
sewardj | b411202 | 2007-11-09 22:49:28 +0000 | [diff] [blame] | 137 | if (pthread_join(child, NULL)) { |
| 138 | perror("pthread join"); |
| 139 | exit(1); |
| 140 | } |
| 141 | |
| 142 | printf("done, x = %d\n", x); |
| 143 | |
| 144 | return 0; |
| 145 | } |