libfoedus-core
FOEDUS Core Library
foedus::soc::SharedMutex Class Referencefinal

A mutex that can be placed in shared memory and used from multiple processes. More...

Detailed Description

A mutex that can be placed in shared memory and used from multiple processes.

C++11's mutex doesn't work for multi-process. We need to directly manipulate pthread_mutexattr_setpshared, hence this class. This object is also shared-memory friendly, meaning it has no heap-allocated member. It can be reset to a usable state via memset(zero), too.

Example usage:

mtx.initialize(); // omit this if you invoked constructor. be careful on reinterpret_cast
mtx.lock();
do_something();
mtx.unlock(); // or automatically released when mtx gets out of scope
mtx.uninitialize(); // same above, but not the end of the world

Definition at line 47 of file shared_mutex.hpp.

#include <shared_mutex.hpp>

Public Member Functions

 SharedMutex ()
 
 ~SharedMutex ()
 
 SharedMutex (const SharedMutex &)=delete
 
SharedMutexoperator= (const SharedMutex &)=delete
 
void initialize (bool recursive=false)
 
void uninitialize ()
 
bool is_initialized () const
 
bool is_recursive () const
 
void lock ()
 Unconditionally lock. More...
 
bool timedlock (uint64_t timeout_nanosec)
 Try lock up to the given timeout. More...
 
bool trylock ()
 Instantaneously try the lock. More...
 
void unlock ()
 Unlock it. More...
 
pthread_mutex_t * get_raw_mutex ()
 

Constructor & Destructor Documentation

foedus::soc::SharedMutex::SharedMutex ( )
inline

Definition at line 49 of file shared_mutex.hpp.

References initialize().

49 : initialized_(false), recursive_(false) { initialize(); }
void initialize(bool recursive=false)

Here is the call graph for this function:

foedus::soc::SharedMutex::~SharedMutex ( )
inline

Definition at line 50 of file shared_mutex.hpp.

References uninitialize().

Here is the call graph for this function:

foedus::soc::SharedMutex::SharedMutex ( const SharedMutex )
delete

Member Function Documentation

pthread_mutex_t* foedus::soc::SharedMutex::get_raw_mutex ( )
inline

Definition at line 80 of file shared_mutex.hpp.

Referenced by foedus::soc::SharedCond::timedwait(), and foedus::soc::SharedCond::wait().

80 { return &mutex_; }

Here is the caller graph for this function:

void foedus::soc::SharedMutex::initialize ( bool  recursive = false)

Definition at line 29 of file shared_mutex.cpp.

References ASSERT_ND, and uninitialize().

Referenced by foedus::log::MetaLogControlBlock::initialize(), foedus::proc::ProcManagerControlBlock::initialize(), foedus::savepoint::SavepointManagerControlBlock::initialize(), foedus::memory::PagePoolControlBlock::initialize(), foedus::storage::StorageManagerControlBlock::initialize(), foedus::log::LogManagerControlBlock::initialize(), foedus::soc::SharedCond::initialize(), foedus::thread::ThreadControlBlock::initialize(), foedus::storage::StorageControlBlock::initialize(), foedus::log::LoggerControlBlock::initialize(), foedus::storage::PartitionerMetadata::initialize(), and SharedMutex().

29  {
30  uninitialize();
31  int attr_ret = ::pthread_mutexattr_init(&attr_);
32  ASSERT_ND(attr_ret == 0);
33 
34  int shared_ret = ::pthread_mutexattr_setpshared(&attr_, PTHREAD_PROCESS_SHARED);
35  ASSERT_ND(shared_ret == 0);
36 
37  int type_ret = ::pthread_mutexattr_settype(
38  &attr_,
39  recursive ? PTHREAD_MUTEX_RECURSIVE_NP : PTHREAD_MUTEX_FAST_NP);
40  ASSERT_ND(type_ret == 0);
41 
42  int mutex_ret = ::pthread_mutex_init(&mutex_, &attr_);
43  ASSERT_ND(mutex_ret == 0);
44 
45  recursive_ = recursive;
46  initialized_ = true;
47 }
#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 call graph for this function:

Here is the caller graph for this function:

bool foedus::soc::SharedMutex::is_recursive ( ) const
inline

Definition at line 59 of file shared_mutex.hpp.

59 { return recursive_; }
void foedus::soc::SharedMutex::lock ( )

Unconditionally lock.

Definition at line 63 of file shared_mutex.cpp.

References ASSERT_ND.

Referenced by foedus::soc::SharedMutexScope::lock().

63  {
64  ASSERT_ND(initialized_);
65  int ret = ::pthread_mutex_lock(&mutex_);
66  ASSERT_ND(ret == 0);
67 }
#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:

SharedMutex& foedus::soc::SharedMutex::operator= ( const SharedMutex )
delete
bool foedus::soc::SharedMutex::timedlock ( uint64_t  timeout_nanosec)

Try lock up to the given timeout.

Parameters
[in]timeout_nanosectimeout in nanoseconds
Returns
whether this thread acquired the lock

Definition at line 69 of file shared_mutex.cpp.

References ASSERT_ND, CXX11_NULLPTR, and trylock().

69  {
70  if (timeout_nanosec == 0) {
71  return trylock();
72  }
73 
74  ASSERT_ND(initialized_);
75  struct timespec timeout;
76  struct timeval now;
77  ::gettimeofday(&now, CXX11_NULLPTR);
78  timeout.tv_sec = now.tv_sec + (timeout_nanosec / 1000000000ULL);
79  timeout.tv_nsec = now.tv_usec * 1000ULL + timeout_nanosec % 1000000000ULL;
80  timeout.tv_sec += (timeout.tv_nsec) / 1000000000ULL;
81  timeout.tv_nsec %= 1000000000ULL;
82  int ret = ::pthread_mutex_timedlock(&mutex_, &timeout);
83  ASSERT_ND(ret == 0 || ret == ETIMEDOUT);
84  return ret == 0;
85 }
#define CXX11_NULLPTR
Used in public headers in place of "nullptr" of C++11.
Definition: cxx11.hpp:132
bool trylock()
Instantaneously try the lock.
#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 call graph for this function:

bool foedus::soc::SharedMutex::trylock ( )

Instantaneously try the lock.

Returns
whether this thread acquired the lock

Definition at line 87 of file shared_mutex.cpp.

References ASSERT_ND.

Referenced by timedlock().

87  {
88  ASSERT_ND(initialized_);
89  int ret = ::pthread_mutex_trylock(&mutex_);
90  return ret == 0;
91 }
#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::soc::SharedMutex::uninitialize ( )

Definition at line 49 of file shared_mutex.cpp.

References ASSERT_ND.

Referenced by initialize(), foedus::proc::ProcManagerControlBlock::uninitialize(), foedus::log::MetaLogControlBlock::uninitialize(), foedus::memory::PagePoolControlBlock::uninitialize(), foedus::storage::StorageManagerControlBlock::uninitialize(), foedus::soc::SharedCond::uninitialize(), foedus::savepoint::SavepointManagerControlBlock::uninitialize(), foedus::log::LogManagerControlBlock::uninitialize(), foedus::storage::StorageControlBlock::uninitialize(), foedus::thread::ThreadControlBlock::uninitialize(), foedus::log::LoggerControlBlock::uninitialize(), foedus::storage::PartitionerMetadata::uninitialize(), and ~SharedMutex().

49  {
50  if (!initialized_) {
51  return;
52  }
53 
54  int mutex_ret = ::pthread_mutex_destroy(&mutex_);
55  ASSERT_ND(mutex_ret == 0);
56 
57  int attr_ret = ::pthread_mutexattr_destroy(&attr_);
58  ASSERT_ND(attr_ret == 0);
59 
60  initialized_ = false;
61 }
#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::soc::SharedMutex::unlock ( )

Unlock it.

Definition at line 93 of file shared_mutex.cpp.

References ASSERT_ND.

Referenced by foedus::soc::SharedMutexScope::unlock().

93  {
94  ASSERT_ND(initialized_);
95  int ret = ::pthread_mutex_unlock(&mutex_);
96  ASSERT_ND(ret == 0);
97 }
#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:


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