-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhazard_pointer_sample.cpp
More file actions
278 lines (222 loc) · 9.21 KB
/
hazard_pointer_sample.cpp
File metadata and controls
278 lines (222 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// BSD 3-Clause License
// Copyright (c) 2024, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
/**
* @file hazard_pointer_sample.cpp
* @brief Hazard pointer usage for safe lock-free memory reclamation
* @example hazard_pointer_sample.cpp
*
* Demonstrates basic hazard pointer acquisition and retirement, a
* concurrent lock-free stack using hazard pointers, and a memory
* safety test with concurrent reader/writer threads.
*
* @see hazard_pointer_manager, hazard_pointer
*/
#include "thread_base/lockfree/memory/hazard_pointer.h"
#include "logger/core/logger.h"
#include <thread>
#include <vector>
#include <random>
#include <chrono>
#include <atomic>
using namespace kcenon::thread;
struct TestNode {
std::atomic<int> data{0};
std::atomic<TestNode*> next{nullptr};
explicit TestNode(int value) : data(value) {}
};
class LockFreeStack {
private:
std::atomic<TestNode*> head_{nullptr};
hazard_pointer_manager& hp_manager_;
public:
explicit LockFreeStack(hazard_pointer_manager& hp_mgr) : hp_manager_(hp_mgr) {}
~LockFreeStack() {
while (auto* node = head_.load()) {
head_.store(node->next.load());
delete node;
}
}
void push(int value) {
auto* new_node = new TestNode(value);
auto* old_head = head_.load();
do {
new_node->next.store(old_head);
} while (!head_.compare_exchange_weak(old_head, new_node));
}
bool pop(int& result) {
auto hp = hp_manager_.acquire();
while (true) {
auto* head = hp.protect(head_);
if (!head) {
return false; // Stack is empty
}
auto* next = head->next.load();
// Try to update head
if (head_.compare_exchange_weak(head, next)) {
result = head->data.load();
hp_manager_.retire(head);
return true;
}
}
}
};
void demonstrate_basic_usage() {
log_module::write_information("\n=== Basic Hazard Pointer Usage Demo ===");
hazard_pointer_manager hp_manager(4, 2); // 4 threads, 2 pointers per thread
// Show initial statistics
auto stats = hp_manager.get_statistics();
log_module::write_information("Initial statistics:");
log_module::write_information(" Active hazard pointers: {}", stats.active_hazard_pointers);
log_module::write_information(" Retired list size: {}", stats.retired_list_size);
log_module::write_information(" Total retired: {}", stats.total_retired);
log_module::write_information(" Total reclaimed: {}", stats.total_reclaimed);
// Create a simple atomic pointer
std::atomic<TestNode*> test_ptr{new TestNode(42)};
{
// Acquire hazard pointer and protect the object
auto hp = hp_manager.acquire();
auto* protected_ptr = hp.protect(test_ptr);
log_module::write_information("Protected pointer value: {}", protected_ptr->data.load());
// The hazard pointer automatically clears when going out of scope
}
// Retire the object
auto* node_to_retire = test_ptr.exchange(nullptr);
hp_manager.retire(node_to_retire);
// Force reclamation
hp_manager.scan_and_reclaim();
// Show final statistics
stats = hp_manager.get_statistics();
log_module::write_information("Final statistics:");
log_module::write_information(" Active hazard pointers: {}", stats.active_hazard_pointers);
log_module::write_information(" Retired list size: {}", stats.retired_list_size);
log_module::write_information(" Total retired: {}", stats.total_retired);
log_module::write_information(" Total reclaimed: {}", stats.total_reclaimed);
}
void demonstrate_concurrent_access() {
log_module::write_information("\n=== Concurrent Access Demo ===");
constexpr int NUM_THREADS = 4;
constexpr int OPERATIONS_PER_THREAD = 1000;
hazard_pointer_manager hp_manager(NUM_THREADS, 2);
LockFreeStack stack(hp_manager);
// Fill the stack initially
for (int i = 0; i < 100; ++i) {
stack.push(i);
}
std::atomic<int> push_count{0};
std::atomic<int> pop_count{0};
std::atomic<int> failed_pops{0};
auto start_time = std::chrono::high_resolution_clock::now();
std::vector<std::thread> threads;
threads.reserve(NUM_THREADS);
// Create worker threads
for (int thread_id = 0; thread_id < NUM_THREADS; ++thread_id) {
threads.emplace_back([&, thread_id]() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 1);
for (int op = 0; op < OPERATIONS_PER_THREAD; ++op) {
if (dis(gen) == 0) {
// Push operation
int value = thread_id * OPERATIONS_PER_THREAD + op;
stack.push(value);
push_count.fetch_add(1);
} else {
// Pop operation
int result;
if (stack.pop(result)) {
pop_count.fetch_add(1);
} else {
failed_pops.fetch_add(1);
}
}
}
});
}
// Wait for all threads to complete
for (auto& thread : threads) {
thread.join();
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
log_module::write_information("Concurrent operations completed in {} ms", duration.count());
log_module::write_information("Push operations: {}", push_count.load());
log_module::write_information("Successful pop operations: {}", pop_count.load());
log_module::write_information("Failed pop operations: {}", failed_pops.load());
// Final statistics
auto stats = hp_manager.get_statistics();
log_module::write_information("Final hazard pointer statistics:");
log_module::write_information(" Active hazard pointers: {}", stats.active_hazard_pointers);
log_module::write_information(" Retired list size: {}", stats.retired_list_size);
log_module::write_information(" Total retired: {}", stats.total_retired);
log_module::write_information(" Total reclaimed: {}", stats.total_reclaimed);
}
void demonstrate_memory_safety() {
log_module::write_information("\n=== Memory Safety Demo ===");
hazard_pointer_manager hp_manager(2, 1);
std::atomic<TestNode*> shared_ptr{new TestNode(123)};
std::atomic<bool> reader_done{false};
std::atomic<bool> writer_done{false};
// Reader thread - tries to access the shared object
std::thread reader([&]() {
auto hp = hp_manager.acquire();
for (int i = 0; i < 100; ++i) {
auto* protected_ptr = hp.protect(shared_ptr);
if (protected_ptr) {
// Safe to access the object
volatile int value = protected_ptr->data.load();
(void)value; // Prevent optimization
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
hp.clear();
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
reader_done = true;
});
// Writer thread - tries to replace and retire the shared object
std::thread writer([&]() {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // Let reader start
for (int i = 0; i < 10; ++i) {
auto* new_node = new TestNode(456 + i);
auto* old_node = shared_ptr.exchange(new_node);
if (old_node) {
hp_manager.retire(old_node);
}
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
writer_done = true;
});
reader.join();
writer.join();
// Clean up remaining object
auto* final_node = shared_ptr.exchange(nullptr);
if (final_node) {
hp_manager.retire(final_node);
}
hp_manager.scan_and_reclaim();
auto stats = hp_manager.get_statistics();
log_module::write_information("Memory safety test completed safely!");
log_module::write_information("Final statistics:");
log_module::write_information(" Total retired: {}", stats.total_retired);
log_module::write_information(" Total reclaimed: {}", stats.total_reclaimed);
}
int main() {
// Initialize logger
log_module::start();
log_module::console_target(log_module::log_types::Information);
log_module::write_information("Hazard Pointer Manager Sample");
log_module::write_information("=============================");
try {
demonstrate_basic_usage();
demonstrate_concurrent_access();
demonstrate_memory_safety();
log_module::write_information("\n=== All demos completed successfully! ===");
} catch (const std::exception& e) {
log_module::write_error("Error: {}", e.what());
log_module::stop();
return 1;
}
// Cleanup logger
log_module::stop();
return 0;
}