| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- //-------------------------------------------------------------------------------------------------------
- // Copyright (C) Microsoft. All rights reserved.
- // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
- //-------------------------------------------------------------------------------------------------------
- #pragma once
- class CriticalSection
- #ifndef _WIN32
- : public CCLock
- {
- public:
- CriticalSection(DWORD spincount = 0): CCLock(true) { }
- };
- #else // _WIN32
- {
- public:
- CriticalSection(DWORD spincount = 0)
- {
- #pragma prefast(suppress:6031, "InitializeCriticalSectionAndSpinCount always succeed since Vista. No need to check return value");
- ::InitializeCriticalSectionAndSpinCount(&cs, spincount);
- }
- ~CriticalSection() { ::DeleteCriticalSection(&cs); }
- BOOL TryEnter() { return ::TryEnterCriticalSection(&cs); }
- void Enter() { ::EnterCriticalSection(&cs); }
- void Leave() { ::LeaveCriticalSection(&cs); }
- #if DBG
- bool IsLocked() const { return cs.OwningThread == (HANDLE)::GetCurrentThreadId(); }
- #endif
- private:
- CRITICAL_SECTION cs;
- };
- #endif
- //FakeCriticalSection mimics CriticalSection apis
- class FakeCriticalSection
- {
- public:
- FakeCriticalSection(DWORD spincount = 0) { /*do nothing*/spincount++; }
- ~FakeCriticalSection() {}
- BOOL TryEnter() { return true; }
- void Enter() {}
- void Leave() {}
- #if DBG
- bool IsLocked() const { return true; }
- #endif
- };
- class AutoCriticalSection
- {
- public:
- AutoCriticalSection(CriticalSection * cs) : cs(cs) { cs->Enter(); }
- ~AutoCriticalSection() { cs->Leave(); }
- private:
- CriticalSection * cs;
- };
- class AutoOptionalCriticalSection
- {
- public:
- AutoOptionalCriticalSection(CriticalSection * cs) : cs(cs)
- {
- if (cs)
- {
- cs->Enter();
- }
- }
- ~AutoOptionalCriticalSection()
- {
- if (cs)
- {
- cs->Leave();
- }
- }
- private:
- CriticalSection * cs;
- };
- template <class SyncObject = FakeCriticalSection >
- class AutoRealOrFakeCriticalSection
- {
- public:
- AutoRealOrFakeCriticalSection(SyncObject * cs) : cs(cs) { cs->Enter(); }
- ~AutoRealOrFakeCriticalSection() { cs->Leave(); }
- private:
- SyncObject * cs;
- };
- template <class SyncObject = FakeCriticalSection >
- class AutoOptionalRealOrFakeCriticalSection
- {
- public:
- AutoOptionalRealOrFakeCriticalSection(SyncObject * cs) : cs(cs)
- {
- if (cs)
- {
- cs->Enter();
- }
- }
- ~AutoOptionalRealOrFakeCriticalSection()
- {
- if (cs)
- {
- cs->Leave();
- }
- }
- private:
- SyncObject * cs;
- };
|