class ReadLock
{
public:
- ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
+ ReadLock(ReadWriteLock& lock) :
+ ReadLock(lock.getLock())
{
}
- ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
+ ReadLock(ReadWriteLock* lock) :
+ ReadLock(lock->getLock())
{
}
}
private:
- ReadLock(std::shared_mutex& lock) : d_lock(lock)
+ ReadLock(std::shared_mutex& lock) :
+ d_lock(lock)
{
}
class WriteLock
{
public:
- WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
+ WriteLock(ReadWriteLock& lock) :
+ WriteLock(lock.getLock())
{
}
- WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
+ WriteLock(ReadWriteLock* lock) :
+ WriteLock(lock->getLock())
{
}
}
private:
- WriteLock(std::shared_mutex& lock) : d_lock(lock)
+ WriteLock(std::shared_mutex& lock) :
+ d_lock(lock)
{
}
class TryReadLock
{
public:
- TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
+ TryReadLock(ReadWriteLock& lock) :
+ TryReadLock(lock.getLock())
{
}
- TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
+ TryReadLock(ReadWriteLock* lock) :
+ TryReadLock(lock->getLock())
{
}
}
private:
- TryReadLock(std::shared_mutex& lock) : d_lock(lock, std::try_to_lock)
+ TryReadLock(std::shared_mutex& lock) :
+ d_lock(lock, std::try_to_lock)
{
}
class TryWriteLock
{
public:
- TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
+ TryWriteLock(ReadWriteLock& lock) :
+ TryWriteLock(lock.getLock())
{
}
- TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
+ TryWriteLock(ReadWriteLock* lock) :
+ TryWriteLock(lock->getLock())
{
}
}
private:
- TryWriteLock(std::shared_mutex& lock) : d_lock(lock, std::try_to_lock)
+ TryWriteLock(std::shared_mutex& lock) :
+ d_lock(lock, std::try_to_lock)
{
}
class LockGuardedHolder
{
public:
- explicit LockGuardedHolder(T& value, std::mutex& mutex): d_lock(mutex), d_value(value)
+ explicit LockGuardedHolder(T& value, std::mutex& mutex) :
+ d_lock(mutex), d_value(value)
{
}
- T& operator*() const noexcept {
+ T& operator*() const noexcept
+ {
return d_value;
}
- T* operator->() const noexcept {
+ T* operator->() const noexcept
+ {
return &d_value;
}
class LockGuardedTryHolder
{
public:
- explicit LockGuardedTryHolder(T& value, std::mutex& mutex): d_lock(mutex, std::try_to_lock), d_value(value)
+ explicit LockGuardedTryHolder(T& value, std::mutex& mutex) :
+ d_lock(mutex, std::try_to_lock), d_value(value)
{
}
- T& operator*() const {
+ T& operator*() const
+ {
if (!owns_lock()) {
throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired");
}
return d_value;
}
- T* operator->() const {
+ T* operator->() const
+ {
if (!owns_lock()) {
throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired");
}
return &d_value;
}
- operator bool() const noexcept {
+ operator bool() const noexcept
+ {
return d_lock.owns_lock();
}
- bool owns_lock() const noexcept {
+ bool owns_lock() const noexcept
+ {
return d_lock.owns_lock();
}
class LockGuarded
{
public:
- explicit LockGuarded(const T& value): d_value(value)
+ explicit LockGuarded(const T& value) :
+ d_value(value)
{
}
- explicit LockGuarded(T&& value): d_value(std::move(value))
+ explicit LockGuarded(T&& value) :
+ d_value(std::move(value))
{
}
class SharedLockGuardedHolder
{
public:
- explicit SharedLockGuardedHolder(T& value, std::shared_mutex& mutex): d_lock(mutex), d_value(value)
+ explicit SharedLockGuardedHolder(T& value, std::shared_mutex& mutex) :
+ d_lock(mutex), d_value(value)
{
}
- T& operator*() const noexcept {
+ T& operator*() const noexcept
+ {
return d_value;
}
- T* operator->() const noexcept {
+ T* operator->() const noexcept
+ {
return &d_value;
}
class SharedLockGuardedTryHolder
{
public:
- explicit SharedLockGuardedTryHolder(T& value, std::shared_mutex& mutex): d_lock(mutex, std::try_to_lock), d_value(value)
+ explicit SharedLockGuardedTryHolder(T& value, std::shared_mutex& mutex) :
+ d_lock(mutex, std::try_to_lock), d_value(value)
{
}
- T& operator*() const {
+ T& operator*() const
+ {
if (!owns_lock()) {
throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired");
}
return d_value;
}
- T* operator->() const {
+ T* operator->() const
+ {
if (!owns_lock()) {
throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired");
}
return &d_value;
}
- operator bool() const noexcept {
+ operator bool() const noexcept
+ {
return d_lock.owns_lock();
}
- bool owns_lock() const noexcept {
+ bool owns_lock() const noexcept
+ {
return d_lock.owns_lock();
}
class SharedLockGuardedNonExclusiveHolder
{
public:
- explicit SharedLockGuardedNonExclusiveHolder(const T& value, std::shared_mutex& mutex): d_lock(mutex), d_value(value)
+ explicit SharedLockGuardedNonExclusiveHolder(const T& value, std::shared_mutex& mutex) :
+ d_lock(mutex), d_value(value)
{
}
- const T& operator*() const noexcept {
+ const T& operator*() const noexcept
+ {
return d_value;
}
- const T* operator->() const noexcept {
+ const T* operator->() const noexcept
+ {
return &d_value;
}
class SharedLockGuardedNonExclusiveTryHolder
{
public:
- explicit SharedLockGuardedNonExclusiveTryHolder(const T& value, std::shared_mutex& mutex): d_lock(mutex, std::try_to_lock), d_value(value)
+ explicit SharedLockGuardedNonExclusiveTryHolder(const T& value, std::shared_mutex& mutex) :
+ d_lock(mutex, std::try_to_lock), d_value(value)
{
}
- const T& operator*() const {
+ const T& operator*() const
+ {
if (!owns_lock()) {
throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired");
}
return d_value;
}
- const T* operator->() const {
+ const T* operator->() const
+ {
if (!owns_lock()) {
throw std::runtime_error("Trying to access data protected by a mutex while the lock has not been acquired");
}
return &d_value;
}
- operator bool() const noexcept {
+ operator bool() const noexcept
+ {
return d_lock.owns_lock();
}
- bool owns_lock() const noexcept {
+ bool owns_lock() const noexcept
+ {
return d_lock.owns_lock();
}
class SharedLockGuarded
{
public:
- explicit SharedLockGuarded(const T& value): d_value(value)
+ explicit SharedLockGuarded(const T& value) :
+ d_value(value)
{
}
- explicit SharedLockGuarded(T&& value): d_value(std::move(value))
+ explicit SharedLockGuarded(T&& value) :
+ d_value(std::move(value))
{
}
#include "lock.hh"
-/** This is sort of a light-weight RCU idea.
+/** This is sort of a light-weight RCU idea.
Suitable for when you frequently consult some "readonly" state, which infrequently
- gets changed. One way of dealing with this is fully locking access to the state, but
+ gets changed. One way of dealing with this is fully locking access to the state, but
this is rather wasteful.
- Instead, in the code below, the frequent users of the state get a "readonly" copy of it,
- which they can consult. On access, we atomically compare if the local copy is still current
+ Instead, in the code below, the frequent users of the state get a "readonly" copy of it,
+ which they can consult. On access, we atomically compare if the local copy is still current
with the global one. If it isn't we do the lock thing, and create a new local copy.
- Meanwhile, to upgrade the global state, methods are offered that do appropriate locking
+ Meanwhile, to upgrade the global state, methods are offered that do appropriate locking
and upgrade the 'generation' counter, signaling to the local copies that they need to be
refreshed on the next access.
Two ways to change the global copy are available:
getCopy(), which delivers a deep copy of the current state, followed by setState()
- modify(), which accepts a (lambda)function that modifies the state
+ modify(), which accepts a (lambda)function that modifies the state
- NOTE: The actual destruction of the 'old' state happens when the last local state
+ NOTE: The actual destruction of the 'old' state happens when the last local state
relinquishes its access to the state.
"read-only"
- Sometimes, a 'state' can contain parts that can safely be modified by multiple users, for
+ Sometimes, a 'state' can contain parts that can safely be modified by multiple users, for
example, atomic counters. In such cases, it may be useful to explicitly declare such counters
as mutable. */
-template<typename T> class GlobalStateHolder;
+template <typename T>
+class GlobalStateHolder;
-template<typename T>
+template <typename T>
class LocalStateHolder
{
public:
- explicit LocalStateHolder(GlobalStateHolder<T>* source) : d_source(source)
+ explicit LocalStateHolder(GlobalStateHolder<T>* source) :
+ d_source(source)
{}
- const T* operator->() // fast const-only access, but see "read-only" above
+ const T* operator->() // fast const-only access, but see "read-only" above
{
- if(d_source->getGeneration() != d_generation) {
- d_source->getState(&d_state, & d_generation);
+ if (d_source->getGeneration() != d_generation) {
+ d_source->getState(&d_state, &d_generation);
}
return d_state.get();
}
- const T& operator*() // fast const-only access, but see "read-only" above
+ const T& operator*() // fast const-only access, but see "read-only" above
{
return *operator->();
}
void reset()
{
- d_generation=0;
+ d_generation = 0;
d_state.reset();
}
+
private:
std::shared_ptr<T> d_state;
unsigned int d_generation{0};
const GlobalStateHolder<T>* d_source;
};
-template<typename T>
+template <typename T>
class GlobalStateHolder
{
public:
- GlobalStateHolder() : d_state(std::make_shared<T>())
+ GlobalStateHolder() :
+ d_state(std::make_shared<T>())
{}
LocalStateHolder<T> getLocal()
{
}
}
- T getCopy() const //!< Safely & slowly get a copy of the global state
+ T getCopy() const //!< Safely & slowly get a copy of the global state
{
return *(*(d_state.lock()));
}
-
+
//! Safely & slowly modify the global state
- template<typename F>
- void modify(F act) {
+ template <typename F>
+ void modify(F act)
+ {
auto state = d_state.lock();
auto newState = *(*state); // and yes, these three steps are necessary, can't ever modify state in place, even when locked!
act(newState);
}
typedef T value_type;
+
private:
unsigned int getGeneration() const
{