20#define NEBULA_USER_CRITICAL_SECTION 1
22#if NEBULA_USER_CRITICAL_SECTION
23extern "C" void _WriteBarrier();
24extern "C" void _ReadWriteBarrier();
25#pragma intrinsic(_WriteBarrier)
26#pragma intrinsic(_ReadWriteBarrier)
44#if NEBULA_USER_CRITICAL_SECTION
46 bool PerfLockImmediate(DWORD dwThreadID)
const;
48 bool PerfLock(DWORD dwThreadID)
const;
50 bool PerfLockKernel(DWORD dwThreadID)
const;
52 void PerfUnlock()
const;
54 void WaiterPlus()
const;
56 void WaiterMinus()
const;
58 void AllocateKernelSemaphore()
const;
62 mutable volatile DWORD lockerThread;
63 volatile DWORD spinMax;
64 mutable volatile long waiterCount;
65 mutable volatile HANDLE semaphore;
66 mutable uint recursiveLockCount;
72#if !NEBULA_USER_CRITICAL_SECTION
114Win32CriticalSection::WaiterPlus()
const
116 _InterlockedIncrement(&this->waiterCount);
123Win32CriticalSection::WaiterMinus()
const
125 _InterlockedDecrement(&this->waiterCount);
132Win32CriticalSection::PerfLockImmediate(DWORD dwThreadID)
const
134 return (0 == InterlockedCompareExchange((
long*) &this->lockerThread, dwThreadID, 0));
143 DWORD threadId = GetCurrentThreadId();
144 if (threadId != this->lockerThread)
146 if ((this->lockerThread == 0) &&
147 this->PerfLockImmediate(threadId))
154 this->PerfLock(threadId);
157 this->recursiveLockCount++;
164Win32CriticalSection::AllocateKernelSemaphore()
const
166 if (!this->semaphore)
170 if (InterlockedCompareExchangePointer(&this->semaphore, handle, NULL))
179Win32CriticalSection::PerfLock(DWORD dwThreadID)
const
182 for (DWORD dwSpin = 0; dwSpin < spinMax; dwSpin++)
184 if (this->PerfLockImmediate(dwThreadID))
191 this->AllocateKernelSemaphore();
194 bool result = this->PerfLockKernel(dwThreadID);
204Win32CriticalSection::PerfLockKernel(DWORD dwThreadID)
const
213 if (PerfLockImmediate(dwThreadID))
217 switch (WaitForSingleObject(this->semaphore, INFINITE))
239 if (--recursiveLockCount == 0)
249Win32CriticalSection::PerfUnlock()
const
253 this->lockerThread = 0;
257 if (this->waiterCount > 0)
260 ReleaseSemaphore(this->semaphore, 1, NULL);
Win32-implementation of critical section.
Definition win32criticalsection.h:32
~Win32CriticalSection()
destructor
Definition win32criticalsection.h:86
Win32CriticalSection()
constructor
Definition win32criticalsection.h:77
CRITICAL_SECTION criticalSection
Definition win32criticalsection.h:68
void Leave() const
leave the critical section
Definition win32criticalsection.h:104
void Enter() const
enter the critical section
Definition win32criticalsection.h:95
#define n_assert(exp)
Definition debug.h:50
SemaphoreId CreateSemaphore(const SemaphoreCreateInfo &info)
create semaphore
Definition vksemaphore.cc:37
[TODO: Describe Win32 subsystem]
unsigned int uint
Definition types.h:31