From 7cf2ad22155ddde62a16f660078b2f165ce59081 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 10 Nov 2015 18:19:12 -0800 Subject: [PATCH] Remove lockFor... and expand ReadWriteLockable --- .../shared/src/shared/ReadWriteLockable.h | 136 +++++++++++++----- 1 file changed, 100 insertions(+), 36 deletions(-) diff --git a/libraries/shared/src/shared/ReadWriteLockable.h b/libraries/shared/src/shared/ReadWriteLockable.h index 98dff4a841..04aa20a067 100644 --- a/libraries/shared/src/shared/ReadWriteLockable.h +++ b/libraries/shared/src/shared/ReadWriteLockable.h @@ -9,54 +9,118 @@ #pragma once #ifndef hifi_ReadWriteLockable_h #define hifi_ReadWriteLockable_h + +#include + #include +#include "QTryReadLocker.h" +#include "QTryWriteLocker.h" + class ReadWriteLockable { public: + // Write locks template - bool withWriteLock(F f, bool require = true) const { - if (!require) { - bool result = _lock.tryLockForWrite(); - if (result) { - f(); - _lock.unlock(); - } - return result; - } - - QWriteLocker locker(&_lock); - f(); - return true; - } + bool withWriteLock(F f) const; + + template + bool withWriteLock(F&& f, bool require) const; template - bool withTryWriteLock(F f) const { - return withWriteLock(f, false); - } - + bool withTryWriteLock(F f) const; + template - bool withReadLock(F f, bool require = true) const { - if (!require) { - bool result = _lock.tryLockForRead(); - if (result) { - f(); - _lock.unlock(); - } - return result; - } - - QReadLocker locker(&_lock); - f(); - return true; - } - + bool withTryWriteLock(F f, int timeout) const; + + // Read locks template - bool withTryReadLock(F f) const { - return withReadLock(f, false); - } + bool withReadLock(F f) const; + + template + bool withReadLock(F&& f, bool require) const; + + template + bool withTryReadLock(F f) const; + + template + bool withTryReadLock(F f, int timeout) const; private: mutable QReadWriteLock _lock{ QReadWriteLock::Recursive }; }; +// ReadWriteLockable +template +inline bool ReadWriteLockable::withWriteLock(F f) const { + QWriteLocker locker(&_lock); + f(); + return true; +} + +template +inline bool ReadWriteLockable::withWriteLock(F&& f, bool require) const { + if (require) { + return withWriteLock(std::forward(f)); + } else { + return withTryReadLock(std::forward(f)); + } +} + +template +inline bool ReadWriteLockable::withTryWriteLock(F f) const { + QTryWriteLocker locker(&_lock); + if (locker.isLocked()) { + f(); + return true; + } + return false; +} + +template +inline bool ReadWriteLockable::withTryWriteLock(F f, int timeout) const { + QTryWriteLocker locker(&_lock, timeout); + if (locker.isLocked()) { + f(); + return true; + } + return false; +} + +template +inline bool ReadWriteLockable::withReadLock(F f) const { + QReadLocker locker(&_lock); + f(); + return true; +} + +template +inline bool ReadWriteLockable::withReadLock(F&& f, bool require) const { + if (require) { + return withReadLock(std::forward(f)); + } else { + return withTryReadLock(std::forward(f)); + } +} + +template +inline bool ReadWriteLockable::withTryReadLock(F f) const { + QTryReadLocker locker(&_lock); + if (locker.isLocked()) { + f(); + return true; + } + return false; +} + +template +inline bool ReadWriteLockable::withTryReadLock(F f, int timeout) const { + QTryReadLocker locker(&_lock, timeout); + if (locker.isLocked()) { + f(); + return true; + } + return false; +} + + #endif