Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
impala::SpinLock Class Reference

Lightweight spinlock. More...

#include <spinlock.h>

Collaboration diagram for impala::SpinLock:

Public Member Functions

 SpinLock ()
 
void lock ()
 Acquires the lock, spins until the lock becomes available. More...
 
void unlock ()
 
bool try_lock ()
 Tries to acquire the lock. More...
 
void DCheckLocked ()
 

Private Member Functions

void SlowAcquire ()
 

Private Attributes

bool locked_
 TODO: pad this to be a cache line? More...
 

Static Private Attributes

static const int NUM_SPIN_CYCLES = 70
 

Detailed Description

Lightweight spinlock.

Definition at line 24 of file spinlock.h.

Constructor & Destructor Documentation

impala::SpinLock::SpinLock ( )
inline

Definition at line 26 of file spinlock.h.

Member Function Documentation

void impala::SpinLock::DCheckLocked ( )
inline

Definition at line 43 of file spinlock.h.

References locked_.

Referenced by impala::exec_summary().

void impala::SpinLock::lock ( )
inline

Acquires the lock, spins until the lock becomes available.

Definition at line 29 of file spinlock.h.

References SlowAcquire(), and try_lock().

Referenced by impala::StreamingSampler< int64_t, 64 >::GetSamples().

void impala::SpinLock::SlowAcquire ( )
private

Out-of-line definition of the actual spin loop. The primary goal is to have the actual lock method as short as possible to avoid polluting the i-cache with unnecessary instructions in the non-contested case.

Definition at line 18 of file spinlock.cc.

References impala::AtomicUtil::CpuWait(), NUM_SPIN_CYCLES, and try_lock().

Referenced by lock().

bool impala::SpinLock::try_lock ( )
inline

Tries to acquire the lock.

Definition at line 41 of file spinlock.h.

References locked_.

Referenced by lock(), and SlowAcquire().

void impala::SpinLock::unlock ( )
inline

Member Data Documentation

bool impala::SpinLock::locked_
private

TODO: pad this to be a cache line?

Definition at line 67 of file spinlock.h.

Referenced by DCheckLocked(), try_lock(), and unlock().

const int impala::SpinLock::NUM_SPIN_CYCLES = 70
staticprivate

In typical spin lock implements, we want to spin (and keep the core fully busy), for some number of cycles before yielding. Consider these three cases: 1) lock is un-contended - spinning doesn't kick in and has no effect. 2) lock is taken by another thread and that thread finishes quickly. 3) lock is taken by another thread and that thread is slow (e.g. scheduled away).

In case 3), we'd want to yield so another thread can do work. This thread won't be able to do anything useful until the thread with the lock runs again. In case 2), we don't want to yield (and give up our scheduling time slice) since we will get to run soon after. To try to get the best of everything, we will busy spin for a while before yielding to another thread. TODO: how do we set this.

Definition at line 65 of file spinlock.h.

Referenced by SlowAcquire().


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