summaryrefslogtreecommitdiffstats
path: root/source/OSSupport/CriticalSection.h
diff options
context:
space:
mode:
authormadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-09-23 23:23:33 +0200
committermadmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-09-23 23:23:33 +0200
commit7abb5f7604bb9a0a716e89f3b27e330b016a38b9 (patch)
tree7ccaea302b953c239a0d60548b6f7bcaf72e6527 /source/OSSupport/CriticalSection.h
parentSource files cleanup: Removed unused cBlockToPickup (diff)
downloadcuberite-7abb5f7604bb9a0a716e89f3b27e330b016a38b9.tar
cuberite-7abb5f7604bb9a0a716e89f3b27e330b016a38b9.tar.gz
cuberite-7abb5f7604bb9a0a716e89f3b27e330b016a38b9.tar.bz2
cuberite-7abb5f7604bb9a0a716e89f3b27e330b016a38b9.tar.lz
cuberite-7abb5f7604bb9a0a716e89f3b27e330b016a38b9.tar.xz
cuberite-7abb5f7604bb9a0a716e89f3b27e330b016a38b9.tar.zst
cuberite-7abb5f7604bb9a0a716e89f3b27e330b016a38b9.zip
Diffstat (limited to 'source/OSSupport/CriticalSection.h')
-rw-r--r--source/OSSupport/CriticalSection.h80
1 files changed, 80 insertions, 0 deletions
diff --git a/source/OSSupport/CriticalSection.h b/source/OSSupport/CriticalSection.h
new file mode 100644
index 000000000..9852a2e6c
--- /dev/null
+++ b/source/OSSupport/CriticalSection.h
@@ -0,0 +1,80 @@
+
+#pragma once
+
+
+
+
+
+class cCriticalSection
+{
+public:
+ cCriticalSection(void);
+ ~cCriticalSection();
+
+ void Lock(void);
+ void Unlock(void);
+
+ #ifdef _DEBUG
+ bool IsLocked(void);
+ bool IsLockedByCurrentThread(void);
+ #endif // _DEBUG
+
+private:
+ #ifdef _DEBUG
+ bool m_IsLocked;
+ unsigned long m_OwningThreadID;
+ #endif // _DEBUG
+
+ #ifdef _WIN32
+ CRITICAL_SECTION m_CriticalSection;
+ #else // _WIN32
+ void* m_CriticalSectionPtr ALIGN_8; // Pointer to a CRITICAL_SECTION object
+ void* m_Attributes ALIGN_8;
+ #endif // else _WIN32
+} ALIGN_8;
+
+
+
+
+/// RAII for cCriticalSection - locks the CS on creation, unlocks on destruction
+class cCSLock
+{
+ cCriticalSection * m_CS;
+
+ // Unlike a cCriticalSection, this object should be used from a single thread, therefore access to m_IsLocked is not threadsafe
+ // In Windows, it is an error to call cCriticalSection::Unlock() multiple times if the lock is not held,
+ // therefore we need to check this value whether we are locked or not.
+ bool m_IsLocked;
+
+public:
+ cCSLock(cCriticalSection * a_CS);
+ cCSLock(cCriticalSection & a_CS);
+ ~cCSLock();
+
+ // Temporarily unlock or re-lock:
+ void Lock(void);
+ void Unlock(void);
+
+private:
+ DISALLOW_COPY_AND_ASSIGN(cCSLock);
+} ;
+
+
+
+
+
+/// Temporary RAII unlock for a cCSLock. Useful for unlock-wait-relock scenarios
+class cCSUnlock
+{
+ cCSLock & m_Lock;
+public:
+ cCSUnlock(cCSLock & a_Lock);
+ ~cCSUnlock();
+
+private:
+ DISALLOW_COPY_AND_ASSIGN(cCSUnlock);
+} ;
+
+
+
+