110 [[nodiscard]]
virtual bool isLocked() const noexcept = 0;
124 static constexpr auto MANUAL_LOCK = lockPolicy::MANUAL_LOCK;
126 static constexpr auto AUTO_LOCK = lockPolicy::AUTO_LOCK;
128 static constexpr auto TRY_LOCK = lockPolicy::TRY_LOCK;
130 static constexpr auto ADOPT_LOCK = lockPolicy::ADOPT_LOCK;
154 pthread_mutex_t mutex_;
181 [[nodiscard]]
void* nativeHandle() noexcept override;
187 void lock() override;
194 bool tryLock() override;
200 void unlock() override;
239 [[nodiscard]]
bool isLocked() const noexcept override;
245 void lock() override;
252 bool tryLock() override;
258 void unlock() override;
274 template<typename... MUTEX>
276 tuple<MUTEX* ...> m_list;
321 [[nodiscard]]
bool isLocked()
const noexcept override;
327 void lock()
override;
334 bool tryLock()
override;
340 void unlock()
override;
356 if (
const int code = pthread_mutex_init(&this->mutex_,
nullptr);
363 return reinterpret_cast<ul_integer>(&this->mutex_);
368 return &this->mutex_;
372 if (
const int code = pthread_mutex_lock(&this->mutex_);
379 if (
const int code = pthread_mutex_trylock(&this->mutex_);
390 if (
const int code = pthread_mutex_unlock(&this->mutex_);
397 if (
const int code = pthread_mutex_destroy(&this->mutex_);
399 std::cerr <<
"Fatal error: Failed to destroy mutex (pthread_mutex_destroy returned "
400 << code <<
")" << std::endl;
406 : p_mutex_(p_mutex), is_locked(false) {
417 this->is_locked =
true;
422 return this->is_locked;
427 throw sysError(
"Cannot lock uniqueLock: already locked");
429 this->p_mutex_.lock();
430 this->is_locked =
true;
435 throw sysError(
"Cannot try-lock uniqueLock: already locked");
437 this->is_locked = this->p_mutex_.tryLock();
438 return this->is_locked;
442 if (this->is_locked){
443 this->p_mutex_.unlock();
444 this->is_locked =
false;
452template<
typename... MUTEX>
455 (..., this->m_list.template get<IDXES>()->lock());
456 this->is_locked_all =
true;
459template<
typename... MUTEX>
463 bool lock_status[
sizeof...(IDXES)] = {};
464 auto tryLockStatusUpdate = [&](
auto i,
auto* mutex) {
465 if (mutex->tryLock()) {
466 lock_status[i] =
true;
473 ((tryLockStatusUpdate(idx++, this->m_list.template get<IDXES>())), ...);
478 ? this->m_list.template get<IDXES>()->unlock() : void(), ++idx), ...);
480 this->is_locked_all = success;
484template<
typename... MUTEX>
487 (..., this->m_list.template get<IDXES>()->unlock());
490template<
typename... MUTEX>
494template<
typename... MUTEX>
496 : m_list(&mutex...), is_locked_all(false) {
507 this->is_locked_all =
true;
511template<
typename... MUTEX>
513 return this->is_locked_all;
516template<
typename... MUTEX>
518 if (this->is_locked_all)
519 throw sysError(
"Cannot lock multiLock: already locked");
524template<
typename... MUTEX>
526 if (this->is_locked_all)
527 throw sysError(
"Cannot try-lock multiLock: already locked");
529 return this->tryLockAll(
makeSequence<
sizeof...(MUTEX)>());
532template<
typename... MUTEX>
534 if (this->is_locked_all)
538template<
typename... MUTEX>
Compile-time sequence of unsigned integers.
Definition types.h:255
Abstract base class for lock guard implementations.
Definition mutex.h:85
static constexpr auto TRY_LOCK
Constant for try-lock policy.
Definition mutex.h:128
static constexpr auto AUTO_LOCK
Constant for automatic lock policy.
Definition mutex.h:126
lockPolicy
Locking policies for guard construction.
Definition mutex.h:116
lockGuard & operator=(const lockGuard &)=delete
Deleted copy assignment operator.
virtual bool tryLock()=0
Attempts to lock the associated mutex(es) without blocking.
virtual void unlock()=0
Unlocks the associated mutex(es)
virtual bool isLocked() const noexcept=0
Checks if the guard currently holds the lock.
lockGuard()=default
Default constructor.
static constexpr auto MANUAL_LOCK
Constant for manual lock policy.
Definition mutex.h:124
virtual void lock()=0
Locks the associated mutex(es)
virtual ~lockGuard()=default
Virtual destructor.
lockGuard(const lockGuard &)=delete
Deleted copy constructor.
static constexpr auto ADOPT_LOCK
Constant for adopt lock policy.
Definition mutex.h:130
RAII wrapper for multiple mutex locking.
Definition mutex.h:275
void unlock() override
Unlocks all managed mutexes.
Definition mutex.h:533
multiLock & operator=(multiLock &&)=delete
Deleted move assignment operator.
void lock() override
Locks all managed mutexes.
Definition mutex.h:517
bool tryLock() override
Attempts to lock all managed mutexes without blocking.
Definition mutex.h:525
multiLock(multiLock &&)=delete
Deleted move constructor.
multiLock(MUTEX &... mutex)
Constructs a multiLock with AUTO_LOCK policy.
Definition mutex.h:491
bool isLocked() const noexcept override
Checks if all mutexes are currently locked.
Definition mutex.h:512
~multiLock() override
Destructor - automatically unlocks all if locked.
Definition mutex.h:539
Abstract base class for mutex implementations.
Definition mutex.h:30
virtual void unlock()=0
Unlocks the mutex.
virtual void * nativeHandle() noexcept=0
Gets the native handle of the mutex.
mutexBase(const mutexBase &)=delete
Deleted copy constructor.
mutexBase & operator=(const mutexBase &)=delete
Deleted copy assignment operator.
virtual void lock()=0
Locks the mutex, blocking if necessary.
virtual bool tryLock()=0
Attempts to lock the mutex without blocking.
virtual ul_integer id() const =0
Gets a unique identifier for the mutex.
mutexBase()=default
Default constructor.
POSIX thread mutex implementation.
Definition mutex.h:153
pMutex(pMutex &&)=delete
Deleted move constructor.
void unlock() override
Unlocks the mutex.
Definition mutex.h:389
pthread_mutex_t native_handle
Native handle type (pthread_mutex_t)
Definition mutex.h:157
pMutex()
Constructs and initializes the mutex.
Definition mutex.h:355
bool tryLock() override
Attempts to lock the mutex without blocking.
Definition mutex.h:378
ul_integer id() const override
Gets a unique identifier for the mutex.
Definition mutex.h:362
pMutex & operator=(pMutex &&)=delete
Deleted move assignment operator.
~pMutex() override
Destroys the mutex.
Definition mutex.h:396
void * nativeHandle() noexcept override
Gets the native mutex handle.
Definition mutex.h:366
void lock() override
Locks the mutex, blocking if necessary.
Definition mutex.h:371
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Definition printable.h:263
Exception for generic system failure.
Definition error.h:306
Container for multiple heterogeneous elements.
Definition tuple.h:53
RAII wrapper for single mutex locking.
Definition mutex.h:216
void unlock() override
Unlocks the associated mutex.
Definition mutex.h:441
uniqueLock & operator=(uniqueLock &&)=delete
Deleted move assignment operator.
void lock() override
Locks the associated mutex.
Definition mutex.h:425
uniqueLock(uniqueLock &&)=delete
Deleted move constructor.
uniqueLock(pMutex &p_mutex, lockPolicy policy=AUTO_LOCK)
Constructs a uniqueLock with specified policy.
Definition mutex.h:405
bool isLocked() const noexcept override
Checks if the lock is currently held.
Definition mutex.h:421
bool tryLock() override
Attempts to lock the associated mutex without blocking.
Definition mutex.h:433
~uniqueLock() override
Destructor - automatically unlocks if locked.
Definition mutex.h:448
Custom exception classes and callback validation utilities.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
std::uint64_t ul_integer
64-bit unsigned integer type
Definition config.h:274
Main namespace for the project Original.
Definition algorithms.h:21
consteval auto makeSequence() noexcept
Creates an index sequence of given length.
Definition types.h:386
decltype( reverseIndexSequenceImpl(makeSequence< N >())) makeReverseSequence
Creates a reversed index sequence.
Definition types.h:313
Heterogeneous tuple container implementation.