Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Member Functions | Private Attributes | Friends | List of all members
cCriticalSection Class Reference

#include <CriticalSection.h>

Public Member Functions

 cCriticalSection (void)
 
bool IsLocked (void)
 Returns true if the CS is currently locked. More...
 
bool IsLockedByCurrentThread (void)
 Returns true if the CS is currently locked by the thread calling this function. More...
 
void Lock (void)
 
void Unlock (void)
 

Private Attributes

std::recursive_mutex m_Mutex
 
std::thread::id m_OwningThreadID
 ID of the thread that is currently holding the CS. More...
 
int m_RecursionCount
 Number of times that this CS is currently locked (levels of recursion). More...
 

Friends

class cDeadlockDetect
 

Detailed Description

Definition at line 8 of file CriticalSection.h.

Constructor & Destructor Documentation

◆ cCriticalSection()

cCriticalSection::cCriticalSection ( void  )

Definition at line 12 of file CriticalSection.cpp.

Member Function Documentation

◆ IsLocked()

bool cCriticalSection::IsLocked ( void  )

Returns true if the CS is currently locked.

Note that since it relies on the m_RecursionCount value, it is inherently thread-unsafe, prone to false positives. Also, due to multithreading, the state can change between this when function is evaluated and the returned value is used. To be used in ASSERT(IsLocked()) only.

Definition at line 45 of file CriticalSection.cpp.

◆ IsLockedByCurrentThread()

bool cCriticalSection::IsLockedByCurrentThread ( void  )

Returns true if the CS is currently locked by the thread calling this function.

Note that since it relies on the m_RecursionCount value, it is inherently thread-unsafe, prone to false positives. Also, due to multithreading, the state can change between this when function is evaluated and the returned value is used. To be used in ASSERT(IsLockedByCurrentThread()) only.

Definition at line 54 of file CriticalSection.cpp.

◆ Lock()

void cCriticalSection::Lock ( void  )

Definition at line 21 of file CriticalSection.cpp.

◆ Unlock()

void cCriticalSection::Unlock ( void  )

Definition at line 33 of file CriticalSection.cpp.

Friends And Related Function Documentation

◆ cDeadlockDetect

friend class cDeadlockDetect
friend

Definition at line 10 of file CriticalSection.h.

Member Data Documentation

◆ m_Mutex

std::recursive_mutex cCriticalSection::m_Mutex
private

Definition at line 45 of file CriticalSection.h.

◆ m_OwningThreadID

std::thread::id cCriticalSection::m_OwningThreadID
private

ID of the thread that is currently holding the CS.

Note that this value should be considered true only when the CS is locked; without the lock, it is UndefinedBehavior to even read it, but making it std::atomic would impose too much of a runtime penalty. When unlocked, the value stored here has no meaning, it may be an ID of a previous holder, or it could be any garbage. It is only ever read without the lock in the DeadlockDetect, where the server is terminating anyway.

Definition at line 43 of file CriticalSection.h.

◆ m_RecursionCount

int cCriticalSection::m_RecursionCount
private

Number of times that this CS is currently locked (levels of recursion).

Zero if not locked. Note that this value should be considered true only when the CS is locked; without the lock, it is UndefinedBehavior to even read it, but making it std::atomic would impose too much of a runtime penalty. It is only ever read without the lock in the DeadlockDetect, where the server is terminating anyway.

Definition at line 36 of file CriticalSection.h.


The documentation for this class was generated from the following files: