OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Ogre::LightweightMutex Class Reference

A lightweight mutex is a synchronization mechanism, very similar to a regular mutex. More...

#include <OgreLightweightMutex.h>

Public Member Functions

 LightweightMutex ()
 ~LightweightMutex ()
void lock ()
 Acquires the exclusive lock. More...
bool tryLock ()
 Tries to aquire the lock and returns immediately. More...
void unlock ()
 Releases the lock aquired through either. More...

Private Member Functions

 OGRE_ALIGNED_DECL (long, mCounter, 4)

Private Attributes

HANDLE mSemaphore

Detailed Description

A lightweight mutex is a synchronization mechanism, very similar to a regular mutex.

Regular mutexes are well known to be expensive because they need to enter & leave kernel mode.

Lightweight mutexes usually make use of atomic instructions (in x86 they're referred to as instructions with the "lock" prefix) to avoid entering kernel mode when no other thread has aquired the lock.
This comes with a couple caveats that we don't care for the applications we need, but is important to keep them in mind: 1) LightweightMutexes can't be shared among processes 2) Priority inversion is not a concern 3) Recursive locks aren't supported (same thread calling lock() twice can deadlock) Note that some LightweightMutex implementations may offer this functionality, but we don't guarantee them in all platforms/architectures. It is possible to write a lightweight mutex that supports recursive locks, but that requires a call to GetCurrentThreadId (in Windows), which as much as saying just use a regular mutex.
Windows users are familiar with the concept of LightweightMutexes because there is already an implementation provided: CRITICAL_SECTION. We go further by reinventing the wheel and writting it ourselves. If you ever wondered how a CRITICAL_SECTION works, now you know.
Interesting reads: http://preshing.com/20111124/always-use-a-lightweight-mutex http://preshing.com/20120226/roll-your-own-lightweight-mutex http://preshing.com/20120305/implementing-a-recursive-mutex

Definition at line 70 of file OgreLightweightMutex.h.

Constructor & Destructor Documentation

Ogre::LightweightMutex::LightweightMutex ( )
Ogre::LightweightMutex::~LightweightMutex ( )

Member Function Documentation

void Ogre::LightweightMutex::lock ( )

Acquires the exclusive lock.

Waits if necessary until another thread releases the lock. Recursive locking is not guaranteed (do not call this function twice from the same thread)

Ogre::LightweightMutex::OGRE_ALIGNED_DECL ( long  ,
mCounter  ,
bool Ogre::LightweightMutex::tryLock ( )

Tries to aquire the lock and returns immediately.

On failure returns false, true on success

void Ogre::LightweightMutex::unlock ( )

Releases the lock aquired through either.

See also
lock or

Member Data Documentation

HANDLE Ogre::LightweightMutex::mSemaphore

Definition at line 74 of file OgreLightweightMutex.h.

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