forked from tapetums/include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeteredSection.hpp
418 lines (351 loc) · 10.9 KB
/
MeteredSection.hpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#pragma once
//---------------------------------------------------------------------------//
//
// MeteredSection.hpp
// Original Author: Dan Chou
// Description: Defines the metered section synchronization object
// Bug Fixed by: しゅう
// Bug Fixed by: JVR
// Modified by: tapetums
//
// See Also:
// http://msdn.microsoft.com/en-us/library/ms810428.aspx
// http://msdn.microsoft.com/ja-jp/library/cc429052.aspx
// http://anpcf.blog71.fc2.com/blog-entry-7.html
// http://s-project.appspot.com/notes_vc.html
// http://jibaravr.blog51.fc2.com/blog-category-19.html
//
//---------------------------------------------------------------------------//
#include <utility>
#include <windows.h>
#include <strsafe.h>
//---------------------------------------------------------------------------//
//
// Class
//
//---------------------------------------------------------------------------//
class MeteredSection
{
static constexpr size_t MAX_METSECT_NAMELEN = 128;
private:
struct METSECT_SHARED_INFO
{
BOOL fInitialized; // Is the metered section initialized?
LONG lSpinLock; // Used to gain access to this structure
LONG lThreadsWaiting; // Count of threads waiting
LONG lAvailableCount; // Available resource count
LONG lMaximumCount; // Maximum resource count
};
struct METERED_SECTION
{
HANDLE hEvent; // Handle to a kernel event object
HANDLE hFileMap; // Handle to memory mapped file
METSECT_SHARED_INFO* lpSharedInfo;
};
private:
METSECT_SHARED_INFO m_met_info;
METERED_SECTION m_met_sect;
public:
bool Create(LONG lInitialCount, LONG lMaximumCount, LPCSTR lpName);
bool Create(LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName);
bool Open(LPCSTR lpName);
bool Open(LPCWSTR lpName);
DWORD Enter(DWORD dwMilliseconds);
bool Leave(LONG lReleaseCount, LONG* lpPreviousCount);
void Close();
private:
bool Init (LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName, BOOL bOpenOnly);
bool CreateSectEvent(LPCWSTR lpName, BOOL bOpenOnly);
bool CreateFileView (LONG lInitialCount, LONG lMaximumCount, LPCWSTR lpName, BOOL bOpenOnly);
void GetLock ();
void ReleaseLock();
};
//---------------------------------------------------------------------------//
//
// Interface functions
//
//---------------------------------------------------------------------------//
inline bool MeteredSection::Create
(
LONG lInitialCount,
LONG lMaximumCount,
LPCSTR lpName
)
{
WCHAR lpNameW[MAX_PATH];
// lpName を MBCS から UTF-16 に変換
::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpName, -1, lpNameW, MAX_PATH);
return Create(lInitialCount, lMaximumCount, lpNameW);
}
//---------------------------------------------------------------------------//
inline bool MeteredSection::Create
(
LONG lInitialCount,
LONG lMaximumCount,
LPCWSTR lpName
)
{
// Verify the parameters
if ( (lMaximumCount < 1) ||
(lInitialCount > lMaximumCount) ||
(lInitialCount < 0) ||
((lpName) && (wcslen(lpName) > MAX_METSECT_NAMELEN)) )
{
::SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
// Initialize it
if ( ! Init(lInitialCount, lMaximumCount, lpName, FALSE) )
{
// Metered section failed to initialize
Close();
return false;
}
return true;
}
//---------------------------------------------------------------------------//
inline bool MeteredSection::Open
(
LPCSTR lpName
)
{
WCHAR lpNameW [MAX_PATH];
// lpName を MBCS から UTF-16 に変換
::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpName, -1, lpNameW, MAX_PATH);
return Open(lpNameW);
}
//---------------------------------------------------------------------------//
inline bool MeteredSection::Open
(
LPCWSTR lpName
)
{
if ( lpName == nullptr )
{
return false;
}
if ( ! Init(0, 0, lpName, TRUE) )
{
// Metered section failed to initialize
Close();
return false;
}
return true;
}
//---------------------------------------------------------------------------//
inline DWORD MeteredSection::Enter
(
DWORD dwMilliseconds
)
{
for ( ; ; )
{
GetLock();
// We have access to the metered section, everything we do now will be atomic
if ( m_met_sect.lpSharedInfo->lAvailableCount >= 1 )
{
m_met_sect.lpSharedInfo->lAvailableCount--;
ReleaseLock();
return WAIT_OBJECT_0;
}
// Couldn't get in. Wait on the event object
m_met_sect.lpSharedInfo->lThreadsWaiting++;
::ResetEvent(m_met_sect.hEvent);
ReleaseLock();
if ( WAIT_TIMEOUT == WaitForSingleObject(m_met_sect.hEvent, dwMilliseconds) )
{
return WAIT_TIMEOUT;
}
}
}
//---------------------------------------------------------------------------//
inline bool MeteredSection::Leave
(
LONG lReleaseCount,
LONG* lpPreviousCount
)
{
int iCount;
GetLock();
// Save the old value if they want it
if ( lpPreviousCount )
{
*lpPreviousCount = m_met_sect.lpSharedInfo->lAvailableCount;
}
// We have access to the metered section, everything we do now will be atomic
if ( (lReleaseCount < 0) ||
(m_met_sect.lpSharedInfo->lAvailableCount + lReleaseCount > m_met_sect.lpSharedInfo->lMaximumCount) )
{
ReleaseLock();
::SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
m_met_sect.lpSharedInfo->lAvailableCount += lReleaseCount;
// Set the event the appropriate number of times
lReleaseCount = min(lReleaseCount, m_met_sect.lpSharedInfo->lThreadsWaiting);
if ( m_met_sect.lpSharedInfo->lThreadsWaiting )
{
for ( iCount = 0; iCount < lReleaseCount; ++iCount )
{
m_met_sect.lpSharedInfo->lThreadsWaiting--;
/// SetEvent(m_met_sect.hEvent);
}
/// }
/// ReleaseLock();
/// ここから追加
ReleaseLock();
::SetEvent(m_met_sect.hEvent);
}
else
{
ReleaseLock();
}
/// ここまで追加
return true;
}
//---------------------------------------------------------------------------//
void MeteredSection::Close()
{
// Clean up
if ( m_met_sect.lpSharedInfo) { UnmapViewOfFile(m_met_sect.lpSharedInfo ); }
if ( m_met_sect.hFileMap) { CloseHandle(m_met_sect.hFileMap ); }
if ( m_met_sect.hEvent) { CloseHandle(m_met_sect.hEvent ); }
}
//---------------------------------------------------------------------------//
inline bool MeteredSection::Init
(
LONG lInitialCount,
LONG lMaximumCount,
LPCWSTR lpName,
BOOL bOpenOnly
)
{
/// ここから追加
m_met_sect.hEvent = nullptr;
m_met_sect.hFileMap = nullptr;
m_met_sect.lpSharedInfo = nullptr;
/// ここまで追加
// Try to create the event object
if ( CreateSectEvent(lpName, bOpenOnly) )
{
// Try to create the memory mapped file
if ( CreateFileView(lInitialCount, lMaximumCount, lpName, bOpenOnly) )
{
return true;
}
}
// Error occured, return false so the caller knows to clean up
return false;
}
//---------------------------------------------------------------------------//
inline bool MeteredSection::CreateSectEvent
(
LPCWSTR lpName,
BOOL bOpenOnly
)
{
WCHAR sz [MAX_PATH];
if ( lpName )
{
/// _stprintf(sz, _T("DKC_MSECT_EVT_%s"), lpName);
::StringCchPrintfW(sz, MAX_PATH, L"DKC_MSECT_EVT_%s", lpName);
if ( bOpenOnly )
{
/// m_met_sect.hEvent = ::OpenEventW(0, FALSE, sz);
m_met_sect.hEvent = ::OpenEventW(EVENT_ALL_ACCESS, FALSE, sz);
}
else
{
// Create an auto-reset named event object
m_met_sect.hEvent = ::CreateEventW(nullptr, FALSE, FALSE, sz);
}
}
else
{
// Create an auto-reset unnamed event object
m_met_sect.hEvent = ::CreateEventW(nullptr, FALSE, FALSE, nullptr);
}
return m_met_sect.hEvent ? true : false;
}
//---------------------------------------------------------------------------//
inline bool MeteredSection::CreateFileView
(
LONG lInitialCount,
LONG lMaximumCount,
LPCWSTR lpName,
BOOL bOpenOnly
)
{
WCHAR sz [MAX_PATH];
DWORD dwLastError;
if ( lpName )
{
/// _stprintf(sz, _T("DKC_MSECT_MMF_%s"), lpName);
::StringCchPrintfW(sz, MAX_PATH, L"DKC_MSECT_MMF_%s", lpName);
if ( bOpenOnly )
{
/// m_met_sect.hFileMap = OpenFileMappingW(0, FALSE, sz);
m_met_sect.hFileMap = ::OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, sz);
}
else
{
// Create a named file mapping
m_met_sect.hFileMap = ::CreateFileMappingW
(
INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE,
0, sizeof(METSECT_SHARED_INFO), sz
);
}
}
else
{
// Create an unnamed file mapping
m_met_sect.hFileMap = ::CreateFileMappingW
(
INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE,
0, sizeof(METSECT_SHARED_INFO), nullptr
);
}
// Map a view of the file
if ( m_met_sect.hFileMap )
{
dwLastError = ::GetLastError();
m_met_sect.lpSharedInfo = (METSECT_SHARED_INFO*)::MapViewOfFile
(
m_met_sect.hFileMap, FILE_MAP_WRITE, 0, 0, 0
);
if ( m_met_sect.lpSharedInfo )
{
if ( (! bOpenOnly) && dwLastError != ERROR_ALREADY_EXISTS )
{
m_met_sect.lpSharedInfo->lSpinLock = 0;
m_met_sect.lpSharedInfo->lThreadsWaiting = 0;
m_met_sect.lpSharedInfo->lAvailableCount = lInitialCount;
m_met_sect.lpSharedInfo->lMaximumCount = lMaximumCount;
::InterlockedExchange((LONG*)&(m_met_sect.lpSharedInfo->fInitialized), (LONG)TRUE);
}
else
{ // Already exists; wait for it to be initialized by the creator
while ( ! m_met_sect.lpSharedInfo->fInitialized ) { ::Sleep(0); }
}
return true;
}
}
return false;
}
//---------------------------------------------------------------------------//
inline void MeteredSection::GetLock()
{
// Spin and get access to the metered section lock
while ( ::InterlockedExchange(&(m_met_sect.lpSharedInfo->lSpinLock), 1) != 0 )
{
::Sleep(0);
}
}
//---------------------------------------------------------------------------//
inline void MeteredSection::ReleaseLock()
{
::InterlockedExchange(&(m_met_sect.lpSharedInfo->lSpinLock), 0);
}
//---------------------------------------------------------------------------//
// MeteredSection.hpp