2016-02-23 15:00:59 +01:00
|
|
|
#pragma once
|
|
|
|
|
2017-08-02 23:03:22 +02:00
|
|
|
#include <cstdlib>
|
2016-02-23 15:00:59 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
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> data;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto data_(data.lock());
|
|
|
|
data_->x = 123;
|
|
|
|
}
|
|
|
|
|
|
|
|
Here, "data" is automatically unlocked when "data_" goes out of
|
|
|
|
scope.
|
|
|
|
*/
|
|
|
|
|
2016-04-08 18:07:13 +02:00
|
|
|
template<class T, class M = std::mutex>
|
2016-02-23 15:00:59 +01:00
|
|
|
class Sync
|
|
|
|
{
|
|
|
|
private:
|
2016-04-08 18:07:13 +02:00
|
|
|
M mutex;
|
2016-02-23 15:00:59 +01:00
|
|
|
T data;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Sync() { }
|
|
|
|
Sync(const T & data) : data(data) { }
|
2017-04-06 14:30:31 +02:00
|
|
|
Sync(T && data) noexcept : data(std::move(data)) { }
|
2016-02-23 15:00:59 +01:00
|
|
|
|
|
|
|
class Lock
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Sync * s;
|
2016-04-08 18:07:13 +02:00
|
|
|
std::unique_lock<M> lk;
|
2016-02-23 15:00:59 +01:00
|
|
|
friend Sync;
|
2016-02-24 13:31:46 +01:00
|
|
|
Lock(Sync * s) : s(s), lk(s->mutex) { }
|
2016-02-23 15:00:59 +01:00
|
|
|
public:
|
2016-02-24 13:31:46 +01:00
|
|
|
Lock(Lock && l) : s(l.s) { abort(); }
|
2016-02-23 15:00:59 +01:00
|
|
|
Lock(const Lock & l) = delete;
|
2016-02-24 13:31:46 +01:00
|
|
|
~Lock() { }
|
2016-02-23 15:00:59 +01:00
|
|
|
T * operator -> () { return &s->data; }
|
|
|
|
T & operator * () { return s->data; }
|
|
|
|
|
2016-02-24 13:31:46 +01:00
|
|
|
void wait(std::condition_variable & cv)
|
2016-02-23 15:00:59 +01:00
|
|
|
{
|
|
|
|
assert(s);
|
2016-02-24 13:31:46 +01:00
|
|
|
cv.wait(lk);
|
2016-02-23 15:00:59 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 15:49:37 +02:00
|
|
|
template<class Rep, class Period>
|
|
|
|
void wait_for(std::condition_variable & cv,
|
|
|
|
const std::chrono::duration<Rep, Period> & duration)
|
|
|
|
{
|
|
|
|
assert(s);
|
|
|
|
cv.wait_for(lk, duration);
|
|
|
|
}
|
|
|
|
|
2016-02-23 15:00:59 +01:00
|
|
|
template<class Rep, class Period, class Predicate>
|
2016-02-24 13:31:46 +01:00
|
|
|
bool wait_for(std::condition_variable & cv,
|
2016-02-23 15:00:59 +01:00
|
|
|
const std::chrono::duration<Rep, Period> & duration,
|
|
|
|
Predicate pred)
|
|
|
|
{
|
|
|
|
assert(s);
|
2016-02-24 13:31:46 +01:00
|
|
|
return cv.wait_for(lk, duration, pred);
|
2016-02-23 15:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Clock, class Duration>
|
2016-02-24 13:31:46 +01:00
|
|
|
std::cv_status wait_until(std::condition_variable & cv,
|
2016-02-23 15:00:59 +01:00
|
|
|
const std::chrono::time_point<Clock, Duration> & duration)
|
|
|
|
{
|
|
|
|
assert(s);
|
2016-02-24 13:31:46 +01:00
|
|
|
return cv.wait_until(lk, duration);
|
2016-02-23 15:00:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Lock lock() { return Lock(this); }
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|