-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholder.hpp
59 lines (45 loc) · 1.47 KB
/
holder.hpp
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
#include "util.hpp"
#include "large_atomic.hpp"
class raw_holder {
private:
const int element_size_;
std::unique_ptr<large_atomic<intptr_t, intptr_t>[]> contents_;
public:
raw_holder(int element_size);
static constexpr int INVALID_VERSION = -1;
intptr_t get(intptr_t* value);
// Precondition: Whenever a call to update(ver+1) starts, at least one call to update(ver) has finished
// Precondition: All calls to update(ver) for a given ver have equal values
void update(intptr_t version, const intptr_t* value);
};
class raw_locked_holder {
private:
const int element_size_;
std::mutex m_;
VAR_T(intptr_t) version_;
std::unique_ptr<VAR_T(intptr_t)[]> contents_;
public:
raw_locked_holder(int element_size);
static constexpr int INVALID_VERSION = -1;
intptr_t get(intptr_t* value);
void update(intptr_t version, const intptr_t* value);
};
template<typename T, typename Raw = raw_holder> class holder {
private:
Raw holder_;
static constexpr int element_size_ = (sizeof(T) + sizeof(intptr_t) - 1) / sizeof(intptr_t);
public:
holder() : holder_(element_size_) { }
static constexpr int INVALID_VERSION = Raw::INVALID_VERSION;
intptr_t get(T* value) {
intptr_t buffer[element_size_];
intptr_t result = holder_.get(buffer);
memcpy(value, buffer, sizeof(T));
return result;
}
void update(intptr_t version, const T* value) {
intptr_t buffer[element_size_];
memcpy(buffer, value, sizeof(T));
holder_.update(version, buffer);
}
};