diff --git a/CHANGELOG.md b/CHANGELOG.md index a6efc83..89d908f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ chronological order. - Native operations rejected during close no longer access failure counters in the state being destroyed; entered failures still contribute diagnostics. +- Native C and C++ handle entry no longer uses lock-backed atomic shared-pointer + snapshots. Concurrent close drains entered calls before handle destruction; + C ABI 2 and the mapped protocol remain unchanged. ## 3.0.0 - 2026-07-16 diff --git a/docs/lifecycle.md b/docs/lifecycle.md index 2d1b3de..6672de7 100644 --- a/docs/lifecycle.md +++ b/docs/lifecycle.md @@ -118,6 +118,12 @@ logical close. After every thread has stopped using the opaque pointer, wrappers perform this second, caller-synchronized step automatically after their local operation drain. +C++ store operations pin the local handle with lock-free integer atomics. +Concurrent `close()` calls reject new entries, logically close the native +store, and wait for existing pins before destroying the opaque handle. +Moving a store drains source pins before transferring the still-open handle. +Synchronize concurrent attempts to reopen or replace the same wrapper. + ## Generations and Incarnations Slots, leases, participants, directory operations, and their public tokens use diff --git a/src/cpp/include/shared_memory_store/detail/local_handle.hpp b/src/cpp/include/shared_memory_store/detail/local_handle.hpp new file mode 100644 index 0000000..2c6188f --- /dev/null +++ b/src/cpp/include/shared_memory_store/detail/local_handle.hpp @@ -0,0 +1,128 @@ +#pragma once + +#include +#include +#include +#include + +namespace shared_memory_store::detail { + +// Process-local ownership for a public wrapper. A snapshot enters with bounded +// lock-free atomics; only close/move wait. T::close() stops native operations +// before the last snapshot is drained and T can be destroyed. +template +class local_handle { + enum class state : std::uint32_t { closed, open, closing }; + static_assert(std::atomic::is_always_lock_free); + static_assert(std::atomic::is_always_lock_free); + +public: + class snapshot { + public: + snapshot() noexcept = default; + snapshot(const snapshot&) = delete; + snapshot& operator=(const snapshot&) = delete; + snapshot(snapshot&& other) noexcept + : owner_(std::exchange(other.owner_, nullptr)), value_(other.value_) {} + ~snapshot() { if (owner_) owner_->leave(); } + explicit operator bool() const noexcept { return owner_ != nullptr; } + T* operator->() const noexcept { return value_; } + private: + friend class local_handle; + snapshot(const local_handle* owner, T* value) noexcept + : owner_(owner), value_(value) {} + const local_handle* owner_{}; + T* value_{}; + }; + + local_handle() noexcept = default; + local_handle(const local_handle&) = delete; + local_handle& operator=(const local_handle&) = delete; + local_handle(local_handle&& other) noexcept { reset(other.take()); } + local_handle& operator=(local_handle&& other) noexcept { + if (this != &other) reset(other.take()); + return *this; + } + ~local_handle() { close(); } + + [[nodiscard]] snapshot pin() const noexcept { + if (state_.load(std::memory_order_acquire) != state::open) return {}; + // The sequentially consistent increment and recheck pair with close's + // state transition and drain. A late entrant either contributes to the + // drain or observes closing, so it cannot dereference a detached value. + active_.fetch_add(1, std::memory_order_seq_cst); + if (state_.load(std::memory_order_seq_cst) != state::open) { + leave(); + return {}; + } + return snapshot(this, value_.get()); + } + + [[nodiscard]] bool is_open() const noexcept { + return state_.load(std::memory_order_acquire) == state::open; + } + + void close() noexcept { + if (!begin_close()) return; + if (value_) value_->close(); + drain(); + value_.reset(); + finish_close(); + } + + // Replacement, like construction, requires the caller to synchronize + // concurrent replacements. Ordinary calls and close use pin()/close(). + void reset(std::unique_ptr value) noexcept { + close(); + value_ = std::move(value); + state_.store(value_ ? state::open : state::closed, std::memory_order_release); + } + +private: + bool begin_close() noexcept { + auto expected = state::open; + if (state_.compare_exchange_strong(expected, state::closing, + std::memory_order_seq_cst, std::memory_order_seq_cst)) return true; + while (expected == state::closing) { + state_.wait(expected, std::memory_order_acquire); + expected = state_.load(std::memory_order_acquire); + } + return false; + } + + void drain() const noexcept { + auto count = active_.load(std::memory_order_seq_cst); + while (count != 0) { + active_.wait(count, std::memory_order_acquire); + count = active_.load(std::memory_order_seq_cst); + } + } + + void finish_close() noexcept { + state_.store(state::closed, std::memory_order_release); + state_.notify_all(); + } + + void leave() const noexcept { + if (active_.fetch_sub(1, std::memory_order_seq_cst) == 1 && + state_.load(std::memory_order_seq_cst) != state::open) { + // With the sequentially consistent close/drain ordering, either + // close sees zero readers or this check sees closing and wakes it. + // Open-handle operations never enter the atomic-wait adapter. + active_.notify_all(); + } + } + + std::unique_ptr take() noexcept { + if (!begin_close()) return {}; + drain(); + auto value = std::move(value_); + finish_close(); + return value; + } + + std::unique_ptr value_; + std::atomic state_{state::closed}; + mutable std::atomic active_{}; +}; +} // namespace shared_memory_store::detail diff --git a/src/cpp/include/shared_memory_store/store.hpp b/src/cpp/include/shared_memory_store/store.hpp index a31ee56..6c28d29 100644 --- a/src/cpp/include/shared_memory_store/store.hpp +++ b/src/cpp/include/shared_memory_store/store.hpp @@ -1,6 +1,7 @@ #pragma once #include "c_api.h" +#include "detail/local_handle.hpp" #include #include @@ -492,17 +493,8 @@ class memory_store { ~memory_store() { close(); } memory_store(const memory_store&) = delete; memory_store& operator=(const memory_store&) = delete; - memory_store(memory_store&& other) noexcept - : control_(other.control_.exchange({}, std::memory_order_acq_rel)) {} - memory_store& operator=(memory_store&& other) noexcept { - if (this != &other) { - close(); - control_.store( - other.control_.exchange({}, std::memory_order_acq_rel), - std::memory_order_release); - } - return *this; - } + memory_store(memory_store&& other) noexcept = default; + memory_store& operator=(memory_store&& other) noexcept = default; static open_status try_create_or_open( const store_options& options, @@ -538,8 +530,8 @@ class memory_store { return open_status::mapping_failed; } try { - result.control_.store( - std::make_shared( + result.control_.reset( + std::make_unique( opened_handle, protocol_info{ identity.layout_major, @@ -547,8 +539,7 @@ class memory_store { identity.resource_protocol, identity.required_features, identity.optional_features, - }), - std::memory_order_release); + })); } catch (...) { sms_close_store(opened_handle); sms_destroy_store(opened_handle); @@ -557,31 +548,19 @@ class memory_store { return open_status::success; } - bool valid() const noexcept { - return control_.load(std::memory_order_acquire) != nullptr; - } + bool valid() const noexcept { return control_.is_open(); } protocol_info protocol() const noexcept { - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); return control ? control->protocol : protocol_info{}; } - void close() noexcept { - auto control = control_.load(std::memory_order_acquire); - if (!control) return; - control->close(); - std::shared_ptr expected = control; - (void)control_.compare_exchange_strong( - expected, - {}, - std::memory_order_acq_rel, - std::memory_order_acquire); - } + void close() noexcept { control_.close(); } status try_publish( std::span key, std::span value, std::span descriptor = {}, wait_options wait = wait_options::defaults()) noexcept { - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) return status::store_disposed; const auto native = detail::native_wait(wait); return static_cast(sms_publish( @@ -603,7 +582,7 @@ class memory_store { reinterpret_cast(value.data()), static_cast(value.size())}); } - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) { copied = 0; return status::store_disposed; @@ -624,7 +603,7 @@ class memory_store { value_lease& lease, wait_options wait = wait_options::defaults()) noexcept { lease.reset(); - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) return status::store_disposed; sms_lease* handle{}; const auto native = detail::native_wait(wait); @@ -637,7 +616,7 @@ class memory_store { status try_remove( std::span key, wait_options wait = wait_options::defaults()) noexcept { - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) return status::store_disposed; const auto native = detail::native_wait(wait); return static_cast(sms_remove( @@ -651,7 +630,7 @@ class memory_store { value_reservation& reservation, wait_options wait = wait_options::defaults()) noexcept { reservation.reset(); - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) return status::store_disposed; sms_reservation* handle{}; const auto native = detail::native_wait(wait); @@ -666,7 +645,7 @@ class memory_store { bool recover_current_process, recovery_report& report, wait_options wait = wait_options::defaults()) noexcept { - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) return status::store_disposed; sms_recovery_report native{}; native.struct_size = sizeof(native); @@ -684,7 +663,7 @@ class memory_store { bool recover_current_process, recovery_report& report, wait_options wait = wait_options::defaults()) noexcept { - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) return status::store_disposed; sms_recovery_report native{}; native.struct_size = sizeof(native); @@ -704,7 +683,7 @@ class memory_store { snapshot.value_ = {}; snapshot.value_.struct_size = sizeof(snapshot.value_); snapshot.value_.abi_version = SMS_C_ABI_VERSION; - const auto control = control_.load(std::memory_order_acquire); + const auto control = control_.pin(); if (!control) return status::store_disposed; const auto native = detail::native_wait(wait); return static_cast(sms_get_diagnostics( @@ -712,7 +691,7 @@ class memory_store { } private: - std::atomic> control_; + detail::local_handle control_; }; } // namespace shared_memory_store diff --git a/src/cpp/src/c_api.cpp b/src/cpp/src/c_api.cpp index f7bb865..12f273d 100644 --- a/src/cpp/src/c_api.cpp +++ b/src/cpp/src/c_api.cpp @@ -17,13 +17,14 @@ using sms::detail::Wait; struct sms_store { enum class close_state { open, closing, closed }; + static_assert(std::atomic::is_always_lock_free); sms_store(std::shared_ptr value, LayoutV2 layout) : implementation(std::move(value)), public_layout(layout) {} - // Hot calls take only an atomic shared snapshot. Close coordination never - // enters a publish/read/remove path. - std::atomic> implementation; + // The opaque allocation survives logical close. Keep its owner immutable; + // hot calls borrow it until caller-synchronized sms_destroy_store. + const std::shared_ptr implementation; const LayoutV2 public_layout; std::atomic state{close_state::open}; }; @@ -339,8 +340,7 @@ void SMS_CALL sms_close_store(sms_store* store) { } } - auto implementation = store->implementation.exchange( - {}, std::memory_order_acq_rel); + auto* implementation = store->implementation.get(); if (implementation) implementation->close(); store->state.store( sms_store::close_state::closed, std::memory_order_release); @@ -348,8 +348,7 @@ void SMS_CALL sms_close_store(sms_store* store) { } catch (...) { // No C++ exception may cross the C ABI. A synchronization-adapter // failure is not shared corruption and cannot justify termination. - auto implementation = store->implementation.exchange( - {}, std::memory_order_acq_rel); + auto* implementation = store->implementation.get(); if (implementation) implementation->close(); store->state.store( sms_store::close_state::closed, std::memory_order_release); @@ -368,9 +367,10 @@ void SMS_CALL sms_destroy_store(sms_store* store) { sms_status SMS_CALL sms_get_store_layout(sms_store* store, const sms_wait_options* wait_options, sms_store_layout* layout) { Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!layout || layout->struct_size < sizeof(*layout) || !abi_compatible(layout->abi_version) || !read_wait(wait_options, wait)) return SMS_STATUS_UNKNOWN_FAILURE; @@ -422,9 +422,10 @@ sms_status SMS_CALL sms_get_store_layout(sms_store* store, const sms_wait_option sms_status SMS_CALL sms_publish(sms_store* store, sms_bytes key, sms_bytes value, sms_bytes descriptor, const sms_wait_options* wait_options) { Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!read_wait(wait_options, wait) || !valid_bytes(key) || !valid_bytes(value) || !valid_bytes(descriptor)) return SMS_STATUS_UNKNOWN_FAILURE; @@ -439,9 +440,10 @@ sms_status SMS_CALL sms_publish_segments(sms_store* store, sms_bytes key, int64_t* copied_bytes) { if (copied_bytes) *copied_bytes = 0; Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!copied_bytes || !read_wait(wait_options, wait) || !valid_bytes(key) || !valid_bytes(descriptor) || segment_count > std::numeric_limits::max() || @@ -461,9 +463,10 @@ sms_status SMS_CALL sms_acquire(sms_store* store, sms_bytes key, if (!lease) return SMS_STATUS_INVALID_LEASE; *lease = nullptr; Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!read_wait(wait_options, wait) || !valid_bytes(key)) return SMS_STATUS_UNKNOWN_FAILURE; if (wait_canceled(wait_options)) return SMS_STATUS_OPERATION_CANCELED; @@ -472,7 +475,7 @@ sms_status SMS_CALL sms_acquire(sms_store* store, sms_bytes key, as_span(key), wait, slot, lifecycle, lease_id); if (status != SMS_STATUS_SUCCESS) return status; auto* handle = new (std::nothrow) sms_lease{ - implementation, slot, lifecycle, lease_id}; + store->implementation, slot, lifecycle, lease_id}; if (!handle) { implementation->release_lease( slot, lifecycle, lease_id, Wait{1000}); @@ -515,9 +518,10 @@ void SMS_CALL sms_destroy_lease(sms_lease* lease) { sms_status SMS_CALL sms_remove(sms_store* store, sms_bytes key, const sms_wait_options* wait_options) { Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!read_wait(wait_options, wait) || !valid_bytes(key)) return SMS_STATUS_UNKNOWN_FAILURE; if (wait_canceled(wait_options)) return SMS_STATUS_OPERATION_CANCELED; @@ -530,9 +534,10 @@ sms_status SMS_CALL sms_reserve(sms_store* store, sms_bytes key, int32_t payload if (!reservation) return SMS_STATUS_INVALID_RESERVATION; *reservation = nullptr; Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!read_wait(wait_options, wait) || !valid_bytes(key) || !valid_bytes(descriptor)) return SMS_STATUS_UNKNOWN_FAILURE; @@ -543,7 +548,7 @@ sms_status SMS_CALL sms_reserve(sms_store* store, sms_bytes key, int32_t payload wait, slot, lifecycle); if (status != SMS_STATUS_SUCCESS) return status; auto* handle = new (std::nothrow) sms_reservation{ - implementation, slot, lifecycle}; + store->implementation, slot, lifecycle}; if (!handle) { implementation->abort_reservation( slot, lifecycle, false, Wait{1000}); @@ -618,9 +623,10 @@ void SMS_CALL sms_destroy_reservation(sms_reservation* reservation) { sms_status SMS_CALL sms_recover_leases(sms_store* store, int32_t recover_current_process, const sms_wait_options* wait_options, sms_recovery_report* report) { Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!report || report->struct_size < sizeof(*report) || !abi_compatible(report->abi_version) || !read_wait(wait_options, wait)) return SMS_STATUS_UNKNOWN_FAILURE; @@ -636,9 +642,10 @@ sms_status SMS_CALL sms_recover_reservations(sms_store* store, int32_t recover_c const sms_wait_options* wait_options, sms_recovery_report* report) { Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!report || report->struct_size < sizeof(*report) || !abi_compatible(report->abi_version) || !read_wait(wait_options, wait)) return SMS_STATUS_UNKNOWN_FAILURE; @@ -653,9 +660,10 @@ sms_status SMS_CALL sms_recover_reservations(sms_store* store, int32_t recover_c sms_status SMS_CALL sms_get_diagnostics(sms_store* store, const sms_wait_options* wait_options, sms_diagnostics* diagnostics) { Wait wait{}; - auto implementation = store - ? store->implementation.load(std::memory_order_acquire) - : std::shared_ptr{}; + auto* implementation = store && + store->state.load(std::memory_order_acquire) == sms_store::close_state::open + ? store->implementation.get() + : nullptr; if (!implementation) return SMS_STATUS_STORE_DISPOSED; if (!diagnostics || diagnostics->struct_size < sizeof(*diagnostics) || !abi_compatible(diagnostics->abi_version) || !read_wait(wait_options, wait)) diff --git a/src/cpp/src/lifecycle_gate.hpp b/src/cpp/src/lifecycle_gate.hpp index a66ea02..1eead21 100644 --- a/src/cpp/src/lifecycle_gate.hpp +++ b/src/cpp/src/lifecycle_gate.hpp @@ -11,6 +11,7 @@ namespace sms::detail { // close callers may park through C++20 atomic wait while entered calls drain. // No object from this class is placed in shared memory. class LifecycleGate { + static_assert(std::atomic::is_always_lock_free); public: class Operation { public: @@ -60,8 +61,8 @@ class LifecycleGate { if (state_.load(std::memory_order_acquire) != open_state) { return SMS_STATUS_STORE_DISPOSED; } - active_.fetch_add(1, std::memory_order_acq_rel); - if (state_.load(std::memory_order_acquire) != open_state) { + active_.fetch_add(1, std::memory_order_seq_cst); + if (state_.load(std::memory_order_seq_cst) != open_state) { leave(); return SMS_STATUS_STORE_DISPOSED; } @@ -76,8 +77,8 @@ class LifecycleGate { auto expected = open_state; const bool owner = state_.compare_exchange_strong( expected, closing_state, - std::memory_order_acq_rel, - std::memory_order_acquire); + std::memory_order_seq_cst, + std::memory_order_seq_cst); if (!owner) { for (;;) { const auto observed = state_.load(std::memory_order_acquire); @@ -87,7 +88,7 @@ class LifecycleGate { } for (;;) { - const auto observed = active_.load(std::memory_order_acquire); + const auto observed = active_.load(std::memory_order_seq_cst); if (observed == 0) return true; active_.wait(observed, std::memory_order_acquire); } @@ -104,7 +105,11 @@ class LifecycleGate { private: void leave() noexcept { - if (active_.fetch_sub(1, std::memory_order_acq_rel) == 1) { + // Avoid the library's potentially mutex-backed wait adapter on open + // hot paths. SC ordering guarantees that close either observes zero + // or this last reader observes closing and notifies its waiter. + if (active_.fetch_sub(1, std::memory_order_seq_cst) == 1 && + state_.load(std::memory_order_seq_cst) != open_state) { active_.notify_all(); } } diff --git a/tests/cpp/CMakeLists.txt b/tests/cpp/CMakeLists.txt index deb4e76..15df3c2 100644 --- a/tests/cpp/CMakeLists.txt +++ b/tests/cpp/CMakeLists.txt @@ -4,6 +4,7 @@ set( store_tests.cpp c_api_tests.cpp lifecycle_tests.cpp + local_handle_tests.cpp diagnostics_tests.cpp) # Register the planned SMS2 test topology without making an incomplete test diff --git a/tests/cpp/local_handle_tests.cpp b/tests/cpp/local_handle_tests.cpp new file mode 100644 index 0000000..a30190d --- /dev/null +++ b/tests/cpp/local_handle_tests.cpp @@ -0,0 +1,169 @@ +#include "shared_memory_store/detail/local_handle.hpp" +#include "test_support.hpp" + +#include +#include +#include +#include + +namespace { +struct Observations { + std::atomic closed{}; + std::atomic destroyed{}; +}; + +struct Resource { + explicit Resource(Observations& observations) : observations(observations) {} + void close() noexcept { observations.closed.fetch_add(1); } + ~Resource() { observations.destroyed.fetch_add(1); } + Observations& observations; + const int value{42}; +}; + +using Handle = shared_memory_store::detail::local_handle; + +template +bool eventually(Predicate predicate) { + const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(5); + while (!predicate()) { + if (std::chrono::steady_clock::now() >= deadline) return false; + std::this_thread::yield(); + } + return true; +} + +int paused_snapshot_does_not_block_other_calls() { + Observations observations; + Handle handle; + handle.reset(std::make_unique(observations)); + std::optional paused(handle.pin()); + std::atomic done{}, successful{true}; + std::thread reader([&] { + for (int i = 0; i < 10000; ++i) { + const auto pin = handle.pin(); + if (!pin || pin->value != 42) successful = false; + } + done = true; + }); + const bool progressed = eventually([&] { return done.load(); }); + paused.reset(); + reader.join(); + SMS_CHECK(progressed && successful); + SMS_CHECK(observations.closed == 0 && observations.destroyed == 0); + handle.close(); + SMS_CHECK(observations.closed == 1 && observations.destroyed == 1); + return 0; +} + +int close_drains_snapshots_and_all_closers_observe_destruction() { + Observations observations; + Handle handle; + handle.reset(std::make_unique(observations)); + std::optional paused(handle.pin()); + std::atomic returned{}; + std::atomic saw_destruction{true}; + std::vector closers; + for (int i = 0; i < 4; ++i) { + closers.emplace_back([&] { + handle.close(); + if (observations.destroyed != 1) saw_destruction = false; + ++returned; + }); + } + const bool logically_closed = eventually([&] { return observations.closed == 1; }); + const bool drained_later = logically_closed && returned == 0 && + observations.destroyed == 0 && !handle.pin() && + paused->operator->()->value == 42; + paused.reset(); + for (auto& closer : closers) closer.join(); + SMS_CHECK(drained_later && saw_destruction && returned == 4); + SMS_CHECK(observations.closed == 1 && observations.destroyed == 1); + handle.close(); + SMS_CHECK(!handle.is_open() && !handle.pin()); + SMS_CHECK(observations.closed == 1 && observations.destroyed == 1); + return 0; +} + +int move_waits_for_snapshots_without_closing_the_resource() { + Observations observations; + Handle source; + source.reset(std::make_unique(observations)); + std::optional paused(source.pin()); + std::optional moved; + std::atomic returned{}; + std::thread mover([&] { + moved.emplace(std::move(source)); + returned = true; + }); + const bool closed_gate = eventually([&] { return !source.is_open(); }); + const bool deferred = closed_gate && !returned && observations.closed == 0 && + observations.destroyed == 0; + paused.reset(); + mover.join(); + SMS_CHECK(deferred && returned && !source.pin()); + SMS_CHECK(moved->is_open() && moved->pin()->value == 42); + SMS_CHECK(observations.closed == 0 && observations.destroyed == 0); + moved.reset(); + SMS_CHECK(observations.closed == 1 && observations.destroyed == 1); + return 0; +} + +int enter_close_races_and_replacement_preserve_ownership() { + for (int round = 0; round < 200; ++round) { + Observations observations; + Handle handle; + handle.reset(std::make_unique(observations)); + std::atomic start{}, valid{true}; + std::vector readers; + for (int i = 0; i < 4; ++i) { + readers.emplace_back([&] { + while (!start.load()) std::this_thread::yield(); + for (int attempt = 0; attempt < 100; ++attempt) { + const auto pin = handle.pin(); + if (pin && (pin->value != 42 || observations.destroyed != 0)) valid = false; + } + }); + } + start = true; + handle.close(); + for (auto& reader : readers) reader.join(); + SMS_CHECK(valid && observations.closed == 1 && observations.destroyed == 1); + handle.reset(std::make_unique(observations)); + SMS_CHECK(handle.is_open() && handle.pin()->value == 42); + handle.close(); + SMS_CHECK(observations.closed == 2 && observations.destroyed == 2); + } + return 0; +} + +int public_wrapper_move_and_close_keep_token_lifetimes() { + using namespace shared_memory_store; + memory_store source; + SMS_CHECK(memory_store::try_create_or_open(sms_test_options("local-handle-move"), source) + == open_status::success); + SMS_CHECK(source.try_publish(sms_test_bytes("key"), sms_test_bytes("value")) == status::success); + value_lease lease; + SMS_CHECK(source.try_acquire(sms_test_bytes("key"), lease) == status::success); + memory_store destination(std::move(source)); + SMS_CHECK(!source.valid() && destination.valid()); + SMS_CHECK(lease.valid() && lease.value().size() == 5); + std::thread close_one([&] { destination.close(); }); + std::thread close_two([&] { destination.close(); }); + close_one.join(); + close_two.join(); + SMS_CHECK(!destination.valid() && !lease.valid()); + SMS_CHECK(destination.try_publish(sms_test_bytes("other"), {}) == status::store_disposed); + SMS_CHECK(memory_store::try_create_or_open(sms_test_options("local-handle-reopen"), destination) + == open_status::success); + SMS_CHECK(destination.try_publish(sms_test_bytes("other"), {}) == status::success); + return 0; +} +} // namespace + +int main() { + if (paused_snapshot_does_not_block_other_calls() != 0) return 1; + if (close_drains_snapshots_and_all_closers_observe_destruction() != 0) return 1; + if (move_waits_for_snapshots_without_closing_the_resource() != 0) return 1; + if (enter_close_races_and_replacement_preserve_ownership() != 0) return 1; + return public_wrapper_move_and_close_keep_token_lifetimes(); +}