1 /* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
15 
16 #include "thr_template.c"
17 
18 volatile uint32 b32;
19 volatile int32  c32;
20 my_atomic_rwlock_t rwl;
21 
22 /* add and sub a random number in a loop. Must get 0 at the end */
test_atomic_add(void * arg)23 pthread_handler_t test_atomic_add(void *arg)
24 {
25   int m= (*(int *)arg)/2;
26   int32 x;
27   for (x= ((int)(intptr)(&m)); m ; m--)
28   {
29     x= (x*m+0x87654321) & INT_MAX32;
30     my_atomic_rwlock_wrlock(&rwl);
31     my_atomic_add32(&bad, x);
32     my_atomic_rwlock_wrunlock(&rwl);
33 
34     my_atomic_rwlock_wrlock(&rwl);
35     my_atomic_add32(&bad, -x);
36     my_atomic_rwlock_wrunlock(&rwl);
37   }
38   pthread_mutex_lock(&mutex);
39   if (!--running_threads) pthread_cond_signal(&cond);
40   pthread_mutex_unlock(&mutex);
41   return 0;
42 }
43 
44 volatile int64 a64;
45 /* add and sub a random number in a loop. Must get 0 at the end */
test_atomic_add64(void * arg)46 pthread_handler_t test_atomic_add64(void *arg)
47 {
48   int m= (*(int *)arg)/2;
49   int64 x;
50   for (x= ((int64)(intptr)(&m)); m ; m--)
51   {
52     x= (x*m+0xfdecba987654321LL) & INT_MAX64;
53     my_atomic_rwlock_wrlock(&rwl);
54     my_atomic_add64(&a64, x);
55     my_atomic_rwlock_wrunlock(&rwl);
56 
57     my_atomic_rwlock_wrlock(&rwl);
58     my_atomic_add64(&a64, -x);
59     my_atomic_rwlock_wrunlock(&rwl);
60   }
61   pthread_mutex_lock(&mutex);
62   if (!--running_threads)
63   {
64     bad= (a64 != 0);
65     pthread_cond_signal(&cond);
66   }
67   pthread_mutex_unlock(&mutex);
68   return 0;
69 }
70 
71 
72 /*
73   1. generate thread number 0..N-1 from b32
74   2. add it to bad
75   3. swap thread numbers in c32
76   4. (optionally) one more swap to avoid 0 as a result
77   5. subtract result from bad
78   must get 0 in bad at the end
79 */
test_atomic_fas(void * arg)80 pthread_handler_t test_atomic_fas(void *arg)
81 {
82   int    m= *(int *)arg;
83   int32  x;
84 
85   my_atomic_rwlock_wrlock(&rwl);
86   x= my_atomic_add32(&b32, 1);
87   my_atomic_rwlock_wrunlock(&rwl);
88 
89   my_atomic_rwlock_wrlock(&rwl);
90   my_atomic_add32(&bad, x);
91   my_atomic_rwlock_wrunlock(&rwl);
92 
93   for (; m ; m--)
94   {
95     my_atomic_rwlock_wrlock(&rwl);
96     x= my_atomic_fas32(&c32, x);
97     my_atomic_rwlock_wrunlock(&rwl);
98   }
99 
100   if (!x)
101   {
102     my_atomic_rwlock_wrlock(&rwl);
103     x= my_atomic_fas32(&c32, x);
104     my_atomic_rwlock_wrunlock(&rwl);
105   }
106 
107   my_atomic_rwlock_wrlock(&rwl);
108   my_atomic_add32(&bad, -x);
109   my_atomic_rwlock_wrunlock(&rwl);
110 
111   pthread_mutex_lock(&mutex);
112   if (!--running_threads) pthread_cond_signal(&cond);
113   pthread_mutex_unlock(&mutex);
114   return 0;
115 }
116 
117 /*
118   same as test_atomic_add, but my_atomic_add32 is emulated with
119   my_atomic_cas32 - notice that the slowdown is proportional to the
120   number of CPUs
121 */
test_atomic_cas(void * arg)122 pthread_handler_t test_atomic_cas(void *arg)
123 {
124   int m= (*(int *)arg)/2, ok= 0;
125   int32 x, y;
126   for (x= ((int)(intptr)(&m)); m ; m--)
127   {
128     my_atomic_rwlock_wrlock(&rwl);
129     y= my_atomic_load32(&bad);
130     my_atomic_rwlock_wrunlock(&rwl);
131     x= (x*m+0x87654321) & INT_MAX32;
132     do {
133       my_atomic_rwlock_wrlock(&rwl);
134       ok= my_atomic_cas32(&bad, &y, (uint32)y+x);
135       my_atomic_rwlock_wrunlock(&rwl);
136     } while (!ok) ;
137     do {
138       my_atomic_rwlock_wrlock(&rwl);
139       ok= my_atomic_cas32(&bad, &y, y-x);
140       my_atomic_rwlock_wrunlock(&rwl);
141     } while (!ok) ;
142   }
143   pthread_mutex_lock(&mutex);
144   if (!--running_threads) pthread_cond_signal(&cond);
145   pthread_mutex_unlock(&mutex);
146   return 0;
147 }
148 
149 
do_tests()150 void do_tests()
151 {
152   plan(6);
153 
154   bad= my_atomic_initialize();
155   ok(!bad, "my_atomic_initialize() returned %d", bad);
156 
157   my_atomic_rwlock_init(&rwl);
158 
159   b32= c32= 0;
160   test_concurrently("my_atomic_add32", test_atomic_add, THREADS, CYCLES);
161   b32= c32= 0;
162   test_concurrently("my_atomic_fas32", test_atomic_fas, THREADS, CYCLES);
163   b32= c32= 0;
164   test_concurrently("my_atomic_cas32", test_atomic_cas, THREADS, CYCLES);
165 
166   {
167     /*
168       If b is not volatile, the wrong assembly code is generated on OSX Lion
169       as the variable is optimized away as a constant.
170       See Bug#62533 / Bug#13030056.
171       Another workaround is to specify architecture explicitly using e.g.
172       CFLAGS/CXXFLAGS= "-m64".
173     */
174     volatile int64 b=0x1000200030004000LL;
175     a64=0;
176     my_atomic_add64(&a64, b);
177     ok(a64==b, "add64");
178   }
179   a64=0;
180   test_concurrently("my_atomic_add64", test_atomic_add64, THREADS, CYCLES);
181 
182   my_atomic_rwlock_destroy(&rwl);
183 }
184