mirror of
https://github.com/lubosz/overte.git
synced 2025-04-16 15:30:11 +02:00
commit
789290f540
3 changed files with 285 additions and 37 deletions
92
libraries/shared/src/shared/QTryReadLocker.h
Normal file
92
libraries/shared/src/shared/QTryReadLocker.h
Normal file
|
@ -0,0 +1,92 @@
|
|||
//
|
||||
// QTryReadLocker.h
|
||||
// shared/src/shared/QTryReadLocker.h
|
||||
//
|
||||
// Created by Clément Brisset on 10/29/15.
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_QTryReadLocker_h
|
||||
#define hifi_QTryReadLocker_h
|
||||
|
||||
#include <QtCore/QReadWriteLock>
|
||||
|
||||
class QTryReadLocker {
|
||||
public:
|
||||
QTryReadLocker(QReadWriteLock* readWriteLock);
|
||||
QTryReadLocker(QReadWriteLock* readWriteLock, int timeout);
|
||||
~QTryReadLocker();
|
||||
|
||||
bool isLocked() const;
|
||||
|
||||
void unlock();
|
||||
bool tryRelock();
|
||||
bool tryRelock(int timeout);
|
||||
|
||||
QReadWriteLock* readWriteLock() const;
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QTryReadLocker)
|
||||
quintptr _val;
|
||||
};
|
||||
|
||||
// Implementation
|
||||
inline QTryReadLocker::QTryReadLocker(QReadWriteLock* areadWriteLock) :
|
||||
_val(reinterpret_cast<quintptr>(areadWriteLock))
|
||||
{
|
||||
Q_ASSERT_X((_val & quintptr(1u)) == quintptr(0),
|
||||
"QTryReadLocker", "QTryReadLocker pointer is misaligned");
|
||||
tryRelock();
|
||||
}
|
||||
|
||||
inline QTryReadLocker::QTryReadLocker(QReadWriteLock* areadWriteLock, int timeout) :
|
||||
_val(reinterpret_cast<quintptr>(areadWriteLock))
|
||||
{
|
||||
Q_ASSERT_X((_val & quintptr(1u)) == quintptr(0),
|
||||
"QTryReadLocker", "QTryReadLocker pointer is misaligned");
|
||||
tryRelock(timeout);
|
||||
}
|
||||
|
||||
inline QTryReadLocker::~QTryReadLocker() {
|
||||
unlock();
|
||||
}
|
||||
|
||||
inline bool QTryReadLocker::isLocked() const {
|
||||
return (_val & quintptr(1u)) == quintptr(1u);
|
||||
}
|
||||
|
||||
inline void QTryReadLocker::unlock() {
|
||||
if (_val && isLocked()) {
|
||||
_val &= ~quintptr(1u);
|
||||
readWriteLock()->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
inline bool QTryReadLocker::tryRelock() {
|
||||
if (_val && !isLocked()) {
|
||||
if (readWriteLock()->tryLockForRead()) {
|
||||
_val |= quintptr(1u);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool QTryReadLocker::tryRelock(int timeout) {
|
||||
if (_val && !isLocked()) {
|
||||
if (readWriteLock()->tryLockForRead(timeout)) {
|
||||
_val |= quintptr(1u);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline QReadWriteLock* QTryReadLocker::readWriteLock() const {
|
||||
return reinterpret_cast<QReadWriteLock*>(_val & ~quintptr(1u));
|
||||
}
|
||||
|
||||
#endif // hifi_QTryReadLocker_h
|
92
libraries/shared/src/shared/QTryWriteLocker.h
Normal file
92
libraries/shared/src/shared/QTryWriteLocker.h
Normal file
|
@ -0,0 +1,92 @@
|
|||
//
|
||||
// QTryWriteLocker.h
|
||||
// shared/src/shared/QTryWriteLocker.h
|
||||
//
|
||||
// Created by Clément Brisset on 10/29/15.
|
||||
// Copyright 2015 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_QTryWriteLocker_h
|
||||
#define hifi_QTryWriteLocker_h
|
||||
|
||||
#include <QtCore/QReadWriteLock>
|
||||
|
||||
class QTryWriteLocker {
|
||||
public:
|
||||
QTryWriteLocker(QReadWriteLock* readWriteLock);
|
||||
QTryWriteLocker(QReadWriteLock* readWriteLock, int timeout);
|
||||
~QTryWriteLocker();
|
||||
|
||||
bool isLocked() const;
|
||||
|
||||
void unlock();
|
||||
bool tryRelock();
|
||||
bool tryRelock(int timeout);
|
||||
|
||||
QReadWriteLock* readWriteLock() const;
|
||||
|
||||
private:
|
||||
Q_DISABLE_COPY(QTryWriteLocker)
|
||||
quintptr _val;
|
||||
};
|
||||
|
||||
// Implementation
|
||||
inline QTryWriteLocker::QTryWriteLocker(QReadWriteLock* readWriteLock) :
|
||||
_val(reinterpret_cast<quintptr>(readWriteLock))
|
||||
{
|
||||
Q_ASSERT_X((_val & quintptr(1u)) == quintptr(0),
|
||||
"QTryWriteLocker", "QTryWriteLocker pointer is misaligned");
|
||||
tryRelock();
|
||||
}
|
||||
|
||||
inline QTryWriteLocker::QTryWriteLocker(QReadWriteLock* readWriteLock, int timeout) :
|
||||
_val(reinterpret_cast<quintptr>(readWriteLock))
|
||||
{
|
||||
Q_ASSERT_X((_val & quintptr(1u)) == quintptr(0),
|
||||
"QTryWriteLocker", "QTryWriteLocker pointer is misaligned");
|
||||
tryRelock(timeout);
|
||||
}
|
||||
|
||||
inline QTryWriteLocker::~QTryWriteLocker() {
|
||||
unlock();
|
||||
}
|
||||
|
||||
inline bool QTryWriteLocker::isLocked() const {
|
||||
return (_val & quintptr(1u)) == quintptr(1u);
|
||||
}
|
||||
|
||||
inline void QTryWriteLocker::unlock() {
|
||||
if (_val && isLocked()) {
|
||||
_val &= ~quintptr(1u);
|
||||
readWriteLock()->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
inline bool QTryWriteLocker::tryRelock() {
|
||||
if (_val && !isLocked()) {
|
||||
if (readWriteLock()->tryLockForWrite()) {
|
||||
_val |= quintptr(1u);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool QTryWriteLocker::tryRelock(int timeout) {
|
||||
if (_val && !isLocked()) {
|
||||
if (readWriteLock()->tryLockForWrite(timeout)) {
|
||||
_val |= quintptr(1u);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline QReadWriteLock* QTryWriteLocker::readWriteLock() const {
|
||||
return reinterpret_cast<QReadWriteLock*>(_val & ~quintptr(1u));
|
||||
}
|
||||
|
||||
#endif // hifi_QTryWriteLocker_h
|
|
@ -9,54 +9,118 @@
|
|||
#pragma once
|
||||
#ifndef hifi_ReadWriteLockable_h
|
||||
#define hifi_ReadWriteLockable_h
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include <QtCore/QReadWriteLock>
|
||||
|
||||
#include "QTryReadLocker.h"
|
||||
#include "QTryWriteLocker.h"
|
||||
|
||||
class ReadWriteLockable {
|
||||
public:
|
||||
// Write locks
|
||||
template <typename F>
|
||||
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;
|
||||
}
|
||||
void withWriteLock(F&& f) const;
|
||||
|
||||
template <typename F>
|
||||
bool withWriteLock(F&& f, bool require) const;
|
||||
|
||||
template <typename F>
|
||||
bool withTryWriteLock(F f) const {
|
||||
return withWriteLock(f, false);
|
||||
}
|
||||
|
||||
bool withTryWriteLock(F&& f) const;
|
||||
|
||||
template <typename F>
|
||||
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 <typename F>
|
||||
bool withTryReadLock(F f) const {
|
||||
return withReadLock(f, false);
|
||||
}
|
||||
void withReadLock(F&& f) const;
|
||||
|
||||
template <typename F>
|
||||
bool withReadLock(F&& f, bool require) const;
|
||||
|
||||
template <typename F>
|
||||
bool withTryReadLock(F&& f) const;
|
||||
|
||||
template <typename F>
|
||||
bool withTryReadLock(F&& f, int timeout) const;
|
||||
|
||||
private:
|
||||
mutable QReadWriteLock _lock{ QReadWriteLock::Recursive };
|
||||
mutable QReadWriteLock _lock { QReadWriteLock::Recursive };
|
||||
};
|
||||
|
||||
// ReadWriteLockable
|
||||
template <typename F>
|
||||
inline void ReadWriteLockable::withWriteLock(F&& f) const {
|
||||
QWriteLocker locker(&_lock);
|
||||
f();
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline bool ReadWriteLockable::withWriteLock(F&& f, bool require) const {
|
||||
if (require) {
|
||||
withWriteLock(std::forward<F>(f));
|
||||
return true;
|
||||
} else {
|
||||
return withTryReadLock(std::forward<F>(f));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline bool ReadWriteLockable::withTryWriteLock(F&& f) const {
|
||||
QTryWriteLocker locker(&_lock);
|
||||
if (locker.isLocked()) {
|
||||
f();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline bool ReadWriteLockable::withTryWriteLock(F&& f, int timeout) const {
|
||||
QTryWriteLocker locker(&_lock, timeout);
|
||||
if (locker.isLocked()) {
|
||||
f();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline void ReadWriteLockable::withReadLock(F&& f) const {
|
||||
QReadLocker locker(&_lock);
|
||||
f();
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline bool ReadWriteLockable::withReadLock(F&& f, bool require) const {
|
||||
if (require) {
|
||||
withReadLock(std::forward<F>(f));
|
||||
return true;
|
||||
} else {
|
||||
return withTryReadLock(std::forward<F>(f));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline bool ReadWriteLockable::withTryReadLock(F&& f) const {
|
||||
QTryReadLocker locker(&_lock);
|
||||
if (locker.isLocked()) {
|
||||
f();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline bool ReadWriteLockable::withTryReadLock(F&& f, int timeout) const {
|
||||
QTryReadLocker locker(&_lock, timeout);
|
||||
if (locker.isLocked()) {
|
||||
f();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue