]> git.hungrycats.org Git - bees/commitdiff
multilock: allow locking to be switched off at runtime
authorZygo Blaxell <bees@furryterror.org>
Thu, 24 Sep 2026 02:10:47 +0000 (22:10 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
enable_locking() was only called once, before any worker thread started,
so a plain bool was enough.  Deciding at runtime whether the
clone/LOGICAL_INO serialization is still needed means flipping it while
other threads are in get_lock().

Make the flag atomic and document what a change affects:  only later
get_lock() calls.  Held handles still release through put_lock(), and
threads already waiting are still granted as the holders drain.

Assisted-by: Claude-Code:claude-opus-5-5
include/crucible/multilock.h

index 1ea5659f58c2f212d1e2607ea67bd809c8c1a61e..98f2b79c0d8b78c397d49a4767285d81de3aa3be 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef CRUCIBLE_MULTILOCK_H
 #define CRUCIBLE_MULTILOCK_H
 
+#include <atomic>
 #include <condition_variable>
 #include <cstdint>
 #include <deque>
@@ -64,7 +65,8 @@ namespace crucible {
                /// Bumped each time a new turn begins; a waiter belongs to the current cohort iff its captured phase is older than this.
                uint64_t m_phase = 0;
                /// When false, get_lock() returns a null handle and no serialization is performed.
-               bool m_do_locking = true;
+               /// Atomic because it may be changed while other threads are taking locks.
+               atomic<bool> m_do_locking{true};
 
                /** \brief Movable-by-shared_ptr token representing one held lock of a given type.
 
@@ -114,7 +116,13 @@ namespace crucible {
                    shared_ptr and performs no serialization.
                */
                static shared_ptr<LockHandle> get_lock(const string &type);
-               /// \brief Globally enable or disable locking on the process-wide instance; when disabled, get_lock() is a no-op returning null.
+               /** \brief Globally enable or disable locking on the process-wide instance; when disabled, get_lock() is a no-op returning null.
+
+                   May be called at any time.  Disabling does not revoke locks
+                   already held or wake threads already waiting:  held handles
+                   still release normally, and waiters are granted as the
+                   current holders drain.  Only later get_lock() calls change.
+               */
                static void enable_locking(bool enabled);
                /** \brief Number of threads currently blocked waiting for a turn of \p type.