Adds misc benchmarks
[libcds.git] / test / stress / misc / spinlock_driver.cpp
1 #include <atomic>
2 #include <cds/gc/dhp.h>
3 #include <cds/gc/hp.h>
4 #include <cds/sync/spinlock.h>
5 #include <cds_test/stress_test.h>
6 #include <iostream>
7 #include <thread>
8
9 using namespace std;
10
11 namespace {
12
13 typedef cds::sync::spin SpinLock;
14 typedef cds::sync::reentrant_spin32 Reentrant32;
15 typedef cds::sync::reentrant_spin64 Reentrant64;
16
17 #define TASK(lock_type, lock_ptr) \
18   static void Thread ## lock_type() { \
19     for (int i = 0; i < 100000; i++) { \
20       for (int j = 0; j < 30000; j++) { \
21         lock_ptr->lock(); \
22         x = i + j; \
23         lock_ptr->unlock(); \
24       } \
25     } \
26   }
27
28 #define LOCK_TEST(lock_type, lock_ptr) \
29   TEST_F(SpinLockTest, lock_type) { \
30   lock_ptr = new lock_type(); \
31   std::thread threads[kThreads]; \
32   for (int i = 0; i < kThreads; i++) { \
33     threads[i] = std::thread(Thread ## lock_type); \
34   } \
35   for (int i = 0; i < kThreads; i++) { \
36     threads[i].join(); \
37   } \
38 }
39
40 class SpinLockTest : public cds_test::stress_fixture {
41 protected:
42   static int x;
43   static SpinLock* spin_mutex;
44   static Reentrant32* reentrant_mutex32;
45   static Reentrant64* reentrant_mutex64;
46
47   static const int kThreads = 6;
48
49   static void SetUpTestCase() {}
50
51   TASK(SpinLock, spin_mutex)
52   TASK(Reentrant32, reentrant_mutex32)
53   TASK(Reentrant64, reentrant_mutex64)
54 };
55
56 int SpinLockTest::x;
57 const int SpinLockTest::kThreads;
58 SpinLock* SpinLockTest::spin_mutex;
59 Reentrant32* SpinLockTest::reentrant_mutex32;
60 Reentrant64* SpinLockTest::reentrant_mutex64;
61
62 LOCK_TEST(SpinLock, spin_mutex)
63 LOCK_TEST(Reentrant32, reentrant_mutex32)
64 LOCK_TEST(Reentrant64, reentrant_mutex64)
65
66 } // namespace