e230de5fcde22f133fb1100ec0d975b5dbe33564
[libcds.git] / test / stress / misc / seqlock_driver.cpp
1 #include <atomic>
2 #include <cds/gc/dhp.h>
3 #include <cds/gc/hp.h>
4 #include <cds/sync/seqlock.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_others::SeqLock SeqLock;
14
15 class SeqLockTest : public cds_test::stress_fixture {
16 protected:
17   static int sum;
18   static SeqLock *seqlock;
19   static const int kReaderThreads = 0;
20   static const int kWriterThreads = 0;
21   static const int kReaderWriterThreads = 6;
22   static const int kWriterPercentage = 15;
23   static const int kPassCount = 15000;
24
25   static void SetUpTestCase() {}
26
27   static void ReaderThread() {}
28
29   static void WriterThread() {}
30
31   static void ReaderWriterThread() {
32     for (int i = 0; i < kPassCount; i++) {
33       for (int j = 0; j < kPassCount; j++) {
34         if (rand(100) < kWriterPercentage) {
35           sum += seqlock->read();
36         } else {
37           seqlock->write(rand(10));
38         }
39       }
40     }
41   }
42 };
43
44 int SeqLockTest::sum;
45 SeqLock *SeqLockTest::seqlock;
46 const int SeqLockTest::kReaderThreads;
47 const int SeqLockTest::kWriterThreads;
48 const int SeqLockTest::kReaderWriterThreads;
49
50 TEST_F(SeqLockTest, BasicReadWriter) {
51   seqlock = new SeqLock();
52   int num_threads = kReaderThreads + kWriterThreads + kReaderWriterThreads;
53   std::thread *threads = new std::thread[num_threads];
54   for (int i = 0; i < kReaderThreads; i++) {
55     threads[i] = std::thread(ReaderThread);
56   }
57   for (int i = kReaderThreads; i < (kReaderThreads + kWriterThreads); i++) {
58     threads[i] = std::thread(WriterThread);
59   }
60   for (int i = (kReaderThreads + kWriterThreads); i < num_threads; i++) {
61     threads[i] = std::thread(ReaderWriterThread);
62   }
63
64   for (int i = 0; i < num_threads; i++) {
65     threads[i].join();
66   }
67 }
68
69 } // namespace