TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
G3D::Spinlock Class Reference

A mutual exclusion lock that busy-waits when locking. More...

#include <GMutex.h>

Public Member Functions

 Spinlock ()
 
bool lock ()
 
void unlock ()
 

Private Attributes

AtomicInt32 x
 

Detailed Description

A mutual exclusion lock that busy-waits when locking.

On a machine with one (significant) thread per processor core, a Spinlock may be substantially faster than a mutex.

See also
G3D::GThread, G3D::GMutex, G3D::AtomicInt32

Constructor & Destructor Documentation

G3D::Spinlock::Spinlock ( )
inline
43 : x(0) {}
AtomicInt32 x
Definition: GMutex.h:39

Member Function Documentation

bool G3D::Spinlock::lock ( )
inline

Busy waits until the lock is unlocked, then locks it exclusively. Returns true if the lock succeeded on the first try (indicating no contention).

Unlike a G3D::GMutex, a single thread cannot re-enter Spinlock::lock() that it already locked.

52  {
53  bool first = true;
54  while (x.compareAndSet(0, 1) == 1) {
55  first = false;
56 # ifdef G3D_WINDOWS
57  Sleep(0);
58 # else
59  usleep(0);
60 # endif
61  }
62  return first;
63  }
AtomicInt32 x
Definition: GMutex.h:39
int32 compareAndSet(const int32 comperand, const int32 exchange)
Definition: AtomicInt32.h:142

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void G3D::Spinlock::unlock ( )
inline
65  {
66  x.compareAndSet(1, 0);
67  }
AtomicInt32 x
Definition: GMutex.h:39
int32 compareAndSet(const int32 comperand, const int32 exchange)
Definition: AtomicInt32.h:142

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

AtomicInt32 G3D::Spinlock::x
private

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