mCRL2
Loading...
Searching...
No Matches
mcrl2::utilities::shared_mutex Class Reference

#include <shared_mutex.h>

Public Member Functions

 shared_mutex ()
 
 ~shared_mutex ()
 
 shared_mutex (const shared_mutex &other)
 The copy/move constructor/assignment should not be called while any lock_guard or shared_guard is alive.
 
 shared_mutex (shared_mutex &&other) noexcept
 
shared_mutex & operator= (const shared_mutex &other)
 
shared_mutex & operator= (shared_mutex &&other) noexcept
 
void lock ()
 Obtain exclusive access, and stop all other threads that use this mutex.
 
bool try_lock ()
 Try to obtain exclusive access without blocking on other exclusive locks.
 
void unlock ()
 Release exclusive access.
 
void lock_shared ()
 Acquires a shared lock on this mutex.
 
bool try_lock_shared ()
 Tries to acquire a shared lock on this mutex without blocking.
 
void unlock_shared ()
 Releases a shared lock on this mutex.
 
bool is_shared_locked () const
 

Private Member Functions

void set_forbidden_flags ()
 
bool is_busy () const
 
void wait_for_busy () const
 Waits for the busy flag to become false.
 
void set_forbidden (bool value)
 

Private Attributes

std::atomic< bool > m_busy_flag = false
 A boolean flag indicating whether this thread is working inside the global aterm pool.
 
std::atomic< bool > m_forbidden_flag = false
 
std::size_t m_lock_depth = 0
 It can happen that un/lock_shared calls are nested, so keep track of the nesting depth and only actually perform un/locking at the root.
 
std::shared_ptr< shared_mutex_data > m_shared
 

Detailed Description

An implementation of a shared mutex (also called readers-write lock in the literature) based on the notion of busy and forbidden flags.

Definition at line 59 of file shared_mutex.h.

Constructor & Destructor Documentation

◆ shared_mutex() [1/3]

mcrl2::utilities::shared_mutex::shared_mutex ( )
inline

Definition at line 62 of file shared_mutex.h.

◆ ~shared_mutex()

mcrl2::utilities::shared_mutex::~shared_mutex ( )
inline

Definition at line 68 of file shared_mutex.h.

◆ shared_mutex() [2/3]

mcrl2::utilities::shared_mutex::shared_mutex ( const shared_mutex &  other)
inline

The copy/move constructor/assignment should not be called while any lock_guard or shared_guard is alive.

Definition at line 74 of file shared_mutex.h.

◆ shared_mutex() [3/3]

mcrl2::utilities::shared_mutex::shared_mutex ( shared_mutex &&  other)
inlinenoexcept

Definition at line 80 of file shared_mutex.h.

Member Function Documentation

◆ is_busy()

bool mcrl2::utilities::shared_mutex::is_busy ( ) const
inlineprivate
Returns
True iff the shared mutex has its busy flag set.

Definition at line 270 of file shared_mutex.h.

◆ is_shared_locked()

bool mcrl2::utilities::shared_mutex::is_shared_locked ( ) const
inline
Returns
True iff the shared mutex is in the shared section

Definition at line 235 of file shared_mutex.h.

◆ lock()

void mcrl2::utilities::shared_mutex::lock ( )
inline

Obtain exclusive access, and stop all other threads that use this mutex.

Equivalent to std::shared_mutex::lock. Blocks until exclusive access is acquired.

Precondition
The calling thread does not hold a (shared) lock on this mutex.
Postcondition
All threads sharing this mutex are suspended outside of their shared sections.

Definition at line 117 of file shared_mutex.h.

◆ lock_shared()

void mcrl2::utilities::shared_mutex::lock_shared ( )
inline

Acquires a shared lock on this mutex.

Equivalent to std::shared_mutex::lock_shared, except that recursive shared locking by the owning thread is explicitly allowed: every lock_shared() must be balanced by exactly one unlock_shared().

Postcondition
No thread can acquire the exclusive lock until the last unlock_shared().

Definition at line 171 of file shared_mutex.h.

◆ operator=() [1/2]

shared_mutex & mcrl2::utilities::shared_mutex::operator= ( const shared_mutex &  other)
inline

Definition at line 86 of file shared_mutex.h.

◆ operator=() [2/2]

shared_mutex & mcrl2::utilities::shared_mutex::operator= ( shared_mutex &&  other)
inlinenoexcept

Definition at line 99 of file shared_mutex.h.

◆ set_forbidden()

void mcrl2::utilities::shared_mutex::set_forbidden ( bool  value)
inlineprivate

Definition at line 283 of file shared_mutex.h.

◆ set_forbidden_flags()

void mcrl2::utilities::shared_mutex::set_forbidden_flags ( )
inlineprivate

Definition at line 242 of file shared_mutex.h.

◆ try_lock()

bool mcrl2::utilities::shared_mutex::try_lock ( )
inline

Try to obtain exclusive access without blocking on other exclusive locks.

Equivalent to std::shared_mutex::try_lock.

Precondition
The calling thread does not hold a (shared) lock on this mutex.
Returns
True iff exclusive access was acquired.

Definition at line 133 of file shared_mutex.h.

◆ try_lock_shared()

bool mcrl2::utilities::shared_mutex::try_lock_shared ( )
inline

Tries to acquire a shared lock on this mutex without blocking.

Equivalent to std::shared_mutex::try_lock_shared.

Returns
True iff the shared lock was acquired.

Definition at line 199 of file shared_mutex.h.

◆ unlock()

void mcrl2::utilities::shared_mutex::unlock ( )
inline

Release exclusive access.

Equivalent to std::shared_mutex::unlock.

Precondition
The calling thread holds the exclusive lock.

Definition at line 152 of file shared_mutex.h.

◆ unlock_shared()

void mcrl2::utilities::shared_mutex::unlock_shared ( )
inline

Releases a shared lock on this mutex.

Equivalent to std::shared_mutex::unlock_shared.

Precondition
The calling thread holds a shared lock on this mutex.

Definition at line 222 of file shared_mutex.h.

◆ wait_for_busy()

void mcrl2::utilities::shared_mutex::wait_for_busy ( ) const
inlineprivate

Waits for the busy flag to become false.

Definition at line 277 of file shared_mutex.h.

Member Data Documentation

◆ m_busy_flag

std::atomic<bool> mcrl2::utilities::shared_mutex::m_busy_flag = false
private

A boolean flag indicating whether this thread is working inside the global aterm pool.

Definition at line 289 of file shared_mutex.h.

◆ m_forbidden_flag

std::atomic<bool> mcrl2::utilities::shared_mutex::m_forbidden_flag = false
private

Definition at line 290 of file shared_mutex.h.

◆ m_lock_depth

std::size_t mcrl2::utilities::shared_mutex::m_lock_depth = 0
private

It can happen that un/lock_shared calls are nested, so keep track of the nesting depth and only actually perform un/locking at the root.

Definition at line 294 of file shared_mutex.h.

◆ m_shared

std::shared_ptr<shared_mutex_data> mcrl2::utilities::shared_mutex::m_shared
private

Definition at line 296 of file shared_mutex.h.


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