-
Notifications
You must be signed in to change notification settings - Fork 2
/
cs.h
219 lines (191 loc) · 5.04 KB
/
cs.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// cs.h
//
// Copyright (c) Nikolay Raspopov, 2009-2023.
// This file is part of USB Oblivion (https://www.cherubicsoft.com/en/projects/usboblivion/)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#pragma once
namespace c4u {
inline DWORD WaitForMultipleObjectsLoop (
const DWORD nCount,
const HANDLE* const pHandles,
const BOOL fWaitAll,
const DWORD dwMilliseconds) noexcept
{
MSG msg;
DWORD res;
do {
while (PeekMessage (&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
res = MsgWaitForMultipleObjects (nCount, pHandles, fWaitAll, dwMilliseconds, QS_ALLINPUT);
} while (res == WAIT_OBJECT_0 + nCount);
return res;
}
inline DWORD CoWaitForSingleObject (HANDLE Handle, const DWORD dwMilliseconds) noexcept
{
return WaitForMultipleObjectsLoop (1, &Handle, FALSE, dwMilliseconds);
}
/*inline DWORD CoWaitForSingleObject (
HANDLE Handle,
DWORD dwMilliseconds)
{
DWORD index = 0;
switch (CoWaitForMultipleHandles (0, dwMilliseconds, 1, &Handle, &index)) {
case S_OK:
return WAIT_OBJECT_0;
case RPC_S_CALLPENDING:
return WAIT_TIMEOUT;
}
return WAIT_ABANDONED;
}*/
inline DWORD CoWaitForMultipleObjects (
const DWORD nCount,
const HANDLE* const lpHandles,
const BOOL bWaitAll,
const DWORD dwMilliseconds) noexcept
{
return WaitForMultipleObjectsLoop (nCount, lpHandles, bWaitAll, dwMilliseconds);
}
/*inline DWORD CoWaitForMultipleObjects (
DWORD nCount,
const HANDLE* lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds)
{
DWORD index = 0;
switch (CoWaitForMultipleHandles (bWaitAll ? COWAIT_WAITALL : 0,
dwMilliseconds, nCount, const_cast <HANDLE*>(lpHandles), &index)) {
case S_OK:
return index + WAIT_OBJECT_0;
case RPC_S_CALLPENDING:
return WAIT_TIMEOUT;
}
return index + WAIT_ABANDONED;
}*/
class locker
{
public:
virtual void lock () const = 0;
virtual void unlock () const = 0;
virtual bool try_lock (const DWORD timeout /* ìñ, 0 èëè INFINITE */) const = 0;
};
class mutex : public locker
{
public:
inline mutex () noexcept
{
m_mutex = CreateMutex (nullptr, false, nullptr);
}
inline ~mutex () noexcept
{
CloseHandle (m_mutex);
}
virtual void lock () const noexcept override
{
CoWaitForSingleObject (m_mutex, INFINITE);
}
virtual void unlock () const noexcept override
{
ReleaseMutex (m_mutex);
}
virtual bool try_lock (const DWORD timeout /* ìñ, 0 èëè INFINITE */) const noexcept override
{
return (WaitForMultipleObjectsLoop (1, const_cast <const HANDLE*> (&m_mutex),
FALSE, timeout) == WAIT_OBJECT_0);
}
protected:
mutable HANDLE m_mutex;
private:
mutex( const mutex& );
mutex& operator = ( const mutex& );
};
class cs : public locker
{
public:
inline cs () noexcept
{
InitializeCriticalSection (&m_sect);
}
inline ~cs () noexcept
{
DeleteCriticalSection (&m_sect);
}
virtual void lock () const noexcept override
{
EnterCriticalSection (&m_sect);
}
virtual void unlock () const noexcept override
{
LeaveCriticalSection (&m_sect);
}
virtual bool try_lock (const DWORD timeout /* ìñ, 0 èëè INFINITE */) const noexcept override
{
DWORD start_time = GetTickCount ();
do {
if (TryEnterCriticalSection (&m_sect))
return true;
MSG msg;
while (PeekMessage (&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
} while (GetTickCount () - start_time < timeout);
return false;
}
protected:
mutable CRITICAL_SECTION m_sect;
private:
cs( const cs& );
cs& operator = ( const cs& );
};
class locker_holder
{
public:
inline locker_holder (const locker* c) noexcept
{
if (c) {
c->lock ();
m_hold = c;
}
}
inline locker_holder (const locker* c, const DWORD timeout /* ìñ, 0 èëè INFINITE */) noexcept
{
if (c && c->try_lock (timeout)) {
m_hold = c;
} else
m_hold = nullptr;
}
inline operator bool () const noexcept
{
return (m_hold != nullptr);
}
inline ~locker_holder () noexcept
{
if (m_hold) {
m_hold->unlock ();
m_hold = nullptr;
}
}
protected:
const locker* m_hold;
private:
locker_holder( const locker_holder& );
locker_holder& operator = ( const locker_holder& );
};
}