10 #ifndef hifi_ReadWriteLockable_h
11 #define hifi_ReadWriteLockable_h
15 #include <QtCore/QReadWriteLock>
17 #include "QTryReadLocker.h"
18 #include "QTryWriteLocker.h"
20 class ReadWriteLockable {
24 void withWriteLock(F&& f)
const;
26 template <
typename T,
typename F>
27 T resultWithWriteLock(F&& f)
const;
30 bool withWriteLock(F&& f,
bool require)
const;
33 bool withTryWriteLock(F&& f)
const;
36 bool withTryWriteLock(F&& f,
int timeout)
const;
40 void withReadLock(F&& f)
const;
42 template <
typename T,
typename F>
43 T resultWithReadLock(F&& f)
const;
46 bool withReadLock(F&& f,
bool require)
const;
49 bool withTryReadLock(F&& f)
const;
52 bool withTryReadLock(F&& f,
int timeout)
const;
54 QReadWriteLock& getLock()
const {
return _lock; }
57 mutable QReadWriteLock _lock { QReadWriteLock::Recursive };
62 inline void ReadWriteLockable::withWriteLock(F&& f)
const {
63 QWriteLocker locker(&_lock);
68 inline bool ReadWriteLockable::withWriteLock(F&& f,
bool require)
const {
70 withWriteLock(std::forward<F>(f));
73 return withTryReadLock(std::forward<F>(f));
77 template <
typename T,
typename F>
78 inline T ReadWriteLockable::resultWithWriteLock(F&& f)
const {
87 inline bool ReadWriteLockable::withTryWriteLock(F&& f)
const {
88 QTryWriteLocker locker(&_lock);
89 if (locker.isLocked()) {
97 inline bool ReadWriteLockable::withTryWriteLock(F&& f,
int timeout)
const {
98 QTryWriteLocker locker(&_lock, timeout);
99 if (locker.isLocked()) {
106 template <
typename F>
107 inline void ReadWriteLockable::withReadLock(F&& f)
const {
108 QReadLocker locker(&_lock);
112 template <
typename T,
typename F>
113 inline T ReadWriteLockable::resultWithReadLock(F&& f)
const {
121 template <
typename F>
122 inline bool ReadWriteLockable::withReadLock(F&& f,
bool require)
const {
124 withReadLock(std::forward<F>(f));
127 return withTryReadLock(std::forward<F>(f));
131 template <
typename F>
132 inline bool ReadWriteLockable::withTryReadLock(F&& f)
const {
133 QTryReadLocker locker(&_lock);
134 if (locker.isLocked()) {
141 template <
typename F>
142 inline bool ReadWriteLockable::withTryReadLock(F&& f,
int timeout)
const {
143 QTryReadLocker locker(&_lock, timeout);
144 if (locker.isLocked()) {