diff --git a/CMakeLists.txt b/CMakeLists.txt index d960e92..87f0665 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,6 +69,8 @@ if(TARGET value_test) target_include_directories(value_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") endif() if(TARGET resource_test) + find_package(Threads REQUIRED) + target_link_libraries(resource_test PRIVATE Threads::Threads) target_include_directories(resource_test PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") set_tests_properties(resource_test PROPERTIES TIMEOUT 30) endif() diff --git a/include/cel/runtime/detail/retirement.hpp b/include/cel/runtime/detail/retirement.hpp index 73688b3..5fd1296 100644 --- a/include/cel/runtime/detail/retirement.hpp +++ b/include/cel/runtime/detail/retirement.hpp @@ -39,7 +39,7 @@ struct retirement_node { /// constant stack space, including during exception unwinding. class retirement_queue { // [knowledge] Reuse dying nodes' links as the worklist; never allocate a traversal - // container or recursively destroy runtime-owned executable edges. + // container or recursively destroy runtime-owned child edges. /// Owns nodes awaiting disposal. retirement_node* _pending = nullptr; @@ -120,6 +120,7 @@ class retirement_ptr { /// Self-copy preserves ownership; the incoming node stays alive while prior ownership retires. /// /// Complexity: prior ownership retirement costs. + // NOLINTNEXTLINE(bugprone-unhandled-self-assignment): copy-and-swap is self-copy safe. retirement_ptr& operator=(const retirement_ptr& other) noexcept { retirement_ptr next(other); swap(next); @@ -145,6 +146,12 @@ class retirement_ptr { void retire(retirement_queue& queue) noexcept { queue.release(std::exchange(_node, nullptr)); } /// Reports whether this handle owns a node. explicit operator bool() const noexcept { return _node != nullptr; } + /// Borrows the retained node, or returns null for an empty handle. + /// + /// A retained owner must keep the node alive throughout every use of the pointer. + T* get() const noexcept { return static_cast(_node); } + /// Compares retained node identities, including empty handles. + friend bool operator==(const retirement_ptr&, const retirement_ptr&) noexcept = default; /// Borrows the retained concrete node. /// /// \pre This handle owns a node. @@ -152,7 +159,7 @@ class retirement_ptr { /// throughout every use. T* operator->() const noexcept { assert(_node); - return static_cast(_node); + return get(); } }; } // namespace cel::detail diff --git a/include/cel/runtime/value_type.hpp b/include/cel/runtime/value_type.hpp index ebd61cf..e352dff 100644 --- a/include/cel/runtime/value_type.hpp +++ b/include/cel/runtime/value_type.hpp @@ -2,6 +2,7 @@ #ifndef CEL_RUNTIME_VALUE_TYPE_HPP #define CEL_RUNTIME_VALUE_TYPE_HPP +#include #include #include @@ -63,23 +64,18 @@ struct type_access; /// Example: `auto numbers = value_type::array(value_type::leaf()); auto copy = numbers;`. class value_type { friend struct detail::type_access; - std::shared_ptr _plan; + detail::retirement_ptr _plan; /// Retains complete immutable construction metadata. /// /// \pre The plan is complete and no mutable alias can change it after publication. - explicit value_type(std::shared_ptr plan); + explicit value_type(std::unique_ptr plan); /// Registers a trusted concrete leaf with a checked layout. /// /// \pre Identity and layout describe the same trivially copyable concrete type. /// Throws std::invalid_argument for invalid alignment and std::overflow_error for /// alignment rounding overflow. static value_type make_leaf(std::type_index identity, layout bytes); - /// Releases this reference and uniquely owned metadata without recursion or allocation. - /// - /// Complexity: O(V + E + S) time and O(1) auxiliary space, for released plans V, - /// their descriptor edges E, and stored name bytes S. - static void release_plan(std::shared_ptr& plan) noexcept; public: /// Creates the empty tuple descriptor (source unit). diff --git a/src/type_ops.hpp b/src/type_ops.hpp index e394531..947e83d 100644 --- a/src/type_ops.hpp +++ b/src/type_ops.hpp @@ -79,6 +79,11 @@ class compatibility_context { /// /// These operations do not implement callable execution or concrete closure bodies. struct type_access { + /// Releases a descriptor reference into an enclosing retirement operation. + /// + /// \pre type will only be destroyed after this call. + /// A last-owned plan remains queue-owned until disposal; shared plans may retire elsewhere. + static void retire(value_type& type, retirement_queue& queue) noexcept; /// Reports shared complete immutable metadata identity without traversing the shape. static bool same_plan(const value_type& a, const value_type& b) noexcept { return a._plan == b._plan; diff --git a/src/value_type.cpp b/src/value_type.cpp index 5d6e04a..bf93dfe 100644 --- a/src/value_type.cpp +++ b/src/value_type.cpp @@ -26,11 +26,12 @@ void require_ownership(const value_type& type) { } /// Immutable complete semantic metadata after publication. -struct type_plan { +struct type_plan : retirement_node { // [knowledge] Fields and offsets are canonical and category-appropriate. ownership // borrows retained descendants, collapses unary paths, and puts a largest range last. // Rebuild these borrowed ranges whenever copied fields are rebound. - // Only dying, uniquely owned plans may use release_next or otherwise mutate published data. + // Keep published fields immutable. Detach every child into the last-owner queue before + // deletion. type_kind kind = type_kind::tuple; // Identifies the shape. layout bytes{0, 1}; // Describes its canonical representation. bool owns = true; // Records complete ownership capabilities. @@ -46,9 +47,39 @@ struct type_plan { std::optional result; // Retains a complete callable result. range_kind form = range_kind::range_full; // Retains source range form. opaque_ownership operations; // Retains trusted callable clone/destruction operations. - std::shared_ptr release_next; // Links only dying, uniquely owned plans. + + /// Creates unpublished metadata with one initial ownership reference. + type_plan() : retirement_node(dispose_plan) {} + + /// Copies immutable metadata into a fresh ownership node. + /// + /// Complexity: linear in immediate metadata and copied name bytes; new metadata storage. + type_plan(const type_plan& other) : + retirement_node(dispose_plan), kind(other.kind), bytes(other.bytes), owns(other.owns), + parameter(other.parameter), resources(other.resources), leaf(other.leaf), + fields(other.fields), offsets(other.offsets), ownership(other.ownership), + names(other.names), identity(other.identity), element(other.element), result(other.result), + form(other.form), operations(other.operations) {} + + /// Detaches all descriptor edges into queue and deletes the last-owned plan. + /// + /// \pre base is an exclusively retiring type_plan. + /// Complexity: linear in immediate fields and stored names; O(1) auxiliary space. + static void dispose_plan(retirement_node& base, retirement_queue& queue) noexcept { + auto& plan = static_cast(base); + if (plan.element) type_access::retire(*plan.element, queue); + if (plan.result) type_access::retire(*plan.result, queue); + // Clear all owning fields before their containing storage is released. + for (auto& field : plan.fields) + type_access::retire(field, queue); + delete &plan; + } }; +void type_access::retire(value_type& type, retirement_queue& queue) noexcept { + type._plan.retire(queue); +} + /// Compiles reusable carrier ranges from complete canonical fields. /// /// \pre plan is unpublished and its fields and offsets agree. @@ -88,7 +119,7 @@ value_type type_access::install_callable_ownership(const value_type& signature, opaque_ownership operations) { if (signature.kind() != type_kind::callable || !operations.clone || !operations.destroy) throw std::invalid_argument("callable ownership requires both operations"); - auto plan = std::make_shared(*signature._plan); + auto plan = std::make_unique(*signature._plan.get()); plan->operations = operations; plan->owns = true; return value_type(std::move(plan)); @@ -147,7 +178,7 @@ value_type type_access::bind_runtime_ownership(const value_type& type, opaque_ow if (!changed) { bound.emplace(source, *current.type); } else { - auto plan = std::make_shared(*source); + auto plan = std::make_unique(*source); std::transform(source->fields.begin(), source->fields.end(), plan->fields.begin(), transformed); if (source->element) plan->element = transformed(*source->element); @@ -316,48 +347,18 @@ bool parameter_allowed(const value_type& type) { return type_access::parameter_a namespace cel { -value_type::value_type(std::shared_ptr plan) : _plan(std::move(plan)) {} +value_type::value_type(std::unique_ptr plan) : _plan(plan.release()) {} value_type::value_type() : value_type(tuple({})) {} -void value_type::release_plan(std::shared_ptr& root) noexcept { - auto pending = std::move(root); - // Move dying descendants into their own worklist links before deleting any parent. - while (pending) { - auto current = std::move(pending); - if (current.use_count() != 1) { - current.reset(); - continue; - } - auto& plan = *const_cast(current.get()); - pending = std::move(plan.release_next); - const auto detach = [&](value_type& child) { - auto next = std::move(child._plan); - if (next && next.use_count() == 1) { - auto& node = *const_cast(next.get()); - node.release_next = std::move(pending); - pending = std::move(next); - } - }; - if (plan.element) detach(*plan.element); - if (plan.result) detach(*plan.result); - for (auto& field : plan.fields) - detach(field); - current.reset(); - } -} -value_type::~value_type() { release_plan(_plan); } +value_type::~value_type() = default; value_type& value_type::operator=(const value_type& other) noexcept { - if (this != &other) { - auto next = other._plan; - release_plan(_plan); - _plan = std::move(next); - } + _plan = other._plan; return *this; } value_type value_type::make_leaf(std::type_index identity, layout bytes) { (void)detail::align_offset(bytes.size, bytes.alignment); - auto plan = std::make_shared(); + auto plan = std::make_unique(); plan->kind = type_kind::leaf; plan->bytes = bytes; plan->leaf = identity; @@ -366,7 +367,7 @@ value_type value_type::make_leaf(std::type_index identity, layout bytes) { } value_type value_type::array(value_type element) { - auto plan = std::make_shared(); + auto plan = std::make_unique(); plan->kind = type_kind::array; plan->bytes = {sizeof(void*), alignof(void*)}; plan->owns = element.owns_values(); @@ -377,7 +378,7 @@ value_type value_type::array(value_type element) { } value_type value_type::tuple(std::span fields) { - auto plan = std::make_shared(); + auto plan = std::make_unique(); plan->fields.assign(fields.begin(), fields.end()); detail::canonical_layout(*plan, fields); detail::compile_ownership(*plan); @@ -386,7 +387,7 @@ value_type value_type::tuple(std::span fields) { value_type value_type::record(std::string_view identity, std::span fields) { if (identity.empty()) throw std::invalid_argument("record identity is empty"); - auto plan = std::make_shared(); + auto plan = std::make_unique(); plan->kind = type_kind::record; plan->identity = identity; std::unordered_set names; @@ -421,7 +422,7 @@ value_type value_type::range(range_kind form, value_type endpoint) { default: throw std::invalid_argument("invalid range form"); } - auto plan = std::make_shared(); + auto plan = std::make_unique(); plan->kind = type_kind::range; plan->form = form; plan->element = endpoint; @@ -434,7 +435,7 @@ value_type value_type::range(range_kind form, value_type endpoint) { value_type value_type::callable(std::span parameters, value_type result) { if (!std::all_of(parameters.begin(), parameters.end(), detail::parameter_allowed)) throw std::invalid_argument("callable parameters require built-in composite values"); - auto plan = std::make_shared(); + auto plan = std::make_unique(); plan->kind = type_kind::callable; plan->bytes = {sizeof(void*), alignof(void*)}; plan->owns = false; diff --git a/tests/resource_test.cpp b/tests/resource_test.cpp index d8b8858..ae1a10a 100644 --- a/tests/resource_test.cpp +++ b/tests/resource_test.cpp @@ -8,10 +8,163 @@ #include #include +#include +#include #include +#include #include +#include #include +/// Runs two nonthrowing callbacks on separate threads behind one start gate and joins them. +/// +/// Thread-start failures propagate after releasing any already started worker. +/// Complexity: callback work plus thread startup and joining; O(1) auxiliary space. +template +void run_concurrently(First first, Second second) { + std::latch start{1}; + std::array threads; + const auto release = [](std::latch* gate) noexcept { gate->count_down(); }; + const std::unique_ptr release_on_exit(&start, release); + threads[0] = std::jthread([&] { + start.wait(); + first(); + }); + threads[1] = std::jthread([&] { + start.wait(); + second(); + }); +} + +/// Records actual node disposal and reentrant callbacks across independent retirement queues. +struct retirement_counts { + std::atomic disposed{0}; ///< Counts destroyed nodes. + std::atomic nested{false}; ///< Records nesting within one thread. +}; + +/// An owned chain node for checking the retirement primitive used by type metadata. +struct retirement_probe : cel::detail::retirement_node { + retirement_counts& counts; ///< Borrows counters until every worker has joined. + cel::detail::retirement_ptr child; ///< Retains the next chain node. + static inline thread_local std::size_t depth = 0; ///< Counts active callbacks on this thread. + + /// Adopts one child edge and retains the disposal counters. + retirement_probe(retirement_counts& state, cel::detail::retirement_ptr next) : + retirement_node(dispose_node), counts(state), child(std::move(next)) {} + + /// Records disposal, detaches the child into queue, and deletes this node. + /// + /// \pre base is an exclusively retiring retirement_probe. + static void dispose_node(cel::detail::retirement_node& base, + cel::detail::retirement_queue& queue) noexcept { + auto& node = static_cast(base); + ++depth; + if (depth != 1) node.counts.nested.store(true, std::memory_order_relaxed); + node.child.retire(queue); + node.counts.disposed.fetch_add(1, std::memory_order_relaxed); + delete &node; + --depth; + } +}; + +/// Catches premature disposal or wrong last-owner selection during concurrent edge releases. +TEST_CASE("concurrent last owners retire each node once without nested callbacks") { + constexpr std::size_t count = 12000; + retirement_counts counts; + cel::detail::retirement_ptr root; + CHECK(root.get() == nullptr); + std::vector> aliases; + // Retain interior aliases so independent queues also compete below the root. + for (std::size_t i = 0; i != count; ++i) { + auto next = std::make_unique(counts, std::move(root)); + root = cel::detail::retirement_ptr(next.release()); + if (i % 2 == 0) aliases.push_back(root); + } + { + auto copy = root; + CHECK(copy == root); + CHECK(copy.get() == root.get()); + auto moved = std::move(copy); + // NOLINTNEXTLINE(bugprone-use-after-move): moves promise an empty source handle. + CHECK_FALSE(copy); + CHECK(moved == root); + } + CHECK(counts.disposed.load() == 0); + run_concurrently([owner = std::move(root)]() mutable { owner = {}; }, + [owners = std::move(aliases)]() mutable { + while (!owners.empty()) + owners.pop_back(); + }); + CHECK(counts.disposed.load() == count); + CHECK_FALSE(counts.nested.load()); +} + +/// Catches disposing metadata before its last-owner queue drains. +TEST_CASE("queued metadata retains borrowed views until disposal") { + const auto boolean = cel::value_type::leaf(); + auto type = cel::value_type::record( + "queued", std::array{cel::record_field{"items", cel::value_type::array(boolean)}, + cel::record_field{"code", cel::value_type::callable( + std::array{boolean}, + cel::value_type::leaf())}}); + const auto fields = type.fields(); + const auto names = type.field_names(); + const auto identity = type.record_identity(); + const auto& element = fields[0].element(); + const auto parameters = fields[1].parameters(); + const auto& result = fields[1].result(); + cel::detail::retirement_queue queue; + cel::detail::type_access::retire(type, queue); + CHECK(identity == "queued"); + CHECK(names[0] == "items"); + CHECK(names[1] == "code"); + CHECK(element.same_shape(boolean)); + CHECK(parameters[0].same_shape(boolean)); + CHECK(result.same_shape(cel::value_type::leaf())); +} + +/// Adds an array, shared aggregate, or callable-result edge. +/// +/// Complexity: O(1) time and new metadata space. +cel::value_type extend_metadata(const cel::value_type& child, std::size_t level) { + switch (level % 4) { + case 0: + return cel::value_type::array(child); + case 1: + return cel::value_type::tuple(std::array{child, child}); + case 2: + return cel::value_type::record("shared", std::array{cel::record_field{"left", child}, + cel::record_field{"right", child}}); + default: + return cel::value_type::callable({}, child); + } +} + +/// Catches invalidating retained metadata during concurrent root and interior-alias release. +TEST_CASE("concurrent metadata release preserves retained shared descendants") { + constexpr std::size_t depth = 12000; + auto type = cel::value_type::leaf(); + auto expected = cel::value_type::leaf(); + std::vector aliases; + // Construct a second, independent shape for the descendant that survives retirement. + for (std::size_t i = 0; i != depth / 2; ++i) + expected = extend_metadata(expected, i); + for (std::size_t i = 0; i != depth; ++i) { + type = extend_metadata(type, i); + aliases.push_back(type); + } + const auto survivor = aliases[depth / 2 - 1]; + std::vector roots{type}; + type = cel::value_type{}; + run_concurrently([owners = std::move(roots)]() mutable { owners.clear(); }, + [owners = std::move(aliases)]() mutable { + while (!owners.empty()) + owners.pop_back(); + }); + CHECK(survivor.same_shape(expected)); + CHECK(survivor.result().kind() == cel::type_kind::record); +} + /// Checks every carrier offset and a linear bound on constant-time traversal steps. /// Complexity: O(count log count) time and O(count) temporary space. void check_ownership_traversal(const cel::value_type& type, std::size_t count) {