libfoedus-core
FOEDUS Core Library
foedus::assorted::SpinlockStat Struct Reference

Helper for SPINLOCK_WHILE. More...

Detailed Description

Helper for SPINLOCK_WHILE.

Definition at line 233 of file assorted_func.hpp.

#include <assorted_func.hpp>

Collaboration diagram for foedus::assorted::SpinlockStat:

Public Member Functions

 SpinlockStat ()
 
void yield_backoff ()
 

Public Attributes

uint32_t spins_
 
UniformRandom rnd_
 

Constructor & Destructor Documentation

foedus::assorted::SpinlockStat::SpinlockStat ( )
inline

Definition at line 234 of file assorted_func.hpp.

Member Function Documentation

void foedus::assorted::SpinlockStat::yield_backoff ( )

Definition at line 206 of file assorted_func.cpp.

References foedus::assorted::UniformRandom::next_uint32(), rnd_, foedus::assorted::UniformRandom::set_current_seed(), foedus::assorted::spinlock_yield(), spins_, and foedus::debugging::wait_rdtsc_cycles().

206  {
207  if (spins_ == 0) {
208  // do the real initialization only when we couldn't get a lock.
209  // hopefully 99% cases we don't hit here.
210  std::hash<std::thread::id> h;
211  rnd_.set_current_seed(h(std::this_thread::get_id()));
212  rnd_.next_uint32();
213  }
214  ++spins_;
215 
216  // valgrind is single-threaded. so, we must yield as much as possible to let it run reasonably.
217  if (RUNNING_ON_VALGRIND) {
218  spinlock_yield();
219  return;
220  }
221 
222  // exponential backoff. also do yielding occasionally.
223  const uint64_t kMinSleepCycles = 1ULL << 7; // one atomic CAS
224  const uint64_t kMaxSleepCycles = 1ULL << 12; // even this might be too long..
225  uint64_t wait_cycles_max = spins_ < 5U ? kMinSleepCycles << spins_ : kMaxSleepCycles;
226  uint32_t wait_cycles = kMinSleepCycles + rnd_.next_uint32() % (wait_cycles_max - kMinSleepCycles);
227  debugging::wait_rdtsc_cycles(wait_cycles);
228  if (spins_ % 256U == 0) {
229  spinlock_yield();
230  }
231 }
void wait_rdtsc_cycles(uint64_t cycles)
Wait until the given CPU cycles elapse.
Definition: rdtsc.hpp:60
void set_current_seed(uint64_t seed)
void spinlock_yield()
Invoke _mm_pause(), x86 PAUSE instruction, or something equivalent in the env.

Here is the call graph for this function:

Member Data Documentation

UniformRandom foedus::assorted::SpinlockStat::rnd_

Definition at line 238 of file assorted_func.hpp.

Referenced by yield_backoff().

uint32_t foedus::assorted::SpinlockStat::spins_

Definition at line 237 of file assorted_func.hpp.

Referenced by yield_backoff().


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