libfoedus-core
FOEDUS Core Library
foedus::xct::McsRwLock Struct Reference

An MCS reader-writer lock data structure. More...

Detailed Description

An MCS reader-writer lock data structure.

This implements a fair reader-writer lock by the original authors of MCS lock [PPoPP 1991]. The version implemented here includes a bug fix due to Keir Fraser (University of Cambridge). See https://www.cs.rochester.edu/research/synchronization/pseudocode/rw.html#s_f for the original pseudocode with the fix.

The major use case so far is row-level locking for 2PL.

The assumption is that a thread at any instant can be waiting for only one MCS lock, so knowing the thread ID suffices to locate the block index as well.

TODO(tzwang): add the ownerless variant.

Definition at line 795 of file xct_id.hpp.

#include <xct_id.hpp>

Public Member Functions

 McsRwLock ()
 
 McsRwLock (const McsRwLock &other)=delete
 
McsRwLockoperator= (const McsRwLock &other)=delete
 
void reset ()
 
void increment_nreaders ()
 
uint16_t decrement_nreaders ()
 
uint16_t nreaders ()
 
McsBlockIndex get_tail_waiter_block () const
 
thread::ThreadId get_tail_waiter () const
 
bool has_next_writer () const
 
void set_next_writer (thread::ThreadId thread_id)
 
thread::ThreadId get_next_writer ()
 
thread::ThreadId xchg_next_writer (thread::ThreadId id)
 
bool cas_next_writer_weak (thread::ThreadId expected, thread::ThreadId desired)
 
bool cas_next_writer_strong (thread::ThreadId expected, thread::ThreadId desired)
 
uint32_t xchg_tail (uint32_t new_tail)
 
bool cas_tail_strong (uint32_t expected, uint32_t desired)
 
bool cas_tail_weak (uint32_t expected, uint32_t desired)
 
uint32_t get_tail_int ()
 
bool is_locked () const
 

Static Public Member Functions

static uint32_t to_tail_int (thread::ThreadId tail_waiter, McsBlockIndex tail_waiter_block)
 

Public Attributes

uint32_t tail_
 
thread::ThreadId next_writer_
 
uint16_t nreaders_
 

Static Public Attributes

static const thread::ThreadId kNextWriterNone = 0xFFFFU
 

Friends

std::ostream & operator<< (std::ostream &o, const McsRwLock &v)
 

Constructor & Destructor Documentation

foedus::xct::McsRwLock::McsRwLock ( )
inline

Definition at line 798 of file xct_id.hpp.

References reset().

798 { reset(); }

Here is the call graph for this function:

foedus::xct::McsRwLock::McsRwLock ( const McsRwLock other)
delete

Member Function Documentation

bool foedus::xct::McsRwLock::cas_next_writer_strong ( thread::ThreadId  expected,
thread::ThreadId  desired 
)
inline

Definition at line 835 of file xct_id.hpp.

References next_writer_.

835  {
836  return assorted::raw_atomic_compare_exchange_strong<thread::ThreadId>(
837  &next_writer_, &expected, desired);
838  }
thread::ThreadId next_writer_
Definition: xct_id.hpp:867
bool foedus::xct::McsRwLock::cas_next_writer_weak ( thread::ThreadId  expected,
thread::ThreadId  desired 
)
inline

Definition at line 831 of file xct_id.hpp.

References next_writer_.

831  {
832  return assorted::raw_atomic_compare_exchange_weak<thread::ThreadId>(
833  &next_writer_, &expected, desired);
834  }
thread::ThreadId next_writer_
Definition: xct_id.hpp:867
bool foedus::xct::McsRwLock::cas_tail_strong ( uint32_t  expected,
uint32_t  desired 
)
inline

Definition at line 842 of file xct_id.hpp.

References tail_.

842  {
843  return assorted::raw_atomic_compare_exchange_strong<uint32_t>(&tail_, &expected, desired);
844  }
bool foedus::xct::McsRwLock::cas_tail_weak ( uint32_t  expected,
uint32_t  desired 
)
inline

Definition at line 845 of file xct_id.hpp.

References tail_.

845  {
846  return assorted::raw_atomic_compare_exchange_weak<uint32_t>(&tail_, &expected, desired);
847  }
uint16_t foedus::xct::McsRwLock::decrement_nreaders ( )
inline

Definition at line 811 of file xct_id.hpp.

References nreaders_.

Referenced by foedus::xct::McsImpl< ADAPTOR, McsRwSimpleBlock >::release_rw_reader().

811  {
812  return assorted::raw_atomic_fetch_add<uint16_t>(&nreaders_, -1);
813  }

Here is the caller graph for this function:

thread::ThreadId foedus::xct::McsRwLock::get_next_writer ( )
inline

Definition at line 825 of file xct_id.hpp.

References next_writer_.

Referenced by foedus::xct::McsImpl< ADAPTOR, McsRwSimpleBlock >::acquire_unconditional_rw_writer().

825  {
826  return assorted::atomic_load_acquire<thread::ThreadId>(&next_writer_);
827  }
thread::ThreadId next_writer_
Definition: xct_id.hpp:867

Here is the caller graph for this function:

uint32_t foedus::xct::McsRwLock::get_tail_int ( )
inline

Definition at line 854 of file xct_id.hpp.

References tail_.

854  {
855  return assorted::atomic_load_acquire<uint32_t>(&tail_);
856  }
thread::ThreadId foedus::xct::McsRwLock::get_tail_waiter ( ) const
inline
McsBlockIndex foedus::xct::McsRwLock::get_tail_waiter_block ( ) const
inline
bool foedus::xct::McsRwLock::has_next_writer ( ) const
inline

Definition at line 819 of file xct_id.hpp.

References next_writer_.

819  {
820  return assorted::atomic_load_acquire<thread::ThreadId>(&next_writer_) != kNextWriterNone;
821  }
thread::ThreadId next_writer_
Definition: xct_id.hpp:867
static const thread::ThreadId kNextWriterNone
Definition: xct_id.hpp:796
void foedus::xct::McsRwLock::increment_nreaders ( )
inline

Definition at line 808 of file xct_id.hpp.

References nreaders_.

Referenced by foedus::xct::McsImpl< ADAPTOR, McsRwSimpleBlock >::acquire_unconditional_rw_reader(), foedus::xct::McsImpl< ADAPTOR, McsRwSimpleBlock >::release_rw_writer(), and foedus::xct::McsImpl< ADAPTOR, McsRwSimpleBlock >::retry_async_rw_reader().

808  {
809  assorted::raw_atomic_fetch_add<uint16_t>(&nreaders_, 1);
810  }

Here is the caller graph for this function:

bool foedus::xct::McsRwLock::is_locked ( ) const
inline

Definition at line 857 of file xct_id.hpp.

References nreaders_, and tail_.

Referenced by foedus::xct::RwLockableXctId::is_keylocked(), foedus::xct::operator<<(), and foedus::storage::masstree::MasstreeStoragePimpl::verify_single_thread_border().

857  {
858  return (tail_ & 0xFFFFU) != 0 || nreaders_ > 0;
859  }

Here is the caller graph for this function:

uint16_t foedus::xct::McsRwLock::nreaders ( )
inline

Definition at line 814 of file xct_id.hpp.

References nreaders_.

Referenced by foedus::xct::McsImpl< ADAPTOR, McsRwSimpleBlock >::acquire_unconditional_rw_writer(), and foedus::xct::McsImpl< ADAPTOR, McsRwSimpleBlock >::release_rw_reader().

814  {
815  return assorted::atomic_load_acquire<uint16_t>(&nreaders_);
816  }

Here is the caller graph for this function:

McsRwLock& foedus::xct::McsRwLock::operator= ( const McsRwLock other)
delete
void foedus::xct::McsRwLock::reset ( )
inline
void foedus::xct::McsRwLock::set_next_writer ( thread::ThreadId  thread_id)
inline

Definition at line 822 of file xct_id.hpp.

References xchg_next_writer().

Referenced by reset().

822  {
823  xchg_next_writer(thread_id); // sub-word access...
824  }
thread::ThreadId xchg_next_writer(thread::ThreadId id)
Definition: xct_id.hpp:828

Here is the call graph for this function:

Here is the caller graph for this function:

thread::ThreadId foedus::xct::McsRwLock::xchg_next_writer ( thread::ThreadId  id)
inline

Definition at line 828 of file xct_id.hpp.

References next_writer_.

Referenced by set_next_writer().

828  {
829  return assorted::raw_atomic_exchange<thread::ThreadId>(&next_writer_, id);
830  }
thread::ThreadId next_writer_
Definition: xct_id.hpp:867

Here is the caller graph for this function:

uint32_t foedus::xct::McsRwLock::xchg_tail ( uint32_t  new_tail)
inline

Definition at line 839 of file xct_id.hpp.

References tail_.

839  {
840  return assorted::raw_atomic_exchange<uint32_t>(&tail_, new_tail);
841  }

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  o,
const McsRwLock v 
)
friend

Definition at line 195 of file xct_id.cpp.

195  {
196  o << "<McsRwLock><locked>" << v.is_locked() << "</locked><tail_waiter>"
197  << v.get_tail_waiter() << "</tail_waiter><tail_block>" << v.get_tail_waiter_block()
198  << "</tail_block></McsRwLock>";
199  return o;
200 }

Member Data Documentation

uint16_t foedus::xct::McsRwLock::nreaders_

Definition at line 868 of file xct_id.hpp.

Referenced by decrement_nreaders(), increment_nreaders(), is_locked(), nreaders(), and reset().


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