Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::base::SpinMutex Class Reference

SpinMutex exposes the same interface as ion::port::Mutex, but implements locking via a simple atomic CAS. More...

#include "spinmutex.h"

Public Member Functions

 SpinMutex ()
 
 ~SpinMutex ()
 
bool IsLocked () const
 Returns whether the Mutex is currently locked. Does not block. More...
 
void Lock ()
 Locks the Mutex. More...
 
bool TryLock ()
 Returns true if the mutex was successfully locked, and false otherwise. More...
 
void Unlock ()
 Unlocks the Mutex. More...
 

Detailed Description

SpinMutex exposes the same interface as ion::port::Mutex, but implements locking via a simple atomic CAS.

This gives it two advantages:

  • higher performance (when used in appropriate situations)
  • smaller memory footprint The downside is that waiters continue to consume CPU cycles as they wait.

This implementation is extremely simple, and makes no attempt at fairness. Starvation is possible; when the mutex is already locked, there is no guarantee that a thread that begins to wait for the mutex will become unblocked before subsequent blocking threads.

Definition at line 36 of file spinmutex.h.

Constructor & Destructor Documentation

ion::base::SpinMutex::SpinMutex ( )
inline

Definition at line 38 of file spinmutex.h.

ion::base::SpinMutex::~SpinMutex ( )

Definition at line 26 of file spinmutex.cc.

References DCHECK, and IsLocked().

Member Function Documentation

bool ion::base::SpinMutex::IsLocked ( ) const
inline

Returns whether the Mutex is currently locked. Does not block.

Definition at line 42 of file spinmutex.h.

Referenced by Unlock(), and ~SpinMutex().

void ion::base::SpinMutex::Lock ( )

Locks the Mutex.

Blocks the calling thread or process until the lock is available; no thread or process can return from Lock() until the lock owner Unlock()s.

Definition at line 28 of file spinmutex.cc.

References ion::port::YieldThread().

bool ion::base::SpinMutex::TryLock ( )

Returns true if the mutex was successfully locked, and false otherwise.

Does not block.

Definition at line 40 of file spinmutex.cc.

void ion::base::SpinMutex::Unlock ( )

Unlocks the Mutex.

Any thread or process can now return from Lock(). Does nothing if the Mutex was not locked by the calling thread or at all. Does not block.

Definition at line 46 of file spinmutex.cc.

References DCHECK, and IsLocked().


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