-
Notifications
You must be signed in to change notification settings - Fork 1
/
CCriticalSection.h
495 lines (470 loc) · 13.1 KB
/
CCriticalSection.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#ifndef H_CLASS_CRITICAL_SECTION___
#define H_CLASS_CRITICAL_SECTION___
#include <windows.h>
#include "TCHAR.h"
#include "util.h"
#include <stack>
//#define DEBUGCS
#ifndef _DEBUG
//#define DEBUGCS
#endif
#ifdef DEBUGCS
#define EnterCriticalSection_Session(x) EnterCriticalSection_SessionD(x)
#define LeaveCriticalSection_Session LeaveCriticalSection_SessionD
#define EnterCriticalSection_Object(x) EnterCriticalSection_ObjectD(x)
#define LeaveCriticalSection_Object LeaveCriticalSection_ObjectD
#define EnterCriticalSection_Packet(x) EnterCriticalSection_PacketD(x)
#define LeaveCriticalSection_Packet LeaveCriticalSection_PacketD
#define EnterCriticalSection_StageTexture(x) EnterCriticalSection_StageTextureD(x)
#define LeaveCriticalSection_StageTexture LeaveCriticalSection_StageTextureD
#define EnterCriticalSection_Effect(x) EnterCriticalSection_EffectD(x)
#define LeaveCriticalSection_Effect LeaveCriticalSection_EffectD
#define EnterCriticalSection_Sound(x) EnterCriticalSection_SoundD(x)
#define LeaveCriticalSection_Sound LeaveCriticalSection_SoundD
#define EnterCriticalSection_Lua(x) EnterCriticalSection_LuaD(x)
#define LeaveCriticalSection_Lua LeaveCriticalSection_LuaD
#define EnterCriticalSection_Log(x) EnterCriticalSection_LogD(x)
#define LeaveCriticalSection_Log LeaveCriticalSection_LogD
#define EnterCriticalSection_Input(x) EnterCriticalSection_InputD(x)
#define LeaveCriticalSection_Input LeaveCriticalSection_InputD
#else
#define EnterCriticalSection_Session(x) EnterCriticalSection_SessionR()
#define LeaveCriticalSection_Session() LeaveCriticalSection_SessionR()
#define GetSessoinWord() 0L
#define EnterCriticalSection_Object(x) EnterCriticalSection_ObjectR()
#define LeaveCriticalSection_Object() LeaveCriticalSection_ObjectR()
#define GetObjectWord() 0L
#define EnterCriticalSection_Packet(x) EnterCriticalSection_PacketR()
#define LeaveCriticalSection_Packet LeaveCriticalSection_PacketR
#define GetObjectPacket() 0L
#define EnterCriticalSection_StageTexture(x) EnterCriticalSection_StageTextureR()
#define LeaveCriticalSection_StageTexture LeaveCriticalSection_StageTextureR
#define GetObjectStageTexture() 0L
#define EnterCriticalSection_Effect(x) EnterCriticalSection_EffectR()
#define LeaveCriticalSection_Effect LeaveCriticalSection_EffectR
#define GetObjectEffect() 0L
#define EnterCriticalSection_Sound(x) EnterCriticalSection_SoundR()
#define LeaveCriticalSection_Sound LeaveCriticalSection_SoundR
#define GetObjectSound() 0L
#define EnterCriticalSection_Lua(x) EnterCriticalSection_LuaR()
#define LeaveCriticalSection_Lua LeaveCriticalSection_LuaR
#define GetObjectLua() 0L
#define EnterCriticalSection_Log(x) EnterCriticalSection_LogR()
#define LeaveCriticalSection_Log LeaveCriticalSection_LogR
#define GetObjectLog() 0L
#define EnterCriticalSection_Input(x) EnterCriticalSection_InputR()
#define LeaveCriticalSection_Input LeaveCriticalSection_InputR
#define GetObjectInput() 0L
#endif
class CCriticalSection
{
public:
CCriticalSection()
{
InitializeCriticalSection(&m_CriticalSectionSession);
InitializeCriticalSection(&m_CriticalSectionPacket);
m_CriticalSectionObject = m_CriticalSectionSession;
// InitializeCriticalSection(&m_CriticalSectionObject);
InitializeCriticalSection(&m_CriticalSectionStageTexture);
InitializeCriticalSection(&m_CriticalSectionEffect);
InitializeCriticalSection(&m_CriticalSectionSound);
InitializeCriticalSection(&m_CriticalSectionLua);
InitializeCriticalSection(&m_CriticalSectionLog);
#if !USE_DINPUT
InitializeCriticalSection(&m_CriticalSectionInput);
#endif
};
~CCriticalSection()
{
#ifdef DEBUGCS
if (!m_stackSession.empty())
OutputDebugStr(L"!CCriticalSection::Session\n");
if (!m_stackPacket.empty())
OutputDebugStr(L"!CCriticalSection::Packet\n");
// if (m_stackObject.size())
// OutputDebugStr(L"!CCriticalSection::Object");
if (!m_stackStageTexture.empty())
OutputDebugStr(L"!CCriticalSection::StageTexture\n");
if (!m_stackEffect.empty())
OutputDebugStr(L"!CCriticalSection::Effect\n");
if (!m_stackSound.empty())
OutputDebugStr(L"!CCriticalSection::Sound\n");
if (!m_stackLua.empty())
OutputDebugStr(L"!CCriticalSection::Lua\n");
if (!m_stackLog.empty())
OutputDebugStr(L"!CCriticalSection::Log\n");
#if !USE_DINPUT
if (!m_stackInput.empty())
OutputDebugStr(L"!CCriticalSection::m_bInputFlg\n");
#endif
#endif
OutputDebugStr(L"m_CriticalSectionPacket\n");
DelCS(&m_CriticalSectionPacket);
OutputDebugStr(L"m_CriticalSectionSession\n");
DelCS(&m_CriticalSectionSession);
// OutputDebugStr(L"m_CriticalSectionObject");
// DelCS(&m_CriticalSectionObject);
OutputDebugStr(L"m_CriticalSectionStageTexture\n");
DelCS(&m_CriticalSectionStageTexture);
OutputDebugStr(L"m_CriticalSectionEffect\n");
DelCS(&m_CriticalSectionEffect);
OutputDebugStr(L"m_CriticalSectionSound\n");
DelCS(&m_CriticalSectionSound);
OutputDebugStr(L"m_CriticalSectionLua\n");
DelCS(&m_CriticalSectionLua);
OutputDebugStr(L"m_CriticalSectionLog\n");
DelCS(&m_CriticalSectionLog);
#if !USE_DINPUT
DelCS(&m_CriticalSectionInput);
#endif
};
// inline処理
#ifdef DEBUGCS
inline void DelCS(CRITICAL_SECTION* cs)
{
if (!TryEnterCriticalSection(cs))
{
OutputDebugStr(L"TryEnterCriticalSection:true\n");
LeaveCriticalSection(cs);
}
DeleteCriticalSection(cs);
};
#else
inline void DelCS(CRITICAL_SECTION* cs)
{
if (!TryEnterCriticalSection(cs))
LeaveCriticalSection(cs);
DeleteCriticalSection(cs);
};
#endif
// Session
#ifdef DEBUGCS
inline void EnterCriticalSection_SessionD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionSession))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackSession.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Session", MB_OK);
}
}
m_stackSession.push(w);
};
inline void LeaveCriticalSection_SessionD()
{
m_stackSession.pop();
LeaveCriticalSection(&m_CriticalSectionSession);
};
inline std::stack<WCHAR>& GetSessionWord()
{ return m_stackSession; };
#else
inline void EnterCriticalSection_SessionR()
{
EnterCriticalSection(&m_CriticalSectionSession);
};
inline void LeaveCriticalSection_SessionR()
{
LeaveCriticalSection(&m_CriticalSectionSession);
};
#endif
// Packet
#ifdef DEBUGCS
inline void EnterCriticalSection_PacketD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionPacket))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackPacket.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Packet", MB_OK);
}
}
m_stackPacket.push(w);
};
inline void LeaveCriticalSection_PacketD()
{
m_stackPacket.pop();
LeaveCriticalSection(&m_CriticalSectionPacket);
};
inline std::stack<WCHAR>& GetPacketWord()
{ return m_stackPacket; };
#else
inline void EnterCriticalSection_PacketR()
{
EnterCriticalSection(&m_CriticalSectionPacket);
};
inline void LeaveCriticalSection_PacketR()
{
LeaveCriticalSection(&m_CriticalSectionPacket);
};
#endif
// Object
#ifdef DEBUGCS
inline void EnterCriticalSection_ObjectD(WCHAR w)
{
// EnterCriticalSection(&m_CriticalSectionObject);
// m_stackObject.push(w);
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionSession))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackSession.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Session", MB_OK);
}
}
m_stackSession.push(w);
};
inline void LeaveCriticalSection_ObjectD()
{
// m_stackObject.pop();
// LeaveCriticalSection(&m_CriticalSectionObject);
m_stackSession.pop();
LeaveCriticalSection(&m_CriticalSectionSession);
};
inline std::stack<WCHAR>& GetObjectWord()
{ return m_stackObject; };
#else
inline void EnterCriticalSection_ObjectR()
{
EnterCriticalSection(&m_CriticalSectionSession);
};
inline void LeaveCriticalSection_ObjectR()
{
LeaveCriticalSection(&m_CriticalSectionSession);
};
#endif
// StageTexture
#ifdef DEBUGCS
inline void EnterCriticalSection_StageTextureD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionStageTexture))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackStageTexture.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section StageTexture", MB_OK);
}
}
m_stackStageTexture.push(w);
};
inline void LeaveCriticalSection_StageTextureD()
{
m_stackStageTexture.pop();
LeaveCriticalSection(&m_CriticalSectionStageTexture);
};
inline std::stack<WCHAR>& GetStageTextureWord()
{ return m_stackStageTexture; };
#else
inline void EnterCriticalSection_StageTextureR()
{
EnterCriticalSection(&m_CriticalSectionStageTexture);
};
inline void LeaveCriticalSection_StageTextureR()
{
LeaveCriticalSection(&m_CriticalSectionStageTexture);
};
#endif
// Effect
#ifdef DEBUGCS
inline void EnterCriticalSection_EffectD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionEffect))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackEffect.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Effect", MB_OK);
}
}
m_stackEffect.push(w);
};
inline void LeaveCriticalSection_EffectD()
{
m_stackEffect.pop();
LeaveCriticalSection(&m_CriticalSectionEffect);
};
inline std::stack<WCHAR>& GetEffectWord()
{ return m_stackEffect; };
#else
inline void EnterCriticalSection_EffectR()
{
EnterCriticalSection(&m_CriticalSectionEffect);
};
inline void LeaveCriticalSection_EffectR()
{
LeaveCriticalSection(&m_CriticalSectionEffect);
};
#endif
// Sound
#ifdef DEBUGCS
inline void EnterCriticalSection_SoundD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionSound))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackSound.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Sound", MB_OK);
}
}
m_stackSound.push(w);
};
inline void LeaveCriticalSection_SoundD()
{
m_stackSound.pop();
LeaveCriticalSection(&m_CriticalSectionSound);
};
inline std::stack<WCHAR>& GetSoundWord()
{ return m_stackSound; };
#else
inline void EnterCriticalSection_SoundR()
{
EnterCriticalSection(&m_CriticalSectionSound);
};
inline void LeaveCriticalSection_SoundR()
{
LeaveCriticalSection(&m_CriticalSectionSound);
};
#endif
// Lua
#ifdef DEBUGCS
inline void EnterCriticalSection_LuaD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionLua))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackLua.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Lua", MB_OK);
}
}
m_stackLua.push(w);
};
inline void LeaveCriticalSection_LuaD()
{
m_stackLua.pop();
LeaveCriticalSection(&m_CriticalSectionLua);
};
inline std::stack<WCHAR>& GetLuaWord()
{ return m_stackLua; };
#else
inline void EnterCriticalSection_LuaR()
{
EnterCriticalSection(&m_CriticalSectionLua);
};
inline void LeaveCriticalSection_LuaR()
{
LeaveCriticalSection(&m_CriticalSectionLua);
};
#endif
// Log
#ifdef DEBUGCS
inline void EnterCriticalSection_LogD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionLog))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackLog.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Log", MB_OK);
}
}
m_stackLog.push(w);
};
inline void LeaveCriticalSection_LogD()
{
m_stackLog.pop();
LeaveCriticalSection(&m_CriticalSectionLog);
};
inline std::stack<WCHAR>& GetLogWord()
{ return m_stackLog; };
#else
inline void EnterCriticalSection_LogR()
{
EnterCriticalSection(&m_CriticalSectionLog);
};
inline void LeaveCriticalSection_LogR()
{
LeaveCriticalSection(&m_CriticalSectionLog);
};
#endif
#if !USE_DINPUT
// Input
#ifdef DEBUGCS
inline void EnterCriticalSection_InputD(WCHAR w)
{
int i=0;
while (!TryEnterCriticalSection(&m_CriticalSectionInput))
{
Sleep(1);
if (++i == 3000)
{
WCHAR ws[3] = {m_stackInput.top(), w, L'\0'};
MessageBox(NULL, ws, L"critical section Input", MB_OK);
}
}
m_stackInput.push(w);
};
inline void LeaveCriticalSection_InputD()
{
m_stackInput.pop();
LeaveCriticalSection(&m_CriticalSectionInput);
};
inline std::stack<WCHAR>& GetInputWord()
{ return m_stackInput; };
#else
inline void EnterCriticalSection_InputR()
{
EnterCriticalSection(&m_CriticalSectionInput);
};
inline void LeaveCriticalSection_InputR()
{
LeaveCriticalSection(&m_CriticalSectionInput);
};
#endif
#endif
private:
// 変数
CRITICAL_SECTION m_CriticalSectionPacket; // パケットキュー保護
CRITICAL_SECTION m_CriticalSectionSession; // ユーザー情報保護
CRITICAL_SECTION m_CriticalSectionObject; // オブジェクト情報保護
CRITICAL_SECTION m_CriticalSectionStageTexture; // テクスチャ情報保護
CRITICAL_SECTION m_CriticalSectionEffect; // エフェクト情報保護
CRITICAL_SECTION m_CriticalSectionSound; // サウンド情報保護
CRITICAL_SECTION m_CriticalSectionLua; // Lua情報保護
CRITICAL_SECTION m_CriticalSectionLog; // Log情報保護
#if !USE_DINPUT
CRITICAL_SECTION m_CriticalSectionInput; // 入力情報保護
#endif
#ifdef DEBUGCS
std::stack<WCHAR> m_stackSession;
std::stack<WCHAR> m_stackObject;
std::stack<WCHAR> m_stackPacket;
std::stack<WCHAR> m_stackStageTexture;
std::stack<WCHAR> m_stackEffect;
std::stack<WCHAR> m_stackSound;
std::stack<WCHAR> m_stackLua;
std::stack<WCHAR> m_stackLog;
#if !USE_DINPUT
std::stack<WCHAR> m_stackInput;
#endif
#endif
};
#endif