From 7d448505cf8a63e9e3f4ed6d606693daa1cf584b Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Thu, 1 Dec 2022 21:55:17 +0100 Subject: Make sure we always store record/op before attachments (#195) * Make sure we always store record/op before attachments We don't want to store attachments first - a GC operation could then remove attachments if triggered before storing record/op * zen::Latch * Use latch to wait for attachments to be stored * use zen::latch when adding attachments from project oplog import * changelog --- zencore/include/zencore/thread.h | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'zencore/include') diff --git a/zencore/include/zencore/thread.h b/zencore/include/zencore/thread.h index 16d0e9dee..3c1821a62 100644 --- a/zencore/include/zencore/thread.h +++ b/zencore/include/zencore/thread.h @@ -4,9 +4,9 @@ #include "zencore.h" -#include - +#include #include +#include #include #include @@ -144,6 +144,37 @@ private: void* m_MutexHandle = nullptr; }; +/** + * Downward counter of type std::ptrdiff_t which can be used to synchronize threads + */ +class Latch +{ +public: + Latch(std::ptrdiff_t Count) : Counter(Count) {} + + void CountDown() + { + std::ptrdiff_t Old = Counter.fetch_sub(1); + if (Old == 1) + { + Complete.Set(); + } + } + + void Wait() + { + std::ptrdiff_t Old = Counter.load(); + if (Old != 0) + { + Complete.Wait(); + } + } + +private: + std::atomic_ptrdiff_t Counter; + Event Complete; +}; + /** Basic process abstraction */ class ProcessHandle -- cgit v1.2.3