-
Notifications
You must be signed in to change notification settings - Fork 0
/
hazard_ptr.h
88 lines (76 loc) · 2.5 KB
/
hazard_ptr.h
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
/*
Just messing around atm please ignore
*/
/*
* Implementation for the hazard pointer used for various other structures
*/
#pragma once
namespace ouraglyfi
{
#include <atomic>
#include <list>
template<class T> class HazardPtrList
template<class T>
class HazardPtr {
public:
HazardPtr() noexcept
: next(nullptr), is_active(true), data_ptr(nullptr)
{}
void remember(T * ptr) noexcept {
this->data_ptr.store(ptr)
}
void release() noexcept {
this->data_ptr.store(nullptr)
this->is_active.store(false)
}
private:
friend class HazardPtrList<T>
//The next hazard pointer
HazardPtr<T> *next;
//Is at least one thread accessing this pointer
std::atomic<bool> is_active;
//The actual pointer to the data
std::atomic<T*> data_ptr;
}
/*
template<class T>
class HazardPtrList {
public:
HazardPtr<T> &acquire() noexcept {
bool inactive{false};
for(auto &ptr : hazard_pointers) {
//If the pointer is inactive
if(!ptr->is_active.load()) {
//If someone else hasnt' done it first, activate it and return it
if(ptr->is_active.compare_exchange_strong(inactive, true, std::memory_order_seq_cst) {
return *ptr;
}
}
}
//If at this point we could find no inactive(unused) hazard pointer we need to allocate a new one'
auto ptr = new HazardPtr<T>{};
static std::atomic<bool> busy = false;
while(true) {
auto not_busy = false;
//Spinlock... should be really hard to reach, alternative is to spin on a manual list
//comparing the head and the ptr...
if(alone.compare_exchange_strong(not_busy, true)) {
hazard_pointers.push_back(ptr);
return hazard_pointers.front();
}
}
}
//Checks if the list contains this pointer as an active pointer
bool contains(const T * const ptr) {
for(auto &p : hazard_pointers) {
if(p->data_ptr.load() == ptr && ptr->is_active.load()) {
return true;
}
}
return false;
}
private:
std::list< std::atomic<HazardPtr<T> *> > hazard_pointers;
}
*/
}