438d033a88798b1949ebe1d4f9525ffa4de4093c
[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/sync/barrier.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::SpinBarrier Barrier;
15
16 static size_t s_nBarrierThreadCount = 6;
17 static size_t s_nBarrierPassCount = 100000000;
18
19 class BarrierTest : public cds_test::stress_fixture {
20 protected:
21   static Barrier *barrier;
22   static int count;
23
24   static void SetUpTestCase() {
25     cds_test::config const &cfg = get_config("Misc");
26     GetConfig(BarrierPassCount);
27     GetConfig(BarrierThreadCount);
28   }
29
30   static void TearDownTestCase() {
31     if (count != s_nBarrierPassCount) {
32       cout << "Incorrect" << endl;
33     }
34   }
35
36   static void Thread() {
37     for (ullong i = 0; i < s_nBarrierPassCount; i++) {
38       if (barrier->wait()) {
39         count++;
40       }
41     }
42   }
43 };
44
45 Barrier *BarrierTest::barrier;
46 int BarrierTest::count;
47
48 TEST_F(BarrierTest, Wait) {
49   barrier = new Barrier(s_nBarrierThreadCount);
50   std::thread *threads = new std::thread[s_nBarrierThreadCount];
51   for (int i = 0; i < s_nBarrierThreadCount; i++) {
52     threads[i] = std::thread(Thread);
53   }
54
55   for (int i = 0; i < s_nBarrierThreadCount; i++) {
56     threads[i].join();
57   }
58 }
59
60 } // namespace