-
Notifications
You must be signed in to change notification settings - Fork 27
/
shared_guarded.hpp
207 lines (172 loc) · 4.93 KB
/
shared_guarded.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
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
/***********************************************************************
*
* Copyright (c) 2015-2018 Ansel Sermersheim
* All rights reserved.
*
* This file is part of libguarded
*
* libguarded is free software, released under the BSD 2-Clause license.
* For license details refer to LICENSE provided with this project.
*
***********************************************************************/
#ifndef LIBGUARDED_SHARED_GUARDED_HPP
#define LIBGUARDED_SHARED_GUARDED_HPP
#include <memory>
#if HAVE_CXX14
#include <shared_mutex>
#else
namespace std
{
class shared_timed_mutex;
}
#endif
namespace libguarded
{
/**
\headerfile shared_guarded.hpp <libguarded/shared_guarded.hpp>
This templated class wraps an object and allows only one thread at
a time to modify the protected object.
This class will use std::shared_timed_mutex for the internal
locking mechanism by default. In C++17 the std::shared_mutex class
is also available.
The handle returned by the various lock methods is moveable but not
copyable.
*/
template <typename T, typename M = std::shared_timed_mutex>
class shared_guarded
{
private:
class deleter;
class shared_deleter;
public:
using handle = std::unique_ptr<T, deleter>;
using shared_handle = std::unique_ptr<const T, shared_deleter>;
template <typename... Us>
shared_guarded(Us &&... data);
// exclusive access
handle lock();
handle try_lock();
template <class Duration>
handle try_lock_for(const Duration & duration);
template <class TimePoint>
handle try_lock_until(const TimePoint & timepoint);
// shared access, note "shared" in method names
shared_handle lock_shared() const;
shared_handle try_lock_shared() const;
template <class Duration>
shared_handle try_lock_shared_for(const Duration & duration) const;
template <class TimePoint>
shared_handle try_lock_shared_until(const TimePoint & timepoint) const;
private:
class deleter
{
public:
using pointer = T *;
deleter(M & mutex) : m_deleter_mutex(mutex)
{
}
void operator()(T * ptr)
{
if (ptr) {
m_deleter_mutex.unlock();
}
}
private:
M & m_deleter_mutex;
};
class shared_deleter
{
public:
using pointer = const T *;
shared_deleter(M & mutex) : m_deleter_mutex(mutex)
{
}
void operator()(const T * ptr)
{
if (ptr) {
m_deleter_mutex.unlock_shared();
}
}
private:
M & m_deleter_mutex;
};
T m_obj;
mutable M m_mutex;
};
template <typename T, typename M>
template <typename... Us>
shared_guarded<T, M>::shared_guarded(Us &&... data) : m_obj(std::forward<Us>(data)...)
{
}
template <typename T, typename M>
auto shared_guarded<T, M>::lock() -> handle
{
m_mutex.lock();
return handle(&m_obj, deleter(m_mutex));
}
template <typename T, typename M>
auto shared_guarded<T, M>::try_lock() -> handle
{
if (m_mutex.try_lock()) {
return handle(&m_obj, deleter(m_mutex));
} else {
return handle(nullptr, deleter(m_mutex));
}
}
template <typename T, typename M>
template <typename Duration>
auto shared_guarded<T, M>::try_lock_for(const Duration & duration) -> handle
{
if (m_mutex.try_lock_for(duration)) {
return handle(&m_obj, deleter(m_mutex));
} else {
return handle(nullptr, deleter(m_mutex));
}
}
template <typename T, typename M>
template <typename TimePoint>
auto shared_guarded<T, M>::try_lock_until(const TimePoint & timepoint) -> handle
{
if (m_mutex.try_lock_until(timepoint)) {
return handle(&m_obj, deleter(m_mutex));
} else {
return handle(nullptr, deleter(m_mutex));
}
}
template <typename T, typename M>
auto shared_guarded<T, M>::lock_shared() const -> shared_handle
{
m_mutex.lock_shared();
return shared_handle(&m_obj, shared_deleter(m_mutex));
}
template <typename T, typename M>
auto shared_guarded<T, M>::try_lock_shared() const -> shared_handle
{
if (m_mutex.try_lock_shared()) {
return shared_handle(&m_obj, shared_deleter(m_mutex));
} else {
return shared_handle(nullptr, shared_deleter(m_mutex));
}
}
template <typename T, typename M>
template <typename Duration>
auto shared_guarded<T, M>::try_lock_shared_for(const Duration & d) const -> shared_handle
{
if (m_mutex.try_lock_shared_for(d)) {
return shared_handle(&m_obj, shared_deleter(m_mutex));
} else {
return shared_handle(nullptr, shared_deleter(m_mutex));
}
}
template <typename T, typename M>
template <typename TimePoint>
auto shared_guarded<T, M>::try_lock_shared_until(const TimePoint & tp) const -> shared_handle
{
if (m_mutex.try_lock_shared_until(tp)) {
return shared_handle(&m_obj, shared_deleter(m_mutex));
} else {
return shared_handle(nullptr, shared_deleter(m_mutex));
}
}
}
#endif