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