8 #ifndef META_OCEAN_BASE_LOCK_H
9 #define META_OCEAN_BASE_LOCK_H
30 class OCEAN_BASE_EXPORT
Lock
58 inline bool isLocked();
97 template <
bool tActive>
176 template <
bool tActive>
235 OCEAN_SUPPRESS_UNUSED_WARNING(lock);
244 OCEAN_SUPPRESS_UNUSED_WARNING(lock);
277 [[nodiscard]]
inline Lock* lock()
const;
290 inline void relock(
Lock& lock);
296 inline bool isReleased()
const;
350 [[nodiscard]]
inline Lock* lock()
const;
375 #if defined(_WINDOWS)
379 #elif defined(__APPLE__) || defined(__linux__) || defined(__EMSCRIPTEN__)
381 pthread_mutexattr_t mutexAttribute;
382 pthread_mutexattr_init(&mutexAttribute);
383 pthread_mutexattr_settype(&mutexAttribute, PTHREAD_MUTEX_RECURSIVE);
384 pthread_mutex_init(&
mutex_, &mutexAttribute);
388 pthread_mutexattr_t mutexAttribute = PTHREAD_MUTEX_RECURSIVE;
389 pthread_mutex_init(&
mutex_, &mutexAttribute);
398 #if defined(_WINDOWS)
405 pthread_mutex_destroy(&
mutex_);
413 #if defined(_WINDOWS)
419 pthread_mutex_lock(&
mutex_);
428 #if defined(_WINDOWS)
434 pthread_mutex_unlock(&
mutex_);
442 #if defined(_WINDOWS)
452 if (pthread_mutex_trylock(&
mutex_) == 0)
454 pthread_mutex_unlock(&
mutex_);
474 template <
bool tActive>
481 template <
bool tActive>
488 template <
bool tActive>
497 ocean_assert(
lock_ !=
nullptr);
503 if (
lock_ !=
nullptr)
516 ocean_assert(!
isReleased() &&
"This TemporaryScopedLock object has been released before");
518 if (
lock_ !=
nullptr)
527 ocean_assert(
isReleased() &&
"This TemporaryScopedLock object must be released before");
529 if (
lock_ ==
nullptr)
538 return lock_ ==
nullptr;
544 if (
lock_ !=
nullptr)
562 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:411
~Lock()
Destructs a lock object.
Definition: Lock.h:395
bool isLocked()
Returns whether this critical section is locked by another thread.
Definition: Lock.h:440
Lock & operator=(const Lock &lock)=delete
Disabled assign operator.
void unlock()
Unlocks the critical section.
Definition: Lock.h:425
Lock(const Lock &lock)=delete
Disabled copy constructor.
Lock()
Creates a new lock object.
Definition: Lock.h:373
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:325
~OptionalScopedLock()
Destructs an optional scoped lock and unlocks the internal lock object if defined.
Definition: Lock.h:560
OptionalScopedLock & operator=(const OptionalScopedLock &object)=delete
Disabled accessible assigns operator.
Lock * lock() const
Returns the lock object which (if existing) is locked during the existence of this scoped lock object...
Definition: Lock.h:568
Lock * lock_
Lock object which is locked during the existence of this scoped lock object.
Definition: Lock.h:370
OptionalScopedLock(Lock *lock)
Creates a new optional scoped lock object by a given lock object.
Definition: Lock.h:541
OptionalScopedLock(const OptionalScopedLock &object)=delete
Disabled accessible copy operator.
This class implements a scoped lock object for recursive lock objects.
Definition: Lock.h:135
~ScopedLock()
Destructs a scoped lock and unlocks the internal lock object.
Definition: Lock.h:469
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:463
Lock & lock_
Lock object which is locked during the existence of this scoped lock object.
Definition: Lock.h:167
Specialization of the TemplatedLock class.
Definition: Lock.h:110
TemplatedLock(const TemplatedLock &lock)=delete
Disabled copy constructor.
TemplatedLock & operator=(const TemplatedLock &lock)=delete
Disabled assign operator.
This class implements a template-based recursive lock object.
Definition: Lock.h:99
TemplatedScopedLock(Lock &lock)
Creates a new scoped lock object by a given lock object.
Definition: Lock.h:233
TemplatedScopedLock(TemplatedLock< false > &lock)
Creates a new scoped lock object by a given lock object.
Definition: Lock.h:242
This class implements a recursive scoped lock object that is activated by a boolean template paramete...
Definition: Lock.h:178
TemplatedScopedLock(Lock &lock)
Creates a new scoped lock object by a given lock object.
Definition: Lock.h:475
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:489
Lock & lock_
Lock object which is locked during the existence of this scoped lock object.
Definition: Lock.h:216
This class implements a recursive scoped lock object allowing to release the lock before the scoped o...
Definition: Lock.h:254
void relock(Lock &lock)
Re-locks this scoped lock with a given lock.
Definition: Lock.h:525
TemporaryScopedLock & operator=(const TemporaryScopedLock &object)=delete
Disabled accessible assigns operator.
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:509
void release()
Explicitly releases the lock before the scoped lock object is released.
Definition: Lock.h:514
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:316
bool isReleased() const
Returns whether this scoped lock is released already.
Definition: Lock.h:536
~TemporaryScopedLock()
Destructs a scoped lock and unlocks the internal lock object (if still locked).
Definition: Lock.h:501
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15