#pragma once ///@file #include #include #include #include #include #include "error.hh" namespace nix { /** * This template class ensures synchronized access to a value of type * T. It is used as follows: * * struct Data { int x; ... }; * * Sync data; * * { * auto data_(data.lock()); * data_->x = 123; * } * * Here, "data" is automatically unlocked when "data_" goes out of * scope. */ template class SyncBase { private: M mutex; T data; public: SyncBase() { } SyncBase(const T & data) : data(data) { } SyncBase(T && data) noexcept : data(std::move(data)) { } template class Lock { protected: SyncBase * s; L lk; friend SyncBase; Lock(SyncBase * s) : s(s), lk(s->mutex) { } public: Lock(Lock && l) : s(l.s) { unreachable(); } Lock(const Lock & l) = delete; ~Lock() { } void wait(std::condition_variable & cv) { assert(s); cv.wait(lk); } template std::cv_status wait_for(std::condition_variable & cv, const std::chrono::duration & duration) { assert(s); return cv.wait_for(lk, duration); } template bool wait_for(std::condition_variable & cv, const std::chrono::duration & duration, Predicate pred) { assert(s); return cv.wait_for(lk, duration, pred); } template std::cv_status wait_until(std::condition_variable & cv, const std::chrono::time_point & duration) { assert(s); return cv.wait_until(lk, duration); } }; struct WriteLock : Lock { T * operator -> () { return &WriteLock::s->data; } T & operator * () { return WriteLock::s->data; } }; /** * Acquire write (exclusive) access to the inner value. */ WriteLock lock() { return WriteLock(this); } struct ReadLock : Lock { const T * operator -> () { return &ReadLock::s->data; } const T & operator * () { return ReadLock::s->data; } }; /** * Acquire read access to the inner value. When using * `std::shared_mutex`, this will use a shared lock. */ ReadLock readLock() const { return ReadLock(const_cast(this)); } }; template using Sync = SyncBase, std::unique_lock>; template using SharedSync = SyncBase, std::shared_lock>; }