From 9ac0b00342c559cc4ad50061176ce2fd10482caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 8 Jan 2023 21:31:55 +0100 Subject: FRP: support recursive locking with BhvCurTime --- src/frp.cpp | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/frp.cpp b/src/frp.cpp index a16950c..1d377dd 100644 --- a/src/frp.cpp +++ b/src/frp.cpp @@ -2,6 +2,7 @@ #include #include +#include using namespace erebos; @@ -9,33 +10,50 @@ using std::condition_variable; using std::move; using std::mutex; using std::nullopt; +using std::thread; using std::unique_lock; +namespace { + mutex bhvTimeMutex; condition_variable bhvTimeCond; -bool bhvTimeRunning = false; +optional bhvTimeRunning = nullopt; +uint64_t bhvTimeCount = 0; uint64_t bhvTimeLast = 0; +} + BhvTime::BhvTime(const BhvCurTime & ct): BhvTime(ct.time()) {} BhvCurTime::BhvCurTime() { + auto tid = std::this_thread::get_id(); unique_lock lock(bhvTimeMutex); - bhvTimeCond.wait(lock, []{ return !bhvTimeRunning; }); + bhvTimeCond.wait(lock, [tid]{ + return !bhvTimeRunning || bhvTimeRunning == tid; + }); - bhvTimeRunning = true; - t = BhvTime(++bhvTimeLast); + if (bhvTimeRunning != tid) { + bhvTimeRunning = tid; + bhvTimeLast++; + } + t = BhvTime(bhvTimeLast); + bhvTimeCount++; } BhvCurTime::~BhvCurTime() { if (t) { unique_lock lock(bhvTimeMutex); - bhvTimeRunning = false; - lock.unlock(); - bhvTimeCond.notify_one(); + bhvTimeCount--; + + if (bhvTimeCount == 0) { + bhvTimeRunning.reset(); + lock.unlock(); + bhvTimeCond.notify_one(); + } } } -- cgit v1.2.3