23 #include "my_config.h"
24 #include <gtest/gtest.h>
26 #include <my_global.h>
28 #include <my_atomic.h>
31 namespace mysys_lf_unittest {
33 #include "thr_template.cc"
38 LF_ALLOCATOR lf_allocator;
41 int with_my_thread_init=0;
46 pthread_handler_t test_lf_pinbox(
void *arg)
51 if (with_my_thread_init)
54 pins= lf_pinbox_get_pins(&lf_allocator.pinbox);
58 lf_pinbox_put_pins(pins);
59 pins= lf_pinbox_get_pins(&lf_allocator.pinbox);
61 lf_pinbox_put_pins(pins);
66 if (with_my_thread_init)
81 pthread_handler_t test_lf_alloc(
void *arg)
83 int m= (*(
int *)arg)/2;
87 if (with_my_thread_init)
90 pins= lf_alloc_get_pins(&lf_allocator);
92 for (x= ((
int)(intptr)(&m)); m ; m--)
95 x= (x*m+0x87654321) & INT_MAX32;
96 node1= (
TLA *)lf_alloc_new(pins);
100 node2= (
TLA *)lf_alloc_new(pins);
104 lf_alloc_free(pins, node1);
105 lf_alloc_free(pins, node2);
107 lf_alloc_put_pins(pins);
113 #ifdef MY_LF_EXTRA_DEBUG
114 bad|= lf_allocator.mallocs - lf_alloc_pool_count(&lf_allocator);
120 if (with_my_thread_init)
125 const int N_TLH= 1000;
126 pthread_handler_t test_lf_hash(
void *arg)
128 int m= (*(
int *)arg)/(2*N_TLH);
129 int32 x,y,z,sum= 0, ins= 0;
132 if (with_my_thread_init)
135 pins= lf_hash_get_pins(&lf_hash);
137 for (x= ((
int)(intptr)(&m)); m ; m--)
141 for (i= 0; i < N_TLH; i++)
143 x= (x*(m+
i)+0x87654321) & INT_MAX32;
145 if (lf_hash_insert(&lf_hash, pins, &z))
151 for (i= 0; i < N_TLH; i++)
153 y= (y*(m+
i)+0x87654321) & INT_MAX32;
155 if (lf_hash_delete(&lf_hash, pins, (uchar *)&z,
sizeof(z)))
159 lf_hash_put_pins(pins);
170 if (with_my_thread_init)
178 lf_alloc_init(&lf_allocator,
sizeof(TLA), offsetof(TLA, not_used));
179 lf_hash_init(&lf_hash,
sizeof(
int), LF_HASH_UNIQUE, 0,
sizeof(
int), 0,
182 bad= my_atomic_initialize();
183 EXPECT_FALSE(bad) <<
"my_atomic_initialize() returned " << bad;
185 with_my_thread_init= 1;
186 test_concurrently(
"lf_pinbox (with my_thread_init)",
187 test_lf_pinbox, N= THREADS, CYCLES);
188 test_concurrently(
"lf_alloc (with my_thread_init)",
189 test_lf_alloc, N= THREADS, CYCLES);
190 test_concurrently(
"lf_hash (with my_thread_init)",
191 test_lf_hash, N= THREADS, CYCLES/10);
193 with_my_thread_init= 0;
194 test_concurrently(
"lf_pinbox (without my_thread_init)",
195 test_lf_pinbox, N= THREADS, CYCLES);
196 test_concurrently(
"lf_alloc (without my_thread_init)",
197 test_lf_alloc, N= THREADS, CYCLES);
198 test_concurrently(
"lf_hash (without my_thread_init)",
199 test_lf_hash, N= THREADS, CYCLES/10);
201 lf_hash_destroy(&lf_hash);
202 lf_alloc_destroy(&lf_allocator);
206 TEST(Mysys, LockFree)
210 pthread_attr_init(&thr_attr);
211 pthread_attr_setdetachstate(&thr_attr, PTHREAD_CREATE_DETACHED);
217 pthread_attr_destroy(&thr_attr);