-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstlxx.h
145 lines (125 loc) · 4.25 KB
/
stlxx.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
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
///////////////////////////////////////////////////////////////////////////////
/// \author (c) Anthony Fieroni ([email protected])
/// 2017, Plovdiv, Bulgaria
///
/// \license The MIT License (MIT)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////
#ifndef __STLXXX__
#define __STLXXX__
#include <mutex>
namespace stlxx {
template<class T, class mutex = std::recursive_mutex>
class atomic {
T *m_obj = nullptr;
size_t *m_cnt = nullptr;
mutex *m_mutex = nullptr;
void release() {
if (!m_mutex) return;
m_mutex->lock();
auto cnt = --(*m_cnt);
m_mutex->unlock();
if (cnt == 0) {
delete m_mutex;
delete m_cnt;
delete m_obj;
}
m_obj = nullptr;
m_cnt = nullptr;
m_mutex = nullptr;
}
class lock_proxy {
T *m_obj;
mutex &m_mutex;
public:
lock_proxy(mutex &m, T *o) : m_obj(o), m_mutex(m) { m_mutex.lock(); }
~lock_proxy() { m_mutex.unlock(); }
T* operator->() { return m_obj; }
};
public:
atomic() {
m_obj = new T;
m_mutex = new mutex;
m_cnt = new size_t{ 1 };
}
atomic(T &&o) { operator=(std::move(o)); }
atomic(atomic &&o) { operator=(std::move(o)); }
atomic(const atomic &o) { operator=(o); }
~atomic() { release(); }
atomic& operator=(atomic &&o) {
if (this == &o) return *this;
release();
m_obj = o.m_obj;
m_cnt = o.m_cnt;
m_mutex = o.m_mutex;
o.m_obj = nullptr;
o.m_cnt = nullptr;
o.m_mutex = nullptr;
return *this;
}
atomic& operator=(const atomic &o) {
if (this == &o) return *this;
release();
m_obj = o.m_obj;
m_cnt = o.m_cnt;
m_mutex = o.m_mutex;
std::lock_guard<mutex> guard(*m_mutex);
++(*m_cnt);
return *this;
}
atomic& operator=(T &&o) {
release();
m_mutex = new mutex;
m_cnt = new size_t{ 1 };
m_obj = new T{ std::move(o) };
return *this;
}
lock_proxy operator->() const {
return { *m_mutex, m_obj };
}
operator mutex&() const { return *m_mutex; }
operator const T&() const { return *m_obj; }
operator T&&() && { return std::move(*m_obj); };
};
class synchronize {
template<class T> static
void helper(std::function<void()> &func, T &mutex) {
std::lock_guard<T> lock(mutex, std::adopt_lock);
func();
}
template<class T1, class... TN> static
void helper(std::function<void()> &func, T1& mutex1, TN&... mutexN) {
std::lock_guard<T1> lock(mutex1, std::adopt_lock);
helper(func, mutexN...);
}
template<class T1, class... TN> friend
void synchronized(std::function<void()> func, T1& mutex1, TN&... mutexN);
};
template<class T> inline
void synchronized(std::function<void()> func, T &mutex) {
std::lock_guard<T> lock(mutex, std::adopt_lock);
func();
}
template<class T1, class... TN> inline
void synchronized(std::function<void()> func, T1& mutex1, TN&... mutexN) {
std::lock(mutex1, mutexN...);
synchronize::helper(func, mutex1, mutexN...);
}
}
#endif // __STLXXX__