aboutsummaryrefslogtreecommitdiff
path: root/zencore/include
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2022-12-01 21:55:17 +0100
committerGitHub <[email protected]>2022-12-01 12:55:17 -0800
commit7d448505cf8a63e9e3f4ed6d606693daa1cf584b (patch)
treee3d108e835b6cd5b8d814d0e1f077c813acf3bae /zencore/include
parent0.1.9 (diff)
downloadzen-7d448505cf8a63e9e3f4ed6d606693daa1cf584b.tar.xz
zen-7d448505cf8a63e9e3f4ed6d606693daa1cf584b.zip
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
Diffstat (limited to 'zencore/include')
-rw-r--r--zencore/include/zencore/thread.h35
1 files changed, 33 insertions, 2 deletions
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 <shared_mutex>
-
+#include <atomic>
#include <filesystem>
+#include <shared_mutex>
#include <string_view>
#include <vector>
@@ -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