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;
362 [[nodiscard]]
inline Lock* lock()
const;
391#elif defined(__APPLE__) || defined(__linux__) || defined(__EMSCRIPTEN__)
393 pthread_mutexattr_t mutexAttribute;
394 pthread_mutexattr_init(&mutexAttribute);
395 pthread_mutexattr_settype(&mutexAttribute, PTHREAD_MUTEX_RECURSIVE);
396 pthread_mutex_init(&
mutex_, &mutexAttribute);
400 pthread_mutexattr_t mutexAttribute = PTHREAD_MUTEX_RECURSIVE;
401 pthread_mutex_init(&
mutex_, &mutexAttribute);
417 pthread_mutex_destroy(&
mutex_);
431 pthread_mutex_lock(&
mutex_);
446 pthread_mutex_unlock(&
mutex_);
464 if (pthread_mutex_trylock(&
mutex_) == 0)
466 pthread_mutex_unlock(&
mutex_);
486template <
bool tActive>
493template <
bool tActive>
500template <
bool tActive>
509 ocean_assert(
lock_ !=
nullptr);
515 if (
lock_ !=
nullptr)
528 ocean_assert(!
isReleased() &&
"This TemporaryScopedLock object has been released before");
530 if (
lock_ !=
nullptr)
539 ocean_assert(
isReleased() &&
"This TemporaryScopedLock object must be released before");
541 if (
lock_ ==
nullptr)
550 return lock_ ==
nullptr;
556 if (
lock_ !=
nullptr)
574 if (
lock_ !=
nullptr)
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:423
~Lock()
Destructs a lock object.
Definition Lock.h:407
bool isLocked()
Returns whether this critical section is locked by another thread.
Definition Lock.h:452
Lock & operator=(const Lock &lock)=delete
Disabled assign operator.
void unlock()
Unlocks the critical section.
Definition Lock.h:437
Lock(const Lock &lock)=delete
Disabled copy constructor.
Lock()
Creates a new lock object.
Definition Lock.h:385
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:337
~OptionalScopedLock()
Destructs an optional scoped lock and unlocks the internal lock object if defined.
Definition Lock.h:572
Lock * lock() const
Returns the lock object which (if existing) is locked during the existence of this scoped lock object...
Definition Lock.h:580
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:382
OptionalScopedLock(Lock *lock)
Creates a new optional scoped lock object by a given lock object.
Definition Lock.h:553
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:481
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:475
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:487
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:501
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:537
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:521
void release()
Explicitly releases the lock before the scoped lock object is released.
Definition Lock.h:526
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:548
~TemporaryScopedLock()
Destructs a scoped lock and unlocks the internal lock object (if still locked).
Definition Lock.h:513
The namespace covering the entire Ocean framework.
Definition Accessor.h:15