Permalink
Browse files

spinlocks: add exp backoff mechanism guarded by macro

  • Loading branch information...
stephentu committed Aug 28, 2013
1 parent ab61b86 commit d8f26f0b70b586891fdf339dff4195b53590e408
Showing with 33 additions and 1 deletion.
  1. +26 −0 btree.h
  2. +4 −0 macros.h
  3. +1 −0 spinlock.h
  4. +2 −1 tuple.h
View
26 btree.h
@@ -81,22 +81,48 @@ namespace private_ {
static inline void
Lock(std::atomic<uint64_t> &t)
{
#ifdef SPINLOCK_BACKOFF
uint64_t backoff_shift = 0;
#endif
uint64_t v = Load(t);
while ((v & P::HDR_LOCKED_MASK) ||
!t.compare_exchange_strong(v, v | P::HDR_LOCKED_MASK)) {
#ifdef SPINLOCK_BACKOFF
if (backoff_shift < 63)
backoff_shift++;
uint64_t spins = (1UL << backoff_shift) * BACKOFF_SPINS_FACTOR;
while (spins) {
nop_pause();
spins--;
}
#else
nop_pause();
#endif
v = Load(t);
}
COMPILER_MEMORY_FENCE;
}
static inline unsigned
LockWithSpinCount(std::atomic<uint64_t> &t)
{
#ifdef SPINLOCK_BACKOFF
uint64_t backoff_shift = 0;
#endif
unsigned spins = 0;
uint64_t v = Load(t);
while ((v & P::HDR_LOCKED_MASK) ||
!t.compare_exchange_strong(v, v | P::HDR_LOCKED_MASK)) {
#ifdef SPINLOCK_BACKOFF
if (backoff_shift < 63)
backoff_shift++;
uint64_t backoff_spins = (1UL << backoff_shift) * BACKOFF_SPINS_FACTOR;
while (backoff_spins) {
nop_pause();
backoff_spins--;
}
#else
nop_pause();
#endif
v = Load(t);
spins++;
}
View
@@ -92,6 +92,10 @@
#define SMALL_SIZE_MAP 64
#define EXTRA_SMALL_SIZE_MAP 8
//#define BACKOFF_SPINS_FACTOR 1000
//#define BACKOFF_SPINS_FACTOR 100
#define BACKOFF_SPINS_FACTOR 10
// throw exception after the assert(), so that GCC knows
// we'll never return
#define NDB_UNIMPLEMENTED(what) \
View
@@ -18,6 +18,7 @@ class spinlock {
inline void
lock()
{
// XXX: implement SPINLOCK_BACKOFF
uint32_t v = value;
while (v || !__sync_bool_compare_and_swap(&value, 0, 1)) {
nop_pause();
View
@@ -368,9 +368,10 @@ struct dbtuple {
inline version_t
lock(bool write_intent)
{
// XXX: implement SPINLOCK_BACKOFF
CheckMagic();
#ifdef ENABLE_EVENT_COUNTERS
unsigned long nspins = 0;
unsigned nspins = 0;
#endif
version_t v = hdr;
const version_t lockmask = write_intent ?

0 comments on commit d8f26f0

Please sign in to comment.