/
ordered_guarded.hpp
211 lines (176 loc) · 5.86 KB
/
ordered_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
208
209
210
211
/***********************************************************************
*
* 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_ORDERED_GUARDED_HPP
#define LIBGUARDED_ORDERED_GUARDED_HPP
#include <memory>
#include <mutex>
#include <type_traits>
#if HAVE_CXX14
#include <shared_mutex>
#else
namespace std
{
class shared_timed_mutex;
}
#endif
namespace libguarded
{
/**
\headerfile ordered_guarded.hpp <libguarded/ordered_guarded.hpp>
This templated class wraps an object. The protected object may be
read by any number of threads simultaneously, but only one thread
may modify the object at a time.
This class will use std::shared_timed_mutex for the internal
locking mechanism by default. In C++17, the class std::shared_mutex
is available as well.
The handle returned by the various lock methods is moveable but not
copyable.
*/
template <typename T, typename M = std::shared_timed_mutex>
class ordered_guarded
{
private:
class shared_deleter;
public:
using shared_handle = std::unique_ptr<const T, shared_deleter>;
/**
Construct a guarded object. This constructor will accept any number
of parameters, all of which are forwarded to the constructor of T.
*/
template <typename... Us>
ordered_guarded(Us &&... data);
template <typename Func>
typename std::enable_if<
std::is_same<decltype(std::declval<Func>()(std::declval<T &>())), void>::value, void>::type
modify(Func &&func);
template <typename Func>
typename std::enable_if<
!std::is_same<decltype(std::declval<Func>()(std::declval<T &>())), void>::value,
decltype(std::declval<Func>()(std::declval<T &>()))>::type
modify(Func &&func);
template <typename Func>
typename std::enable_if<
std::is_same<decltype(std::declval<Func>()(std::declval<const T &>())), void>::value,
void>::type
read(Func &&func) const;
template <typename Func>
typename std::enable_if<
!std::is_same<decltype(std::declval<Func>()(std::declval<const T &>())), void>::value,
decltype(std::declval<Func>()(std::declval<const T &>()))>::type
read(Func &&func) const;
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 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>
ordered_guarded<T, M>::ordered_guarded(Us &&... data) : m_obj(std::forward<Us>(data)...)
{
}
template <typename T, typename M>
template <typename Func>
typename std::enable_if<
std::is_same<decltype(std::declval<Func>()(std::declval<T &>())), void>::value, void>::type
ordered_guarded<T, M>::modify(Func &&func)
{
std::lock_guard<M> lock(m_mutex);
func(m_obj);
}
template <typename T, typename M>
template <typename Func>
typename std::enable_if<
!std::is_same<decltype(std::declval<Func>()(std::declval<T &>())), void>::value,
decltype(std::declval<Func>()(std::declval<T &>()))>::type
ordered_guarded<T, M>::modify(Func &&func)
{
std::lock_guard<M> lock(m_mutex);
return func(m_obj);
}
template <typename T, typename M>
template <typename Func>
typename std::enable_if<
std::is_same<decltype(std::declval<Func>()(std::declval<const T &>())), void>::value,
void>::type
ordered_guarded<T, M>::read(Func &&func) const
{
std::shared_lock<M> lock(m_mutex);
func(m_obj);
}
template <typename T, typename M>
template <typename Func>
typename std::enable_if<
!std::is_same<decltype(std::declval<Func>()(std::declval<const T &>())), void>::value,
decltype(std::declval<Func>()(std::declval<const T &>()))>::type
ordered_guarded<T, M>::read(Func &&func) const
{
std::shared_lock<M> lock(m_mutex);
return func(m_obj);
}
template <typename T, typename M>
auto ordered_guarded<T, M>::lock_shared() const -> shared_handle
{
m_mutex.lock_shared();
return std::unique_ptr<const T, shared_deleter>(&m_obj, shared_deleter(m_mutex));
}
template <typename T, typename M>
auto ordered_guarded<T, M>::try_lock_shared() const -> shared_handle
{
if (m_mutex.try_lock_shared()) {
return std::unique_ptr<const T, shared_deleter>(&m_obj, shared_deleter(m_mutex));
} else {
return std::unique_ptr<const T, shared_deleter>(nullptr, shared_deleter(m_mutex));
}
}
template <typename T, typename M>
template <typename Duration>
auto ordered_guarded<T, M>::try_lock_shared_for(const Duration &duration) const -> shared_handle
{
if (m_mutex.try_lock_shared_for(duration)) {
return std::unique_ptr<const T, shared_deleter>(&m_obj, shared_deleter(m_mutex));
} else {
return std::unique_ptr<const T, shared_deleter>(nullptr, shared_deleter(m_mutex));
}
}
template <typename T, typename M>
template <typename TimePoint>
auto ordered_guarded<T, M>::try_lock_shared_until(const TimePoint &timepoint) const -> shared_handle
{
if (m_mutex.try_lock_shared_until(timepoint)) {
return std::unique_ptr<const T, shared_deleter>(&m_obj, shared_deleter(m_mutex));
} else {
return std::unique_ptr<const T, shared_deleter>(nullptr, shared_deleter(m_mutex));
}
}
}
#endif