3 #include <cds/gc/dhp.h>
5 #include <cds/sync/spinlock.h>
6 #include <cds/misc/ticket_lock.h>
7 #include <cds_test/stress_test.h>
17 typedef cds_others::TicketLock TicketLock;
18 typedef cds::sync::spin SpinLock;
19 typedef cds::sync::reentrant_spin32 Reentrant32;
20 typedef cds::sync::reentrant_spin64 Reentrant64;
21 static size_t s_nSpinLockThreadCount = 6;
22 static size_t s_nSpinLockPassCount = 2500000000;
23 static size_t s_nTicketLockPassCount = 4000000;
25 #define TASK(lock_type, lock_ptr, pass_cnt) \
26 static void Thread##lock_type() { \
27 for (size_t i = 0; i < pass_cnt; i++) { \
34 #define LOCK_TEST(lock_type, lock_ptr, pass_cnt) \
35 TEST_F(SpinLockTest, lock_type) { \
36 lock_ptr = new lock_type(); \
38 std::unique_ptr<std::thread[]>(new std::thread[s_nSpinLockThreadCount]); \
39 std::thread *threads = new std::thread[s_nSpinLockThreadCount]; \
40 for (size_t i = 0; i < s_nSpinLockThreadCount; i++) { \
41 threads[i] = std::thread(Thread##lock_type); \
43 for (size_t i = 0; i < s_nSpinLockThreadCount; i++) { \
46 if (x != s_nSpinLockThreadCount * pass_cnt) { \
47 cout << "Incorrect " << #lock_type << "\n"; \
48 cout << "x=" << x << "\nThreadCount=" << s_nSpinLockThreadCount \
49 << "\nPassCount=" << pass_cnt \
50 << "\t&&\tSupposed times=" << s_nSpinLockThreadCount * pass_cnt \
55 class SpinLockTest : public cds_test::stress_fixture {
58 static TicketLock *ticket_mutex;
59 static SpinLock *spin_mutex;
60 static Reentrant32 *reentrant_mutex32;
61 static Reentrant64 *reentrant_mutex64;
63 static void SetUpTestCase() {
64 cds_test::config const &cfg = get_config("Misc");
65 GetConfig(SpinLockThreadCount);
66 GetConfig(SpinLockPassCount);
67 GetConfig(TicketLockPassCount);
70 TASK(TicketLock, ticket_mutex, s_nTicketLockPassCount)
71 TASK(SpinLock, spin_mutex, s_nSpinLockPassCount)
72 TASK(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
73 TASK(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
76 size_t SpinLockTest::x;
77 TicketLock *SpinLockTest::ticket_mutex;
78 SpinLock *SpinLockTest::spin_mutex;
79 Reentrant32 *SpinLockTest::reentrant_mutex32;
80 Reentrant64 *SpinLockTest::reentrant_mutex64;
82 LOCK_TEST(TicketLock, ticket_mutex, s_nTicketLockPassCount)
83 LOCK_TEST(SpinLock, spin_mutex, s_nSpinLockPassCount)
84 LOCK_TEST(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
85 LOCK_TEST(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)