+#include "common.h"
#include <atomic>
#include <cds/gc/dhp.h>
#include <cds/gc/hp.h>
#include <cds/sync/spinlock.h>
+#include <cds/sync/ticket_lock.h>
#include <cds_test/stress_test.h>
#include <iostream>
+#include <iostream>
#include <thread>
using namespace std;
namespace {
+typedef cds_others::TicketLock TicketLock;
typedef cds::sync::spin SpinLock;
typedef cds::sync::reentrant_spin32 Reentrant32;
typedef cds::sync::reentrant_spin64 Reentrant64;
+static size_t s_nSpinLockThreadCount = 6;
+static size_t s_nSpinLockPassCount = 2500000000;
+static size_t s_nTicketLockPassCount = 4000000;
-#define TASK(lock_type, lock_ptr) \
- static void Thread ## lock_type() { \
- for (int i = 0; i < 100000; i++) { \
- for (int j = 0; j < 30000; j++) { \
- lock_ptr->lock(); \
- x = i + j; \
- lock_ptr->unlock(); \
- } \
- } \
+#define TASK(lock_type, lock_ptr, pass_cnt) \
+ static void Thread##lock_type() { \
+ for (int i = 0; i < pass_cnt; i++) { \
+ for (int j = 0; j < pass_cnt; j++) { \
+ lock_ptr->lock(); \
+ x = i + j; \
+ lock_ptr->unlock(); \
+ } \
+ } \
}
-#define LOCK_TEST(lock_type, lock_ptr) \
- TEST_F(SpinLockTest, lock_type) { \
- lock_ptr = new lock_type(); \
- std::thread threads[kThreads]; \
- for (int i = 0; i < kThreads; i++) { \
- threads[i] = std::thread(Thread ## lock_type); \
- } \
- for (int i = 0; i < kThreads; i++) { \
- threads[i].join(); \
- } \
-}
+#define LOCK_TEST(lock_type, lock_ptr) \
+ TEST_F(SpinLockTest, lock_type) { \
+ lock_ptr = new lock_type(); \
+ std::thread *threads = new std::thread[s_nSpinLockThreadCount]; \
+ for (int i = 0; i < s_nSpinLockThreadCount; i++) { \
+ threads[i] = std::thread(Thread##lock_type); \
+ } \
+ for (int i = 0; i < s_nSpinLockThreadCount; i++) { \
+ threads[i].join(); \
+ } \
+ }
class SpinLockTest : public cds_test::stress_fixture {
protected:
static int x;
- static SpinLock* spin_mutex;
- static Reentrant32* reentrant_mutex32;
- static Reentrant64* reentrant_mutex64;
+ static TicketLock *ticket_mutex;
+ static SpinLock *spin_mutex;
+ static Reentrant32 *reentrant_mutex32;
+ static Reentrant64 *reentrant_mutex64;
- static const int kThreads = 6;
-
- static void SetUpTestCase() {}
+ static void SetUpTestCase() {
+ cds_test::config const &cfg = get_config("Misc");
+ GetConfig(SpinLockThreadCount);
+ GetConfig(SpinLockPassCount);
+ GetConfig(TicketLockPassCount);
+ }
- TASK(SpinLock, spin_mutex)
- TASK(Reentrant32, reentrant_mutex32)
- TASK(Reentrant64, reentrant_mutex64)
+ TASK(TicketLock, ticket_mutex, s_nTicketLockPassCount)
+ TASK(SpinLock, spin_mutex, s_nSpinLockPassCount)
+ TASK(Reentrant32, reentrant_mutex32, s_nSpinLockPassCount)
+ TASK(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
};
int SpinLockTest::x;
-const int SpinLockTest::kThreads;
-SpinLock* SpinLockTest::spin_mutex;
-Reentrant32* SpinLockTest::reentrant_mutex32;
-Reentrant64* SpinLockTest::reentrant_mutex64;
+TicketLock *SpinLockTest::ticket_mutex;
+SpinLock *SpinLockTest::spin_mutex;
+Reentrant32 *SpinLockTest::reentrant_mutex32;
+Reentrant64 *SpinLockTest::reentrant_mutex64;
+LOCK_TEST(TicketLock, ticket_mutex)
LOCK_TEST(SpinLock, spin_mutex)
LOCK_TEST(Reentrant32, reentrant_mutex32)
LOCK_TEST(Reentrant64, reentrant_mutex64)