17 #include "my_config.h"
18 #include <gtest/gtest.h>
20 #include <my_global.h>
22 #include <my_atomic.h>
25 namespace mysys_my_atomic_unittest {
27 #include "thr_template.cc"
34 pthread_handler_t test_atomic_add(
void *arg)
36 int m= (*(
int *)arg)/2;
38 for (x= ((
int)(intptr)(&m)); m ; m--)
40 x= (x*m+0x87654321) & INT_MAX32;
41 my_atomic_rwlock_wrlock(&rwl);
42 my_atomic_add32(&bad, x);
43 my_atomic_rwlock_wrunlock(&rwl);
45 my_atomic_rwlock_wrlock(&rwl);
46 my_atomic_add32(&bad, -x);
47 my_atomic_rwlock_wrunlock(&rwl);
57 pthread_handler_t test_atomic_add64(
void *arg)
59 int m= (*(
int *)arg)/2;
61 for (x= ((int64)(intptr)(&m)); m ; m--)
63 x= (x*m+0xfdecba987654321LL) & INT_MAX64;
64 my_atomic_rwlock_wrlock(&rwl);
65 my_atomic_add64(&a64, x);
66 my_atomic_rwlock_wrunlock(&rwl);
68 my_atomic_rwlock_wrlock(&rwl);
69 my_atomic_add64(&a64, -x);
70 my_atomic_rwlock_wrunlock(&rwl);
73 if (!--running_threads)
91 pthread_handler_t test_atomic_fas(
void *arg)
96 my_atomic_rwlock_wrlock(&rwl);
97 x= my_atomic_add32(&b32, 1);
98 my_atomic_rwlock_wrunlock(&rwl);
100 my_atomic_rwlock_wrlock(&rwl);
101 my_atomic_add32(&bad, x);
102 my_atomic_rwlock_wrunlock(&rwl);
106 my_atomic_rwlock_wrlock(&rwl);
107 x= my_atomic_fas32(&c32, x);
108 my_atomic_rwlock_wrunlock(&rwl);
113 my_atomic_rwlock_wrlock(&rwl);
114 x= my_atomic_fas32(&c32, x);
115 my_atomic_rwlock_wrunlock(&rwl);
118 my_atomic_rwlock_wrlock(&rwl);
119 my_atomic_add32(&bad, -x);
120 my_atomic_rwlock_wrunlock(&rwl);
133 pthread_handler_t test_atomic_cas(
void *arg)
135 int m= (*(
int *)arg)/2,
ok= 0;
137 for (x= ((
int)(intptr)(&m)); m ; m--)
139 my_atomic_rwlock_wrlock(&rwl);
140 y= my_atomic_load32(&bad);
141 my_atomic_rwlock_wrunlock(&rwl);
142 x= (x*m+0x87654321) & INT_MAX32;
144 my_atomic_rwlock_wrlock(&rwl);
145 ok= my_atomic_cas32(&bad, &y, (uint32)y+x);
146 my_atomic_rwlock_wrunlock(&rwl);
149 my_atomic_rwlock_wrlock(&rwl);
150 ok= my_atomic_cas32(&bad, &y, y-x);
151 my_atomic_rwlock_wrunlock(&rwl);
163 bad= my_atomic_initialize();
164 EXPECT_FALSE(bad) <<
"my_atomic_initialize() returned";
166 my_atomic_rwlock_init(&rwl);
169 test_concurrently(
"my_atomic_add32", test_atomic_add, THREADS, CYCLES);
171 test_concurrently(
"my_atomic_fas32", test_atomic_fas, THREADS, CYCLES);
173 test_concurrently(
"my_atomic_cas32", test_atomic_cas, THREADS, CYCLES);
183 volatile int64 b=0x1000200030004000LL;
185 my_atomic_add64(&a64, b);
186 EXPECT_EQ(a64, b) <<
"add64";
189 test_concurrently(
"my_atomic_add64", test_atomic_add64, THREADS, CYCLES);
191 my_atomic_rwlock_destroy(&rwl);
199 pthread_attr_init(&thr_attr);
200 pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
206 pthread_attr_destroy(&thr_attr);