Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions docs/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
128 changes: 128 additions & 0 deletions src/cpp/include/shared_memory_store/detail/local_handle.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#pragma once

#include <atomic>
#include <cstdint>
#include <memory>
#include <utility>

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 T>
class local_handle {
enum class state : std::uint32_t { closed, open, closing };
static_assert(std::atomic<state>::is_always_lock_free);
static_assert(std::atomic<std::uint64_t>::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<T> 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<T> take() noexcept {
if (!begin_close()) return {};
drain();
auto value = std::move(value_);
finish_close();
return value;
}

std::unique_ptr<T> value_;
std::atomic<state> state_{state::closed};
mutable std::atomic<std::uint64_t> active_{};
};
} // namespace shared_memory_store::detail
57 changes: 18 additions & 39 deletions src/cpp/include/shared_memory_store/store.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "c_api.h"
#include "detail/local_handle.hpp"

#include <atomic>
#include <cstddef>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -538,17 +530,16 @@ class memory_store {
return open_status::mapping_failed;
}
try {
result.control_.store(
std::make_shared<store_control>(
result.control_.reset(
std::make_unique<store_control>(
opened_handle,
protocol_info{
identity.layout_major,
identity.layout_minor,
identity.resource_protocol,
identity.required_features,
identity.optional_features,
}),
std::memory_order_release);
}));
} catch (...) {
sms_close_store(opened_handle);
sms_destroy_store(opened_handle);
Expand All @@ -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<store_control> 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<const std::byte> key,
std::span<const std::byte> value,
std::span<const std::byte> 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<status>(sms_publish(
Expand All @@ -603,7 +582,7 @@ class memory_store {
reinterpret_cast<const std::uint8_t*>(value.data()),
static_cast<std::uint64_t>(value.size())});
}
const auto control = control_.load(std::memory_order_acquire);
const auto control = control_.pin();
if (!control) {
copied = 0;
return status::store_disposed;
Expand All @@ -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);
Expand All @@ -637,7 +616,7 @@ class memory_store {
status try_remove(
std::span<const std::byte> 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<status>(sms_remove(
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -704,15 +683,15 @@ 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<status>(sms_get_diagnostics(
control->handle, &native, &snapshot.value_));
}

private:
std::atomic<std::shared_ptr<store_control>> control_;
detail::local_handle<store_control> control_;
};

} // namespace shared_memory_store
Loading
Loading