aboutsummaryrefslogtreecommitdiff
path: root/zenstore
diff options
context:
space:
mode:
Diffstat (limited to 'zenstore')
-rw-r--r--zenstore/gc.cpp28
-rw-r--r--zenstore/include/zenstore/gc.h22
2 files changed, 48 insertions, 2 deletions
diff --git a/zenstore/gc.cpp b/zenstore/gc.cpp
index 278f09b0b..52bb33955 100644
--- a/zenstore/gc.cpp
+++ b/zenstore/gc.cpp
@@ -2,9 +2,11 @@
#include <zenstore/gc.h>
+#include <zencore/logging.h>
+#include <zencore/string.h>
+#include <zencore/timer.h>
#include <zenstore/CAS.h>
#include <zenstore/cidstore.h>
-#include <zencore/logging.h>
namespace zen {
@@ -202,4 +204,28 @@ CasGc::OnDroppedCidReferences(std::span<IoHash> Hashes)
ZEN_UNUSED(Hashes);
}
+bool
+Gc::Trigger()
+{
+ uint32_t Expected = uint32_t(GcStatus::kIdle);
+ if (!m_Status.compare_exchange_strong(Expected, static_cast<uint32_t>(GcStatus::kRunning)))
+ {
+ return false;
+ }
+
+ ZEN_ASSERT(GcStatus::kRunning == Status());
+
+ m_GcThread = std::jthread([this]() {
+ Stopwatch Timer;
+ ZEN_INFO("garbage collection STARTING");
+
+ m_CasGc.CollectGarbage();
+ m_Status = static_cast<uint32_t>(GcStatus::kIdle);
+
+ ZEN_INFO("garbage collection DONE after {}", NiceTimeSpanMs(Timer.GetElapsedTimeMs()));
+ });
+
+ return true;
+}
+
} // namespace zen
diff --git a/zenstore/include/zenstore/gc.h b/zenstore/include/zenstore/gc.h
index 560642803..8efe933a0 100644
--- a/zenstore/include/zenstore/gc.h
+++ b/zenstore/include/zenstore/gc.h
@@ -5,8 +5,9 @@
#include <zencore/iohash.h>
#include <zencore/thread.h>
-#include <span>
+#include <atomic>
#include <functional>
+#include <span>
#define ZEN_USE_REF_TRACKING 0 // This is not currently functional
@@ -107,4 +108,23 @@ private:
CidStore* m_CidStore;
};
+enum class GcStatus : uint32_t
+{
+ kIdle,
+ kRunning
+};
+
+class Gc
+{
+public:
+ bool Trigger();
+ CasGc& Cas() { return m_CasGc; }
+ GcStatus Status() const { return static_cast<GcStatus>(m_Status.load()); }
+
+private:
+ CasGc m_CasGc;
+ std::jthread m_GcThread;
+ std::atomic_uint32_t m_Status;
+};
+
} // namespace zen