summaryrefslogtreecommitdiffstats
path: root/src/display/initlock.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:50:49 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 11:50:49 +0000
commitc853ffb5b2f75f5a889ed2e3ef89b818a736e87a (patch)
tree7d13a0883bb7936b84d6ecdd7bc332b41ed04bee /src/display/initlock.h
parentInitial commit. (diff)
downloadinkscape-c853ffb5b2f75f5a889ed2e3ef89b818a736e87a.tar.xz
inkscape-c853ffb5b2f75f5a889ed2e3ef89b818a736e87a.zip
Adding upstream version 1.3+ds.upstream/1.3+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/display/initlock.h')
-rw-r--r--src/display/initlock.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/display/initlock.h b/src/display/initlock.h
new file mode 100644
index 0000000..1a94069
--- /dev/null
+++ b/src/display/initlock.h
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+#ifndef INITLOCK_H
+#define INITLOCK_H
+
+#include <atomic>
+#include <mutex>
+
+/**
+ * Almost entirely analogous to std::once_flag, but with the ability to be reset if the caller knows it is safe to do so.
+ */
+class InitLock
+{
+public:
+ template <typename F>
+ void init(F &&f) const
+ {
+ // Make sure the fast path is a single load-acquire - not guaranteed with std::once_flag.
+ [[likely]] if (inited.load(std::memory_order_acquire)) {
+ return;
+ }
+
+ std::call_once(once, [&] {
+ f();
+ inited.store(true, std::memory_order_release);
+ });
+ }
+
+ void reset()
+ {
+ inited.store(false, std::memory_order_relaxed);
+
+ // Abomination, but tenuously allowed by Standard.
+ // (The alternative, std::mutex, is too large - we want to create a lot of these objects.)
+ once.~once_flag();
+ new (&once) std::once_flag();
+ }
+
+private:
+ mutable std::once_flag once;
+ mutable std::atomic<bool> inited = false;
+};
+
+#endif // INITLOCK_H