8#ifndef META_OCEAN_BASE_LOCK_H
9#define META_OCEAN_BASE_LOCK_H
30class OCEAN_BASE_EXPORT
Lock
58 inline bool isLocked();
97template <
bool tActive>
188template <
bool tActive>
247 OCEAN_SUPPRESS_UNUSED_WARNING(lock);
256 OCEAN_SUPPRESS_UNUSED_WARNING(lock);
289 [[nodiscard]]
inline Lock* lock()
const;
294 inline void release();
302 inline void relock(
Lock& lock);
308 inline bool isReleased()
const;
339template <
typename TScopedLock,
typename TLock = Lock>
412 [[nodiscard]]
inline Lock* lock()
const;
441#elif defined(__APPLE__) || defined(__linux__) || defined(__EMSCRIPTEN__)
443 pthread_mutexattr_t mutexAttribute;
444 pthread_mutexattr_init(&mutexAttribute);
445 pthread_mutexattr_settype(&mutexAttribute, PTHREAD_MUTEX_RECURSIVE);
446 pthread_mutex_init(&
mutex_, &mutexAttribute);
450 pthread_mutexattr_t mutexAttribute = PTHREAD_MUTEX_RECURSIVE;
451 pthread_mutex_init(&
mutex_, &mutexAttribute);
467 pthread_mutex_destroy(&
mutex_);
481 pthread_mutex_lock(&
mutex_);
496 pthread_mutex_unlock(&
mutex_);
514 if (pthread_mutex_trylock(&
mutex_) == 0)
516 pthread_mutex_unlock(&
mutex_);
536template <
bool tActive>
543template <
bool tActive>
550template <
bool tActive>
559 ocean_assert(
lock_ !=
nullptr);
565 if (
lock_ !=
nullptr)
578 ocean_assert(!
isReleased() &&
"This TemporaryScopedLock object has been released before");
580 if (
lock_ !=
nullptr)
589 ocean_assert(
isReleased() &&
"This TemporaryScopedLock object must be released before");
591 if (
lock_ ==
nullptr)
600 return lock_ ==
nullptr;
606 if (
lock_ !=
nullptr)
624 if (
lock_ !=
nullptr)
635template <
typename TScopedLock,
typename TLock>
637 scopedLockFirst_(&lockA < &lockB ? lockA : lockB),
638 scopedLockSecond_(&lockA < &lockB ? lockB : lockA)
This class implements a scoped lock object that locks two lock objects in a deterministic order based...
Definition Lock.h:341
TScopedLock scopedLockFirst_
Scopedlock object holding a lock on the lock with the lowest memory address.
Definition Lock.h:375
DualScopedLockT(TLock &lockA, TLock &lockB)
Creates a new dual scoped lock object based on two given lock objects.
Definition Lock.h:636
DualScopedLockT & operator=(const DualScopedLockT< TScopedLock, TLock > &object)=delete
Disabled accessible assigns operator.
~DualScopedLockT()=default
Destructs the dual scoped lock and unlocks both internal lock objects in reverse order.
DualScopedLockT(const DualScopedLockT< TScopedLock, TLock > &object)=delete
Disabled accessible copy operator.
TScopedLock scopedLockSecond_
Scoped lock object holding a lock on the lock with the highest memory address.
Definition Lock.h:378
This class implements a recursive lock object.
Definition Lock.h:31
CRITICAL_SECTION criticalSection_
Critical section object of windows.
Definition Lock.h:80
void lock()
Locks the critical section.
Definition Lock.h:473
~Lock()
Destructs a lock object.
Definition Lock.h:457
bool isLocked()
Returns whether this critical section is locked by another thread.
Definition Lock.h:502
Lock & operator=(const Lock &lock)=delete
Disabled assign operator.
void unlock()
Unlocks the critical section.
Definition Lock.h:487
Lock(const Lock &lock)=delete
Disabled copy constructor.
Lock()
Creates a new lock object.
Definition Lock.h:435
pthread_mutex_t mutex_
Pthread mutex object.
Definition Lock.h:85
This class implements an optional recursive scoped lock object locking the lock object only if it's d...
Definition Lock.h:387
~OptionalScopedLock()
Destructs an optional scoped lock and unlocks the internal lock object if defined.
Definition Lock.h:622
Lock * lock() const
Returns the lock object which (if existing) is locked during the existence of this scoped lock object...
Definition Lock.h:630
OptionalScopedLock & operator=(const OptionalScopedLock &object)=delete
Disabled accessible assigns operator.
Lock * lock_
Lock object which is locked during the existence of this scoped lock object.
Definition Lock.h:432
OptionalScopedLock(Lock *lock)
Creates a new optional scoped lock object by a given lock object.
Definition Lock.h:603
OptionalScopedLock(const OptionalScopedLock &object)=delete
Disabled accessible copy operator.
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
~ScopedLock()
Destructs a scoped lock and unlocks the internal lock object.
Definition Lock.h:531
ScopedLock(const ScopedLock &object)=delete
Disabled accessible copy operator.
ScopedLock & operator=(const ScopedLock &object)=delete
Disabled accessible assigns operator.
ScopedLock(Lock &lock)
Creates a new scoped lock object by a given lock object.
Definition Lock.h:525
Lock & lock_
Lock object which is locked during the existence of this scoped lock object.
Definition Lock.h:179
TemplatedLock & operator=(const TemplatedLock &lock)=delete
Disabled assign operator.
TemplatedLock(const TemplatedLock &lock)=delete
Disabled copy constructor.
TemplatedLock()=default
Creates a new lock object.
This class implements a template-based recursive lock object.
Definition Lock.h:99
TemplatedLock()=default
Creates a new lock object.
TemplatedScopedLock(Lock &lock)
Creates a new scoped lock object by a given lock object.
Definition Lock.h:245
TemplatedScopedLock(TemplatedLock< false > &lock)
Creates a new scoped lock object by a given lock object.
Definition Lock.h:254
This class implements a recursive scoped lock object that is activated by a boolean template paramete...
Definition Lock.h:190
TemplatedScopedLock(Lock &lock)
Creates a new scoped lock object by a given lock object.
Definition Lock.h:537
TemplatedScopedLock & operator=(const TemplatedScopedLock< tActive > &object)=delete
Disabled accessible assigns operator.
TemplatedScopedLock(const TemplatedScopedLock< tActive > &object)=delete
Disabled accessible copy operator.
~TemplatedScopedLock()
Destructs a scoped lock and unlocks the internal lock object.
Definition Lock.h:551
Lock & lock_
Lock object which is locked during the existence of this scoped lock object.
Definition Lock.h:228
This class implements a recursive scoped lock object allowing to release the lock before the scoped o...
Definition Lock.h:266
void relock(Lock &lock)
Re-locks this scoped lock with a given lock.
Definition Lock.h:587
TemporaryScopedLock(const TemporaryScopedLock &object)=delete
Disabled accessible copy operator.
Lock * lock() const
Returns the lock object which (if existing) is locked during the existence of this scoped lock object...
Definition Lock.h:571
void release()
Explicitly releases the lock before the scoped lock object is released.
Definition Lock.h:576
TemporaryScopedLock()=default
Creates a new scoped lock object which is not locked yet.
Lock * lock_
Lock object which is locked during the existence of this scoped lock object.
Definition Lock.h:328
TemporaryScopedLock & operator=(const TemporaryScopedLock &object)=delete
Disabled accessible assigns operator.
bool isReleased() const
Returns whether this scoped lock is released already.
Definition Lock.h:598
~TemporaryScopedLock()
Destructs a scoped lock and unlocks the internal lock object (if still locked).
Definition Lock.h:563
The namespace covering the entire Ocean framework.
Definition Accessor.h:15