This repository has been archived by the owner on Dec 14, 2024. It is now read-only.
forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode_threadsafe_cow.h
105 lines (80 loc) · 2.78 KB
/
node_threadsafe_cow.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef SRC_NODE_THREADSAFE_COW_H_
#define SRC_NODE_THREADSAFE_COW_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include "util.h"
#include "uv.h"
#include <memory> // std::shared_ptr<T>
#include <utility> // std::forward<T>
namespace node {
// Copy-on-write utility. Not threadsafe, i.e. there is no synchronization
// of the copy operation with other operations.
template <typename T>
class CopyOnWrite final {
public:
template <typename... Args>
explicit CopyOnWrite(Args&&... args)
: data_(std::make_shared<T>(std::forward<Args>(args)...)) {}
CopyOnWrite(const CopyOnWrite<T>& other) = default;
CopyOnWrite& operator=(const CopyOnWrite<T>& other) = default;
CopyOnWrite(CopyOnWrite<T>&& other) = default;
CopyOnWrite& operator=(CopyOnWrite<T>&& other) = default;
const T* read() const { return data_.get(); }
T* write();
const T& operator*() const { return *read(); }
const T* operator->() const { return read(); }
private:
std::shared_ptr<T> data_;
};
// Threadsafe copy-on-write utility. Consumers need to use the Read and
// Write helpers to access the target data structure.
template <typename T>
class ThreadsafeCopyOnWrite final {
private:
// Define this early since some of the public members depend on it
// and some compilers need it to be defined first in that case.
struct Impl {
explicit Impl(const T& data) : data(data) {}
explicit Impl(T&& data) : data(std::move(data)) {}
Impl(const Impl& other);
Impl& operator=(const Impl& other) = delete;
Impl(Impl&& other) = delete;
Impl& operator=(Impl&& other) = delete;
RwLock mutex;
T data;
};
public:
template <typename... Args>
ThreadsafeCopyOnWrite(Args&&... args)
: impl_(T(std::forward<Args>(args)...)) {}
ThreadsafeCopyOnWrite(const ThreadsafeCopyOnWrite<T>& other) = default;
ThreadsafeCopyOnWrite& operator=(const ThreadsafeCopyOnWrite<T>& other) =
default;
ThreadsafeCopyOnWrite(ThreadsafeCopyOnWrite<T>&& other) = default;
ThreadsafeCopyOnWrite& operator=(ThreadsafeCopyOnWrite<T>&& other) = default;
class Read {
public:
explicit Read(const ThreadsafeCopyOnWrite<T>* cow);
const T& operator*() const;
const T* operator->() const;
private:
const ThreadsafeCopyOnWrite<T>* cow_;
RwLock::ScopedReadLock lock_;
};
class Write {
public:
explicit Write(ThreadsafeCopyOnWrite<T>* cow);
T& operator*();
T* operator->();
private:
ThreadsafeCopyOnWrite<T>* cow_;
typename ThreadsafeCopyOnWrite<T>::Impl* impl_;
RwLock::ScopedLock lock_;
};
Read read() const { return Read(this); }
Write write() { return Write(this); }
private:
CopyOnWrite<Impl> impl_;
};
} // namespace node
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#endif // SRC_NODE_THREADSAFE_COW_H_