From decc061939deee10eb6dcdbbe5813fb510868d1a Mon Sep 17 00:00:00 2001 From: Sergei Zimmerman Date: Sun, 1 Mar 2026 20:09:11 +0300 Subject: [PATCH] libutil: Template SyncBase over condition_variable implementation This will allow us to use boost::fibers with the SyncBase class. --- src/libutil/include/nix/util/sync.hh | 29 +++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/libutil/include/nix/util/sync.hh b/src/libutil/include/nix/util/sync.hh index ad640410a465..6f3298324ff8 100644 --- a/src/libutil/include/nix/util/sync.hh +++ b/src/libutil/include/nix/util/sync.hh @@ -27,7 +27,7 @@ namespace nix { * Here, "data" is automatically unlocked when "data_" goes out of * scope. */ -template +template class SyncBase { private: @@ -76,29 +76,35 @@ public: ~Lock() {} - void wait(std::condition_variable & cv) + void wait(CV & cv) { assert(s); cv.wait(lk); } + template + void wait(CV & cv, Predicate pred) + { + assert(s); + cv.wait(lk, std::move(pred)); + } + template - std::cv_status wait_for(std::condition_variable & cv, const std::chrono::duration & duration) + std::cv_status wait_for(CV & 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) + bool wait_for(CV & 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) + std::cv_status wait_until(CV & cv, const std::chrono::time_point & duration) { assert(s); return cv.wait_until(lk, duration); @@ -154,10 +160,15 @@ public: }; template -using Sync = SyncBase, std::unique_lock>; +using Sync = + SyncBase, std::unique_lock, std::condition_variable>; template -using SharedSync = - SyncBase, std::shared_lock>; +using SharedSync = SyncBase< + T, + std::shared_mutex, + std::unique_lock, + std::shared_lock, + std::condition_variable>; } // namespace nix