Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
syncobj.h
Go to the documentation of this file.
1
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above copyright
9
// notice, this list of conditions and the following disclaimer in the
10
// documentation and/or other materials provided with the distribution.
11
// * Neither the name of the Andrey N. Sabelnikov nor the
12
// names of its contributors may be used to endorse or promote products
13
// derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
//
26
27
28
29
30
#ifndef __WINH_OBJ_H__
31
#define __WINH_OBJ_H__
32
33
#include <boost/chrono/duration.hpp>
34
#include <boost/thread/condition_variable.hpp>
35
#include <boost/thread/locks.hpp>
36
#include <boost/thread/mutex.hpp>
37
#include <boost/thread/recursive_mutex.hpp>
38
#include <boost/thread/thread.hpp>
39
40
namespace
epee
41
{
42
43
namespace
debug
44
{
45
inline
unsigned
int
&
g_test_dbg_lock_sleep
()
46
{
47
static
unsigned
int
value
= 0;
48
return
value
;
49
}
50
}
51
52
struct
simple_event
53
{
54
simple_event
() : m_rised(
false
)
55
{
56
}
57
58
void
raise
()
59
{
60
boost::unique_lock<boost::mutex> lock(m_mx);
61
m_rised =
true
;
62
m_cond_var.notify_one();
63
}
64
65
void
wait
()
66
{
67
boost::unique_lock<boost::mutex> lock(m_mx);
68
while
(!m_rised)
69
m_cond_var.wait(lock);
70
m_rised =
false
;
71
}
72
73
private
:
74
boost::mutex m_mx;
75
boost::condition_variable m_cond_var;
76
bool
m_rised;
77
};
78
79
class
critical_region;
80
81
class
critical_section
82
{
83
boost::recursive_mutex m_section;
84
85
public
:
86
//to make copy fake!
87
critical_section
(
const
critical_section
& section)
88
{
89
}
90
91
critical_section
()
92
{
93
}
94
95
~critical_section
()
96
{
97
}
98
99
void
lock
()
100
{
101
m_section.lock();
102
//EnterCriticalSection( &m_section );
103
}
104
105
void
unlock
()
106
{
107
m_section.unlock();
108
}
109
110
bool
tryLock
()
111
{
112
return
m_section.try_lock();
113
}
114
115
// to make copy fake
116
critical_section
&
operator=
(
const
critical_section
& section)
117
{
118
return
*
this
;
119
}
120
};
121
122
123
template
<
class
t_lock>
124
class
critical_region_t
125
{
126
t_lock& m_locker;
127
bool
m_unlocked;
128
129
critical_region_t(
const
critical_region_t&) {}
130
131
public
:
132
critical_region_t
(t_lock& cs): m_locker(cs), m_unlocked(
false
)
133
{
134
m_locker.lock();
135
}
136
137
~critical_region_t
()
138
{
139
unlock
();
140
}
141
142
void
unlock
()
143
{
144
if
(!m_unlocked)
145
{
146
m_locker.unlock();
147
m_unlocked =
true
;
148
}
149
}
150
};
151
152
153
#if defined(WINDWOS_PLATFORM)
154
class
shared_critical_section
155
{
156
public
:
157
shared_critical_section
()
158
{
159
::InitializeSRWLock(&m_srw_lock);
160
}
161
~shared_critical_section
()
162
{}
163
164
bool
lock_shared
()
165
{
166
AcquireSRWLockShared(&m_srw_lock);
167
return
true
;
168
}
169
bool
unlock_shared
()
170
{
171
ReleaseSRWLockShared(&m_srw_lock);
172
return
true
;
173
}
174
bool
lock_exclusive
()
175
{
176
::AcquireSRWLockExclusive(&m_srw_lock);
177
return
true
;
178
}
179
bool
unlock_exclusive
()
180
{
181
::ReleaseSRWLockExclusive(&m_srw_lock);
182
return
true
;
183
}
184
private
:
185
SRWLOCK m_srw_lock;
186
};
187
188
189
class
shared_guard
190
{
191
public
:
192
shared_guard
(shared_critical_section& ref_sec):m_ref_sec(ref_sec)
193
{
194
m_ref_sec.lock_shared();
195
}
196
197
~shared_guard
()
198
{
199
m_ref_sec.unlock_shared();
200
}
201
202
private
:
203
shared_critical_section& m_ref_sec;
204
};
205
206
207
class
exclusive_guard
208
{
209
public
:
210
exclusive_guard
(shared_critical_section& ref_sec):m_ref_sec(ref_sec)
211
{
212
m_ref_sec.lock_exclusive();
213
}
214
215
~exclusive_guard
()
216
{
217
m_ref_sec.unlock_exclusive();
218
}
219
220
private
:
221
shared_critical_section& m_ref_sec;
222
};
223
#endif
224
225
#define SHARED_CRITICAL_REGION_BEGIN(x) { shared_guard critical_region_var(x)
226
#define EXCLUSIVE_CRITICAL_REGION_BEGIN(x) { exclusive_guard critical_region_var(x)
227
228
#define CRITICAL_REGION_LOCAL(x) {boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep()));} epee::critical_region_t<decltype(x)> critical_region_var(x)
229
#define CRITICAL_REGION_BEGIN(x) { boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep())); epee::critical_region_t<decltype(x)> critical_region_var(x)
230
#define CRITICAL_REGION_LOCAL1(x) {boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep()));} epee::critical_region_t<decltype(x)> critical_region_var1(x)
231
#define CRITICAL_REGION_BEGIN1(x) { boost::this_thread::sleep_for(boost::chrono::milliseconds(epee::debug::g_test_dbg_lock_sleep())); epee::critical_region_t<decltype(x)> critical_region_var1(x)
232
233
#define CRITICAL_REGION_END() }
234
235
236
#if defined(WINDWOS_PLATFORM)
237
inline
const
char
*
get_wait_for_result_as_text
(DWORD
res
)
238
{
239
switch
(
res
)
240
{
241
case
WAIT_ABANDONED:
return
"WAIT_ABANDONED"
;
242
case
WAIT_TIMEOUT:
return
"WAIT_TIMEOUT"
;
243
case
WAIT_OBJECT_0:
return
"WAIT_OBJECT_0"
;
244
case
WAIT_OBJECT_0+1:
return
"WAIT_OBJECT_1"
;
245
case
WAIT_OBJECT_0+2:
return
"WAIT_OBJECT_2"
;
246
default
:
return
"UNKNOWN CODE"
;
247
}
248
}
249
#endif
250
251
}
252
253
#endif
epee::critical_region_t::unlock
void unlock()
Definition
syncobj.h:142
epee::critical_region_t::~critical_region_t
~critical_region_t()
Definition
syncobj.h:137
epee::critical_region_t::critical_region_t
critical_region_t(t_lock &cs)
Definition
syncobj.h:132
epee::critical_section::lock
void lock()
Definition
syncobj.h:99
epee::critical_section::~critical_section
~critical_section()
Definition
syncobj.h:95
epee::critical_section::unlock
void unlock()
Definition
syncobj.h:105
epee::critical_section::operator=
critical_section & operator=(const critical_section §ion)
Definition
syncobj.h:116
epee::critical_section::critical_section
critical_section(const critical_section §ion)
Definition
syncobj.h:87
epee::critical_section::tryLock
bool tryLock()
Definition
syncobj.h:110
epee::critical_section::critical_section
critical_section()
Definition
syncobj.h:91
epee::exclusive_guard
Definition
winobj.h:150
epee::exclusive_guard::exclusive_guard
exclusive_guard(shared_critical_section &ref_sec)
Definition
winobj.h:152
epee::exclusive_guard::~exclusive_guard
~exclusive_guard()
Definition
winobj.h:157
epee::shared_critical_section::lock_shared
bool lock_shared()
Definition
winobj.h:105
epee::shared_critical_section::shared_critical_section
shared_critical_section()
Definition
winobj.h:98
epee::shared_critical_section::lock_exclusive
bool lock_exclusive()
Definition
winobj.h:115
epee::shared_critical_section::unlock_exclusive
bool unlock_exclusive()
Definition
winobj.h:120
epee::shared_critical_section::~shared_critical_section
~shared_critical_section()
Definition
winobj.h:102
epee::shared_critical_section::unlock_shared
bool unlock_shared()
Definition
winobj.h:110
epee::shared_guard
Definition
winobj.h:132
epee::shared_guard::~shared_guard
~shared_guard()
Definition
winobj.h:139
epee::shared_guard::shared_guard
shared_guard(shared_critical_section &ref_sec)
Definition
winobj.h:134
res
const char * res
Definition
hmac_keccak.cpp:41
epee::debug
Definition
misc_log_ex.h:137
epee::debug::g_test_dbg_lock_sleep
unsigned int & g_test_dbg_lock_sleep()
Definition
syncobj.h:45
epee
Definition
ado_db_helper.h:67
epee::get_wait_for_result_as_text
const char * get_wait_for_result_as_text(DWORD res)
Definition
winobj.h:210
value
const GenericPointer< typename T::ValueType > T2 value
Definition
pointer.h:1225
false
#define false
epee::simple_event::raise
void raise()
Definition
syncobj.h:58
epee::simple_event::wait
void wait()
Definition
syncobj.h:65
epee::simple_event::simple_event
simple_event()
Definition
syncobj.h:54
contrib
epee
include
syncobj.h
Generated on
for Electroneum by
1.17.0