90ca2399489311fe3651d915d7ede2fd74f9c579
[libcds.git] / test / stress / misc / barrier_driver.cpp
1 #include "common.h"
2 #include <atomic>
3 #include <cds/gc/dhp.h>
4 #include <cds/gc/hp.h>
5 #include <cds/misc/barrier.h>
6 #include <cds_test/stress_test.h>
7 #include <iostream>
8 #include <memory>
9 #include <thread>
10
11 using namespace std;
12
13 namespace {
14
15 typedef cds_others::SpinBarrier Barrier;
16
17 static size_t s_nBarrierThreadCount = 6;
18 static size_t s_nBarrierPassCount = 100000000;
19
20 class BarrierTest : public cds_test::stress_fixture {
21 protected:
22   static Barrier *barrier;
23   static size_t count;
24
25   static void SetUpTestCase() {
26     cds_test::config const &cfg = get_config("Misc");
27     GetConfig(BarrierPassCount);
28     GetConfig(BarrierThreadCount);
29   }
30
31   static void TearDownTestCase() {
32     if (count != s_nBarrierPassCount) {
33       cout << "Incorrect" << endl;
34     }
35   }
36
37   static void Thread() {
38     for (ullong i = 0; i < s_nBarrierPassCount; i++) {
39       if (barrier->wait()) {
40         count++;
41       }
42     }
43   }
44 };
45
46 Barrier *BarrierTest::barrier;
47 size_t BarrierTest::count;
48
49 TEST_F(BarrierTest, Wait) {
50   barrier = new Barrier(s_nBarrierThreadCount);
51   std::unique_ptr<std::thread[]> threads(new std::thread[s_nBarrierThreadCount]);
52   for (size_t i = 0; i < s_nBarrierThreadCount; i++) {
53     threads[i] = std::thread(Thread);
54   }
55
56   for (size_t i = 0; i < s_nBarrierThreadCount; i++) {
57     threads[i].join();
58   }
59 }
60
61 } // namespace