8#ifndef META_OCEAN_TRACKING_SLAM_MUTEX_H
9#define META_OCEAN_TRACKING_SLAM_MUTEX_H
19#include <shared_mutex>
109 using Key = std::pair<bool, std::string>;
119 using Map = std::unordered_map<Key, Measurements, PairHash>;
129 std::string
report(
const bool skipZero =
true);
172template <
bool tEnable>
378template <
bool tEnable>
382 ocean_assert(
name_ !=
nullptr);
385template <
bool tEnable>
388 const double seconds = timer_.seconds();
390 const char* name = name_ !=
nullptr ? name_ :
"Unknown";
394 const double milliseconds = seconds * 1000.0;
400 Log::warning() <<
"Write lock acquisition time: " << name <<
" took " << milliseconds <<
"ms";
404 Log::warning() <<
"Read lock acquisition time: " << name <<
" took " << milliseconds <<
"ms";
416template <
bool tEnable>
419 const double seconds = timer_.seconds();
421 const char* name = name_ !=
nullptr ? name_ :
"Unknown";
425 const double milliseconds = seconds * 1000.0;
431 Log::warning() <<
"Write lock hold duration time: " << name <<
" took " << milliseconds <<
"ms";
435 Log::warning() <<
"Read lock hold duration time: " << name <<
" took " << milliseconds <<
"ms";
463 if (
lock_.owns_lock())
472 ocean_assert(
lock_.owns_lock());
485 if (!mutex.try_lock())
514 if (
lock_.owns_lock())
523 ocean_assert(
lock_.owns_lock());
527 if (
lock_.owns_lock())
539 if (!mutex.try_lock())
This class implements a recursive lock object.
Definition Lock.h:31
static MessageObject warning()
Returns the message for warning messages.
Definition Messenger.h:1090
This template class is the base class for all singleton objects.
Definition Singleton.h:71
static LockManager & get()
Returns a reference to the unique object.
Definition Singleton.h:115
This class implements a singleton manager for collecting and reporting lock timing measurements.
Definition Mutex.h:90
Map acquisitionDelayMap_
The map storing acquisition delay measurements for each lock.
Definition Mutex.h:157
static constexpr double holdDurationThresholdMs_
The threshold in milliseconds for hold duration logging, with range [0, infinity),...
Definition Mutex.h:102
std::vector< double > Measurements
Definition of a vector holding duration measurements in seconds.
Definition Mutex.h:114
void addHoldDurationMeasurement(const std::string &name, const bool isWriteLock, const double duration)
Adds a hold duration measurement for a specific lock.
void addAcquisitionDelayMeasurement(const std::string &name, const bool isWriteLock, const double duration)
Adds an acquisition delay measurement for a specific lock.
static constexpr bool isEnabled_
True, to enable lock timing measurements; False, to disable.
Definition Mutex.h:96
Map holdDurationMap_
The map storing hold duration measurements for each lock.
Definition Mutex.h:160
std::pair< bool, std::string > Key
Definition of a key combining a lock type (read/write) and a name.
Definition Mutex.h:109
std::string report(const bool skipZero=true)
Generates a report of all collected lock timing measurements.
std::unordered_map< Key, Measurements, PairHash > Map
Definition of a map mapping keys to measurements.
Definition Mutex.h:119
LockManager()
Creates a new lock manager.
static constexpr double acquisitionDelayThresholdMs_
The threshold in milliseconds for acquisition delay logging, with range [0, infinity),...
Definition Mutex.h:99
Lock lock_
The lock protecting the maps.
Definition Mutex.h:163
LockTimer()=default
Creates a new lock timer without a name.
void reportAcquisitionDelay(const bool)
No-op implementation of acquisition delay reporting.
Definition Mutex.h:239
LockTimer(const char *)
Creates a new lock timer with a name.
Definition Mutex.h:230
void reportHoldDuration(const bool)
No-op implementation of hold duration reporting.
Definition Mutex.h:248
This class implements a helper for timing lock acquisition and hold durations.
Definition Mutex.h:174
void reportHoldDuration(const bool isWriteLock)
Reports the hold duration to the LockManager.
Definition Mutex.h:417
HighPerformanceTimer timer_
The timer for measuring acquisition delay and hold duration.
Definition Mutex.h:208
void reportAcquisitionDelay(const bool isWriteLock)
Reports the acquisition delay to the LockManager.
Definition Mutex.h:386
LockTimer(const char *name)
Creates a new lock timer with a name.
Definition Mutex.h:379
const char * name_
The name of the lock, nullptr if not specified.
Definition Mutex.h:205
LockTimer()=default
Creates a new lock timer without a name.
This class implements a scoped read lock for a shared mutex.
Definition Mutex.h:261
static bool debugIsLocked(Mutex &mutex)
Returns whether a mutex is currently locked (debug only).
Definition Mutex.h:483
void unlock()
Explicitly releases the lock before the scoped object is destroyed.
Definition Mutex.h:470
std::shared_lock< Mutex > lock_
The underlying shared lock.
Definition Mutex.h:313
ReadLock(Mutex &mutex)
Creates a new read lock and acquires the mutex.
Definition Mutex.h:445
ReadLock(ReadLock &&)=default
Move constructor.
ReadLock & operator=(ReadLock &&)=default
Move assignment operator.
~ReadLock()
Destructs the read lock and releases the mutex.
Definition Mutex.h:459
This class implements a scoped write lock for a shared mutex.
Definition Mutex.h:323
void unlock()
Explicitly releases the lock before the scoped object is destroyed.
Definition Mutex.h:521
WriteLock(WriteLock &&)=default
Move constructor.
std::unique_lock< Mutex > lock_
The underlying unique lock.
Definition Mutex.h:375
WriteLock(Mutex &mutex)
Creates a new write lock and acquires the mutex.
Definition Mutex.h:496
WriteLock & operator=(WriteLock &&)=default
Move assignment operator.
~WriteLock()
Destructs the write lock and releases the mutex.
Definition Mutex.h:510
static bool debugIsLocked(Mutex &mutex)
Returns whether a mutex is currently locked (debug only).
Definition Mutex.h:537
std::shared_mutex Mutex
Definition of a mutex supporting read and write locks.
Definition Mutex.h:78
The namespace covering the entire Ocean framework.
Definition Accessor.h:15