8dfc41dc8fef3502d23642845f4b8e312dc10157
[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 <vector>
10 #include <thread>
11
12 using namespace std;
13
14 namespace {
15
16 typedef cds_others::SpinBarrier Barrier;
17
18 static size_t s_nBarrierThreadCount = 2;
19 static size_t s_nBarrierPassCount = 100000000;
20
21 class BarrierTest : public cds_test::stress_fixture {
22 protected:
23   static Barrier *barrier;
24   static size_t count;
25
26   static void SetUpTestCase() {
27     cds_test::config const &cfg = get_config("Misc");
28     GetConfig(BarrierPassCount);
29     GetConfig(BarrierThreadCount);
30   }
31
32   static void Thread() {
33     for (size_t i = 0; i < s_nBarrierPassCount; i++) {
34       if (barrier->wait()) {
35         count++;
36       }
37     }
38   }
39 };
40
41 Barrier *BarrierTest::barrier;
42 size_t BarrierTest::count;
43
44 TEST_F(BarrierTest, Wait) {
45   barrier = new Barrier(s_nBarrierThreadCount);
46   std::vector<std::thread> threads;
47   for (size_t i = 0; i < s_nBarrierThreadCount; i++) {
48     threads.push_back(std::thread(Thread));
49   }
50
51   for (size_t i = 0; i < s_nBarrierThreadCount; i++) {
52     threads[i].join();
53   }
54   EXPECT_EQ(count, s_nBarrierPassCount);
55   delete barrier;
56 }
57
58 } // namespace