libfoedus-core
FOEDUS Core Library
foedus::assorted::DumbSpinlock Class Reference

A simple spinlock using a boolean field. More...

Detailed Description

A simple spinlock using a boolean field.

As the name suggests, this spinlock implementation is not scalable at all. It's the dumbest implementation of lock, which might cause cacheline invalidation storm when contended. However, in many places it's enough and also the simplicity helps. You just need memory for one bool, that's it. It trivially works for a shared memory, too.

Use this object where you don't expect much contention.

Definition at line 40 of file dumb_spinlock.hpp.

#include <dumb_spinlock.hpp>

Public Member Functions

 DumbSpinlock (bool *locked, bool lock_initially=true)
 
 ~DumbSpinlock ()
 automatically unlocks when out of scope. More...
 
bool is_locked_by_me () const
 
void lock ()
 Locks it if I haven't locked it yet. More...
 
bool try_lock ()
 try-version of the lock. More...
 
void unlock ()
 Unlocks it if I locked it. More...
 

Constructor & Destructor Documentation

foedus::assorted::DumbSpinlock::DumbSpinlock ( bool *  locked,
bool  lock_initially = true 
)
inline

Definition at line 42 of file dumb_spinlock.hpp.

References lock().

42  : locked_by_me_(false), locked_(locked) {
43  if (lock_initially) {
44  lock();
45  }
46  }
void lock()
Locks it if I haven't locked it yet.

Here is the call graph for this function:

foedus::assorted::DumbSpinlock::~DumbSpinlock ( )
inline

automatically unlocks when out of scope.

Definition at line 48 of file dumb_spinlock.hpp.

References unlock().

48 { unlock(); }
void unlock()
Unlocks it if I locked it.

Here is the call graph for this function:

Member Function Documentation

bool foedus::assorted::DumbSpinlock::is_locked_by_me ( ) const
inline

Definition at line 50 of file dumb_spinlock.hpp.

Referenced by foedus::storage::masstree::GrowFirstLayerRoot::run().

50 { return locked_by_me_; }

Here is the caller graph for this function:

void foedus::assorted::DumbSpinlock::lock ( )
inline

Locks it if I haven't locked it yet.

This method is idempotent.

Definition at line 53 of file dumb_spinlock.hpp.

References SPINLOCK_WHILE, and try_lock().

Referenced by DumbSpinlock().

53  {
54  SPINLOCK_WHILE(true) {
55  if (try_lock()) {
56  break;
57  }
58  }
59  }
bool try_lock()
try-version of the lock.
#define SPINLOCK_WHILE(x)
A macro to busy-wait (spinlock) with occasional pause.

Here is the call graph for this function:

Here is the caller graph for this function:

bool foedus::assorted::DumbSpinlock::try_lock ( )
inline

try-version of the lock.

Definition at line 61 of file dumb_spinlock.hpp.

References ASSERT_ND.

Referenced by lock(), and foedus::storage::masstree::GrowFirstLayerRoot::run().

61  {
62  if (locked_by_me_) {
63  return true; // already locked
64  }
65 
66  bool expected = false;
67  if (raw_atomic_compare_exchange_weak<bool>(locked_, &expected, true)) {
68  ASSERT_ND(*locked_);
69  locked_by_me_ = true;
70  return true;
71  } else {
72  ASSERT_ND(expected);
73  return false;
74  }
75  }
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72

Here is the caller graph for this function:

void foedus::assorted::DumbSpinlock::unlock ( )
inline

Unlocks it if I locked it.

This method is idempotent. You can safely call many times.

Definition at line 78 of file dumb_spinlock.hpp.

References ASSERT_ND, and foedus::assorted::memory_fence_acq_rel().

Referenced by ~DumbSpinlock().

78  {
79  if (locked_by_me_) {
80  ASSERT_ND(*locked_);
81  locked_by_me_ = false;
83  *locked_ = false;
85  }
86  }
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
void memory_fence_acq_rel()
Equivalent to std::atomic_thread_fence(std::memory_order_acq_rel).

Here is the call graph for this function:

Here is the caller graph for this function:


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