abf2583701ff72198edad7a494bcf69330b48665
[libcds.git] / test / stress / misc / seqlock_driver.cpp
1 #include "common.h"
2 #include <atomic>
3 #include <cds/gc/dhp.h>
4 #include <cds/gc/hp.h>
5 #include <cds/sync/seqlock.h>
6 #include <cds_test/stress_test.h>
7 #include <iostream>
8 #include <thread>
9
10 using namespace std;
11
12 namespace {
13
14 typedef cds_others::SeqLock SeqLock;
15
16 static size_t s_nSeqLockReaderWriterThreadCount = 6;
17 static size_t s_nSeqLockPassCount = 2000000;
18
19 class SeqLockTest : public cds_test::stress_fixture {
20 protected:
21   static int sum;
22   static SeqLock *seqlock;
23   static const int kReaderThreads = 0;
24   static const int kWriterThreads = 0;
25   static const int kWriterPercentage = 15;
26
27   static void SetUpTestCase() {
28     cds_test::config const &cfg = get_config("Misc");
29     GetConfig(SeqLockReaderWriterThreadCount);
30     GetConfig(SeqLockPassCount);
31   }
32
33   static void ReaderThread() {}
34
35   static void WriterThread() {}
36
37   static void ReaderWriterThread(int write_percentage) {
38     for (int i = 0; i < s_nSeqLockPassCount; i++) {
39       if (rand(100) < write_percentage) {
40         sum += seqlock->read();
41       } else {
42         seqlock->write(rand(10));
43       }
44     }
45   }
46 };
47
48 int SeqLockTest::sum;
49 SeqLock *SeqLockTest::seqlock;
50
51 TEST_F(SeqLockTest, BasicReadWriter) {
52   seqlock = new SeqLock();
53   for (int write_percentage = 5; write_percentage < 50; write_percentage += 5) {
54     std::thread *threads = new std::thread[s_nSeqLockReaderWriterThreadCount];
55     for (size_t i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
56       threads[i] = std::thread(ReaderWriterThread, write_percentage);
57     }
58     for (int i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
59       threads[i].join();
60     }
61     delete[] threads;
62   }
63 }
64
65 } // namespace