diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 5f22be917..62e0f2bcf 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -752,6 +752,11 @@ if(BUILD_TESTING) target_link_libraries(image_memory_test PRIVATE Threads::Threads) add_test(NAME image_memory COMMAND image_memory_test) + # Synchronized one-time image binding (runtime/src/deferred_image_binding.h): pure stdlib, concurrent threads. + add_executable(deferred_image_binding_test test/deferred_image_binding_test.cpp) + target_link_libraries(deferred_image_binding_test PRIVATE Threads::Threads) + add_test(NAME deferred_image_binding COMMAND deferred_image_binding_test) + add_executable(xbox_present_readback_size_test test/xbox_present_readback_size_test.cpp) add_test(NAME xbox_present_readback_size COMMAND xbox_present_readback_size_test) diff --git a/runtime/src/deferred_image_binding.h b/runtime/src/deferred_image_binding.h index 668cf593d..6ef544199 100644 --- a/runtime/src/deferred_image_binding.h +++ b/runtime/src/deferred_image_binding.h @@ -11,12 +11,13 @@ class DeferredImageBinding { public: using Bind = bool (*)(void*); DeferredImageBinding(void* owner, Bind bind) : owner_(owner), bind_(bind) {} + void* owner() const { return owner_; } bool bound() const { return bound_.load(std::memory_order_acquire); } bool ensure() { if (bound()) return true; std::lock_guard lock(mutex_); if (bound()) return true; - if (!bind_(owner_)) return false; + if (!bind_ || !bind_(owner_)) return false; bound_.store(true, std::memory_order_release); return true; } diff --git a/runtime/test/deferred_image_binding_test.cpp b/runtime/test/deferred_image_binding_test.cpp new file mode 100644 index 000000000..b7c3d28f2 --- /dev/null +++ b/runtime/test/deferred_image_binding_test.cpp @@ -0,0 +1,144 @@ +// Unit tests for DeferredImageBinding (runtime/src/deferred_image_binding.h) +// Verifies null callback protection, failure retry behavior, idempotency, +// owner pointer routing, and concurrent single-execution synchronization across threads. +#include +#include +#include +#include +#include + +#include "../src/deferred_image_binding.h" + +namespace { + +int g_checks = 0; +int g_failures = 0; + +void check(bool condition, const char* message) { + ++g_checks; + if (!condition) { + ++g_failures; + std::printf("FAIL: %s\n", message); + } else { + std::printf("PASS: %s\n", message); + } +} + +struct MockTarget { + std::uint64_t id = 42; + std::atomic bind_calls{0}; + bool should_succeed = true; +}; + +bool mock_bind(void* owner) { + if (!owner) return false; + auto* target = static_cast(owner); + target->bind_calls.fetch_add(1, std::memory_order_relaxed); + return target->should_succeed; +} + +} // namespace + +int main() { + using leonida::DeferredImageBinding; + + // 1. Initial state and owner accessor + { + MockTarget target{100, 0, true}; + DeferredImageBinding binding(&target, mock_bind); + check(!binding.bound(), "initial bound() is false"); + check(binding.owner() == &target, "owner() matches target pointer"); + } + + // 2. Null callback guard + { + MockTarget target{101, 0, true}; + DeferredImageBinding null_binding(&target, nullptr); + check(!null_binding.ensure(), "ensure() with null callback returns false"); + check(!null_binding.bound(), "bound() remains false after null callback ensure()"); + } + + // 3. Failed bind and retry behavior + { + MockTarget target{102, 0, false}; // fail initial attempts + DeferredImageBinding binding(&target, mock_bind); + + check(!binding.ensure(), "ensure() returns false when callback fails"); + check(!binding.bound(), "bound() remains false on failure"); + check(target.bind_calls.load() == 1, "callback invoked once on first attempt"); + + // Retry while still failing + check(!binding.ensure(), "retry ensure() returns false when callback still fails"); + check(!binding.bound(), "bound() still false"); + check(target.bind_calls.load() == 2, "callback invoked second time on retry"); + + // Now permit success + target.should_succeed = true; + check(binding.ensure(), "ensure() returns true once callback succeeds"); + check(binding.bound(), "bound() becomes true"); + check(target.bind_calls.load() == 3, "callback invoked third time and succeeded"); + + // 4. Idempotency: subsequent calls do not invoke callback again + check(binding.ensure(), "subsequent ensure() returns true immediately"); + check(target.bind_calls.load() == 3, "callback not invoked again after success"); + check(binding.bound(), "bound() remains true"); + } + + // 5. Null owner pointer handling + { + std::atomic called{false}; + auto null_owner_bind = [](void* owner) -> bool { + return owner == nullptr; + }; + DeferredImageBinding binding(nullptr, null_owner_bind); + check(binding.owner() == nullptr, "owner() is nullptr"); + check(binding.ensure(), "ensure() with nullptr owner passes through to callback"); + check(binding.bound(), "bound() is true"); + } + + // 6. Multithreaded concurrency test (16 threads racing on ensure) + { + MockTarget shared_target{999, 0, true}; + DeferredImageBinding shared_binding(&shared_target, mock_bind); + + constexpr int kThreadCount = 16; + std::vector workers; + workers.reserve(kThreadCount); + std::atomic success_count{0}; + + for (int i = 0; i < kThreadCount; ++i) { + workers.emplace_back([&] { + if (shared_binding.ensure()) { + success_count.fetch_add(1, std::memory_order_relaxed); + } + }); + } + + for (auto& t : workers) { + t.join(); + } + + check(success_count.load() == kThreadCount, "all 16 concurrent threads reported ensure() == true"); + check(shared_target.bind_calls.load() == 1, "callback executed exactly once under concurrent races"); + check(shared_binding.bound(), "shared_binding.bound() is true after threads finish"); + } + + // 7. Multiple independent instances + { + MockTarget t1{1, 0, true}; + MockTarget t2{2, 0, true}; + DeferredImageBinding b1(&t1, mock_bind); + DeferredImageBinding b2(&t2, mock_bind); + + check(b1.ensure(), "b1 binds successfully"); + check(b1.bound() && !b2.bound(), "b1 is bound while b2 remains unbound"); + check(t1.bind_calls.load() == 1 && t2.bind_calls.load() == 0, "only t1 was invoked"); + + check(b2.ensure(), "b2 binds successfully"); + check(b1.bound() && b2.bound(), "both bindings are now bound"); + check(t1.bind_calls.load() == 1 && t2.bind_calls.load() == 1, "both targets invoked exactly once"); + } + + std::printf("\nSummary: %d checks, %d failures\n", g_checks, g_failures); + return g_failures == 0 ? 0 : 1; +}