Electroneum
Loading...
Searching...
No Matches
winobj.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
#ifndef __WINH_OBJ_H__
29
#define __WINH_OBJ_H__
30
31
#include <boost/thread/locks.hpp>
32
33
namespace
epee
34
{
35
class
critical_region
;
36
37
class
critical_section
{
38
39
boost::mutex m_section;
40
41
public
:
42
43
critical_section
(
const
critical_section
& section ) {
44
InitializeCriticalSection( &m_section );
45
}
46
47
critical_section
() {
48
InitializeCriticalSection( &m_section );
49
}
50
51
~critical_section
() {
52
DeleteCriticalSection( &m_section );
53
}
54
55
void
lock
() {
56
EnterCriticalSection( &m_section );
57
}
58
59
void
unlock
() {
60
LeaveCriticalSection( &m_section );
61
}
62
63
bool
tryLock
() {
64
return
TryEnterCriticalSection( &m_section )?
true
:
false
;
65
}
66
67
critical_section
&
operator=
(
const
critical_section
& section )
68
{
69
return
*
this
;
70
}
71
72
73
};
74
75
class
critical_region {
76
77
::critical_section
*m_locker;
78
79
critical_region(
const
critical_region& ){}
80
81
public
:
82
83
critical_region
(
critical_section
&cs ) {
84
m_locker = &cs;
85
cs.lock();
86
}
87
88
~critical_region
()
89
{
90
m_locker->unlock();
91
}
92
};
93
94
95
class
shared_critical_section
96
{
97
public
:
98
shared_critical_section
()
99
{
100
::InitializeSRWLock(&m_srw_lock);
101
}
102
~shared_critical_section
()
103
{}
104
105
bool
lock_shared
()
106
{
107
AcquireSRWLockShared(&m_srw_lock);
108
return
true
;
109
}
110
bool
unlock_shared
()
111
{
112
ReleaseSRWLockShared(&m_srw_lock);
113
return
true
;
114
}
115
bool
lock_exclusive
()
116
{
117
::AcquireSRWLockExclusive(&m_srw_lock);
118
return
true
;
119
}
120
bool
unlock_exclusive
()
121
{
122
::ReleaseSRWLockExclusive(&m_srw_lock);
123
return
true
;
124
}
125
private
:
126
SRWLOCK m_srw_lock;
127
128
};
129
130
131
class
shared_guard
132
{
133
public
:
134
shared_guard
(
shared_critical_section
& ref_sec):m_ref_sec(ref_sec)
135
{
136
m_ref_sec.lock_shared();
137
}
138
139
~shared_guard
()
140
{
141
m_ref_sec.unlock_shared();
142
}
143
144
private
:
145
shared_critical_section
& m_ref_sec;
146
};
147
148
149
class
exclusive_guard
150
{
151
public
:
152
exclusive_guard
(
shared_critical_section
& ref_sec):m_ref_sec(ref_sec)
153
{
154
m_ref_sec.lock_exclusive();
155
}
156
157
~exclusive_guard
()
158
{
159
m_ref_sec.unlock_exclusive();
160
}
161
162
private
:
163
shared_critical_section
& m_ref_sec;
164
};
165
166
167
class
event
168
{
169
public
:
170
event
()
171
{
172
m_hevent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
173
}
174
~event
()
175
{
176
::CloseHandle(m_hevent);
177
178
}
179
180
bool
set
()
181
{
182
return ::SetEvent(m_hevent) ?
true
:
false
;
183
}
184
185
bool
reset
()
186
{
187
return ::ResetEvent(m_hevent) ?
true
:
false
;
188
}
189
190
HANDLE
get_handle
()
191
{
192
return
m_hevent;
193
}
194
private
:
195
HANDLE m_hevent;
196
197
};
198
199
200
#define SHARED_CRITICAL_REGION_BEGIN(x) { shared_guard critical_region_var(x)
201
#define EXCLUSIVE_CRITICAL_REGION_BEGIN(x) { exclusive_guard critical_region_var(x)
202
203
204
205
#define CRITICAL_REGION_LOCAL(x) critical_region critical_region_var(x)
206
#define CRITICAL_REGION_BEGIN(x) { critical_region critical_region_var(x)
207
#define CRITICAL_REGION_END() }
208
209
210
inline
const
char
*
get_wait_for_result_as_text
(DWORD
res
)
211
{
212
switch
(
res
)
213
{
214
case
WAIT_ABANDONED:
return
"WAIT_ABANDONED"
;
215
case
WAIT_TIMEOUT:
return
"WAIT_TIMEOUT"
;
216
case
WAIT_OBJECT_0:
return
"WAIT_OBJECT_0"
;
217
case
WAIT_OBJECT_0+1:
return
"WAIT_OBJECT_1"
;
218
case
WAIT_OBJECT_0+2:
return
"WAIT_OBJECT_2"
;
219
default
:
220
return
"UNKNOWN CODE"
;
221
}
222
223
}
224
225
}
// namespace epee
226
227
#endif
epee::critical_region
Definition
winobj.h:75
epee::critical_region::~critical_region
~critical_region()
Definition
winobj.h:88
epee::critical_region::critical_region
critical_region(critical_section &cs)
Definition
winobj.h:83
epee::critical_section
Definition
syncobj.h:82
epee::critical_section::lock
void lock()
Definition
winobj.h:55
epee::critical_section::~critical_section
~critical_section()
Definition
winobj.h:51
epee::critical_section::unlock
void unlock()
Definition
winobj.h:59
epee::critical_section::operator=
critical_section & operator=(const critical_section §ion)
Definition
winobj.h:67
epee::critical_section::critical_section
critical_section(const critical_section §ion)
Definition
winobj.h:43
epee::critical_section::tryLock
bool tryLock()
Definition
winobj.h:63
epee::critical_section::critical_section
critical_section()
Definition
winobj.h:47
epee::event::reset
bool reset()
Definition
winobj.h:185
epee::event::get_handle
HANDLE get_handle()
Definition
winobj.h:190
epee::event::set
bool set()
Definition
winobj.h:180
epee::event::event
event()
Definition
winobj.h:170
epee::event::~event
~event()
Definition
winobj.h:174
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
Definition
winobj.h:96
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::~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
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
contrib
epee
include
winobj.h
Generated on
for Electroneum by
1.16.1