2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #define HAZPTR_DEBUG true
17 #define HAZPTR_STATS true
18 #define HAZPTR_SCAN_THRESHOLD 10
20 #include <folly/experimental/hazptr/test/HazptrUse1.h>
21 #include <folly/experimental/hazptr/test/HazptrUse2.h>
22 #include <folly/experimental/hazptr/example/LockFreeLIFO.h>
23 #include <folly/experimental/hazptr/example/SWMRList.h>
24 #include <folly/experimental/hazptr/example/WideCAS.h>
25 #include <folly/experimental/hazptr/debug.h>
26 #include <folly/experimental/hazptr/hazptr.h>
28 #include <folly/portability/GFlags.h>
29 #include <folly/portability/GTest.h>
33 DEFINE_int32(num_threads, 5, "Number of threads");
34 DEFINE_int64(num_reps, 1, "Number of test reps");
35 DEFINE_int64(num_ops, 10, "Number of ops or pairs of ops per rep");
37 using namespace folly::hazptr;
39 class HazptrTest : public testing::Test {
41 HazptrTest() : Test() {
42 DEBUG_PRINT("========== start of test scope");
44 ~HazptrTest() override {
45 DEBUG_PRINT("========== end of test scope");
49 TEST_F(HazptrTest, Test1) {
51 Node1* node0 = (Node1*)malloc(sizeof(Node1));
52 DEBUG_PRINT("=== new node0 " << node0 << " " << sizeof(*node0));
53 Node1* node1 = (Node1*)malloc(sizeof(Node1));
54 DEBUG_PRINT("=== malloc node1 " << node1 << " " << sizeof(*node1));
55 Node1* node2 = (Node1*)malloc(sizeof(Node1));
56 DEBUG_PRINT("=== malloc node2 " << node2 << " " << sizeof(*node2));
57 Node1* node3 = (Node1*)malloc(sizeof(Node1));
58 DEBUG_PRINT("=== malloc node3 " << node3 << " " << sizeof(*node3));
62 std::atomic<Node1*> shared0 = {node0};
63 std::atomic<Node1*> shared1 = {node1};
64 std::atomic<Node1*> shared2 = {node2};
65 std::atomic<Node1*> shared3 = {node3};
67 MyMemoryResource myMr;
68 DEBUG_PRINT("=== myMr " << &myMr);
69 hazptr_domain myDomain0;
70 DEBUG_PRINT("=== myDomain0 " << &myDomain0);
71 hazptr_domain myDomain1(&myMr);
72 DEBUG_PRINT("=== myDomain1 " << &myDomain1);
76 DEBUG_PRINT("=== hptr0");
77 hazptr_owner<Node1> hptr0;
78 DEBUG_PRINT("=== hptr1");
79 hazptr_owner<Node1> hptr1(myDomain0);
80 DEBUG_PRINT("=== hptr2");
81 hazptr_owner<Node1> hptr2(myDomain1);
82 DEBUG_PRINT("=== hptr3");
83 hazptr_owner<Node1> hptr3;
87 Node1* n0 = shared0.load();
88 Node1* n1 = shared1.load();
89 Node1* n2 = shared2.load();
90 Node1* n3 = shared3.load();
92 if (hptr0.try_protect(n0, shared0)) {}
93 if (hptr1.try_protect(n1, shared1)) {}
96 if (hptr2.try_protect(n3, shared3)) {}
102 DEBUG_PRINT("=== retire n0 " << n0);
104 DEBUG_PRINT("=== retire n1 " << n1);
105 n1->retire(default_hazptr_domain());
106 DEBUG_PRINT("=== retire n2 " << n2);
107 n2->retire(myDomain0);
108 DEBUG_PRINT("=== retire n3 " << n3);
109 n3->retire(myDomain1);
112 TEST_F(HazptrTest, Test2) {
113 Node2* node0 = new Node2;
114 DEBUG_PRINT("=== new node0 " << node0 << " " << sizeof(*node0));
115 Node2* node1 = (Node2*)malloc(sizeof(Node2));
116 DEBUG_PRINT("=== malloc node1 " << node1 << " " << sizeof(*node1));
117 Node2* node2 = (Node2*)malloc(sizeof(Node2));
118 DEBUG_PRINT("=== malloc node2 " << node2 << " " << sizeof(*node2));
119 Node2* node3 = (Node2*)malloc(sizeof(Node2));
120 DEBUG_PRINT("=== malloc node3 " << node3 << " " << sizeof(*node3));
124 std::atomic<Node2*> shared0 = {node0};
125 std::atomic<Node2*> shared1 = {node1};
126 std::atomic<Node2*> shared2 = {node2};
127 std::atomic<Node2*> shared3 = {node3};
129 MineMemoryResource mineMr;
130 DEBUG_PRINT("=== mineMr " << &mineMr);
131 hazptr_domain mineDomain0;
132 DEBUG_PRINT("=== mineDomain0 " << &mineDomain0);
133 hazptr_domain mineDomain1(&mineMr);
134 DEBUG_PRINT("=== mineDomain1 " << &mineDomain1);
138 DEBUG_PRINT("=== hptr0");
139 hazptr_owner<Node2> hptr0;
140 DEBUG_PRINT("=== hptr1");
141 hazptr_owner<Node2> hptr1(mineDomain0);
142 DEBUG_PRINT("=== hptr2");
143 hazptr_owner<Node2> hptr2(mineDomain1);
144 DEBUG_PRINT("=== hptr3");
145 hazptr_owner<Node2> hptr3;
149 Node2* n0 = shared0.load();
150 Node2* n1 = shared1.load();
151 Node2* n2 = shared2.load();
152 Node2* n3 = shared3.load();
154 if (hptr0.try_protect(n0, shared0)) {}
155 if (hptr1.try_protect(n1, shared1)) {}
158 if (hptr2.try_protect(n3, shared3)) {}
164 DEBUG_PRINT("=== retire n0 " << n0);
165 n0->retire(default_hazptr_domain(), &mineReclaimFnDelete);
166 DEBUG_PRINT("=== retire n1 " << n1);
167 n1->retire(default_hazptr_domain(), &mineReclaimFnFree);
168 DEBUG_PRINT("=== retire n2 " << n2);
169 n2->retire(mineDomain0, &mineReclaimFnFree);
170 DEBUG_PRINT("=== retire n3 " << n3);
171 n3->retire(mineDomain1, &mineReclaimFnFree);
174 TEST_F(HazptrTest, LIFO) {
176 CHECK_GT(FLAGS_num_threads, 0);
177 for (int i = 0; i < FLAGS_num_reps; ++i) {
178 DEBUG_PRINT("========== start of rep scope");
180 std::vector<std::thread> threads(FLAGS_num_threads);
181 for (int tid = 0; tid < FLAGS_num_threads; ++tid) {
182 threads[tid] = std::thread([&s, tid]() {
183 for (int j = tid; j < FLAGS_num_ops; j += FLAGS_num_threads) {
186 while (!s.pop(res)) {}
190 for (auto& t : threads) {
193 DEBUG_PRINT("========== end of rep scope");
197 TEST_F(HazptrTest, SWMRLIST) {
199 hazptr_domain custom_domain;
201 CHECK_GT(FLAGS_num_threads, 0);
202 for (int i = 0; i < FLAGS_num_reps; ++i) {
203 DEBUG_PRINT("========== start of rep scope");
204 SWMRListSet<T> s(custom_domain);
205 std::vector<std::thread> threads(FLAGS_num_threads);
206 for (int tid = 0; tid < FLAGS_num_threads; ++tid) {
207 threads[tid] = std::thread([&s, tid]() {
208 for (int j = tid; j < FLAGS_num_ops; j += FLAGS_num_threads) {
213 for (int j = 0; j < 10; ++j) {
216 for (int j = 0; j < 10; ++j) {
219 for (auto& t : threads) {
222 DEBUG_PRINT("========== end of rep scope");
226 TEST_F(HazptrTest, WIDECAS) {
229 std::string v = "11112222";
230 auto ret = s.cas(u, v);
246 TEST_F(HazptrTest, VirtualTest) {
247 struct Thing : public hazptr_obj_base<Thing> {
249 DEBUG_PRINT("this: " << this << " &a: " << &a << " a: " << a);
253 for (int i = 0; i < 100; i++) {
254 auto bar = new Thing;
257 hazptr_owner<Thing> hptr;
260 EXPECT_EQ(bar->a, i);
264 TEST_F(HazptrTest, DestructionTest) {
265 hazptr_domain myDomain0;
266 struct Thing : public hazptr_obj_base<Thing> {
268 Thing(Thing* n) : next(n) {}
270 DEBUG_PRINT("this: " << this << " next: " << next);
276 Thing* last{nullptr};
277 for (int i = 0; i < 2000; i++) {
278 last = new Thing(last);