SPH
src
core
objects
wrappers
Locking.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
common/Assert.h
"
4
#include <mutex>
5
6
NAMESPACE_SPH_BEGIN
7
11
template
<
typename
T,
typename
TMutex = std::mutex>
12
class
Locking
{
13
private
:
14
T value;
15
TMutex mutex;
16
17
public
:
18
Locking
() =
default
;
19
20
Locking
(
const
T& value)
21
: value(value) {}
22
23
Locking
(T&& value)
24
: value(
std
::move(value)) {}
25
26
~Locking
() {
27
// make sure all the proxies are destroyed before killing the object
28
std::unique_lock<TMutex>
lock
(mutex);
29
}
30
31
class
Proxy
{
32
template
<
typename
,
typename
>
33
friend
class
Locking
;
34
35
private
:
36
T& value;
37
std::unique_lock<TMutex>
lock
;
38
39
// private constructor, to avoid manually constructing proxies
40
Proxy
(T& value, TMutex& mutex)
41
: value(value)
42
,
lock
(mutex) {}
43
44
public
:
45
Proxy
(
Proxy
&& other)
46
: value(other.value)
47
,
lock
(
std
::move(other.
lock
)) {}
48
49
T*
operator->
()
const
{
50
return
&value;
51
}
52
53
T&
get
() {
54
return
value;
55
}
56
57
const
T&
get
()
const
{
58
return
value;
59
}
60
61
bool
isLocked
() {
62
return
lock
.owns_lock();
63
}
64
65
void
release
() {
66
if
(
lock
.owns_lock()) {
67
lock
.unlock();
68
}
69
}
70
};
71
72
Proxy
lock
() {
73
return
Proxy
(value, mutex);
74
}
75
76
Proxy
operator->
() {
77
return
this->
lock
();
78
}
79
};
80
81
NAMESPACE_SPH_END
Assert.h
Custom assertions.
NAMESPACE_SPH_BEGIN
NAMESPACE_SPH_BEGIN
Definition:
BarnesHut.cpp:13
NAMESPACE_SPH_END
#define NAMESPACE_SPH_END
Definition:
Object.h:12
Locking::Proxy
Definition:
Locking.h:31
Locking::Proxy::Proxy
Proxy(Proxy &&other)
Definition:
Locking.h:45
Locking::Proxy::get
T & get()
Definition:
Locking.h:53
Locking::Proxy::get
const T & get() const
Definition:
Locking.h:57
Locking::Proxy::operator->
T * operator->() const
Definition:
Locking.h:49
Locking::Proxy::release
void release()
Definition:
Locking.h:65
Locking::Proxy::isLocked
bool isLocked()
Definition:
Locking.h:61
Locking
Wraps given object together with a mutex, locking it every time the object is accessed.
Definition:
Locking.h:12
Locking::~Locking
~Locking()
Definition:
Locking.h:26
Locking::operator->
Proxy operator->()
Definition:
Locking.h:76
Locking::lock
Proxy lock()
Definition:
Locking.h:72
Locking::Locking
Locking(T &&value)
Definition:
Locking.h:23
Locking::Locking
Locking(const T &value)
Definition:
Locking.h:20
Locking::Locking
Locking()=default
std
Overload of std::swap for Sph::Array.
Definition:
Array.h:578
Generated by
1.9.1