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
33namespace epee
34{
35class critical_region;
36
37class critical_section {
38
39 boost::mutex m_section;
40
41public:
42
44 InitializeCriticalSection( &m_section );
45 }
46
48 InitializeCriticalSection( &m_section );
49 }
50
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
68 {
69 return *this;
70 }
71
72
73};
74
75class critical_region {
76
77 ::critical_section *m_locker;
78
79 critical_region( const critical_region& ){}
80
81public:
82
84 m_locker = &cs;
85 cs.lock();
86 }
87
89 {
90 m_locker->unlock();
91 }
92};
93
94
96{
97public:
99 {
100 ::InitializeSRWLock(&m_srw_lock);
101 }
104
106 {
107 AcquireSRWLockShared(&m_srw_lock);
108 return true;
109 }
111 {
112 ReleaseSRWLockShared(&m_srw_lock);
113 return true;
114 }
116 {
117 ::AcquireSRWLockExclusive(&m_srw_lock);
118 return true;
119 }
121 {
122 ::ReleaseSRWLockExclusive(&m_srw_lock);
123 return true;
124 }
125private:
126 SRWLOCK m_srw_lock;
127
128};
129
130
132{
133public:
134 shared_guard(shared_critical_section& ref_sec):m_ref_sec(ref_sec)
135 {
136 m_ref_sec.lock_shared();
137 }
138
140 {
141 m_ref_sec.unlock_shared();
142 }
143
144private:
145 shared_critical_section& m_ref_sec;
146};
147
148
150{
151public:
152 exclusive_guard(shared_critical_section& ref_sec):m_ref_sec(ref_sec)
153 {
154 m_ref_sec.lock_exclusive();
155 }
156
158 {
159 m_ref_sec.unlock_exclusive();
160 }
161
162private:
163 shared_critical_section& m_ref_sec;
164};
165
166
167class event
168{
169public:
171 {
172 m_hevent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
173 }
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 }
194private:
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
critical_region(critical_section &cs)
Definition winobj.h:83
critical_section & operator=(const critical_section &section)
Definition winobj.h:67
critical_section(const critical_section &section)
Definition winobj.h:43
bool reset()
Definition winobj.h:185
HANDLE get_handle()
Definition winobj.h:190
bool set()
Definition winobj.h:180
exclusive_guard(shared_critical_section &ref_sec)
Definition winobj.h:152
shared_guard(shared_critical_section &ref_sec)
Definition winobj.h:134
const char * res
const char * get_wait_for_result_as_text(DWORD res)
Definition winobj.h:210