From 6de076dd9c29dcbfe829f76ed107106f9b1c7390 Mon Sep 17 00:00:00 2001 From: tzcnt Date: Thu, 9 Jul 2026 21:20:52 -0700 Subject: [PATCH 1/4] add non-suspending mux::poll() --- include/tmc/detail/result_each.hpp | 9 +++++++-- include/tmc/detail/result_each.ipp | 27 ++++++++++++++++++++++++++- include/tmc/mux_many.hpp | 21 +++++++++++++++++++-- include/tmc/mux_tuple.hpp | 21 +++++++++++++++++++-- 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/include/tmc/detail/result_each.hpp b/include/tmc/detail/result_each.hpp index 845f5c5b..c0f2ea82 100644 --- a/include/tmc/detail/result_each.hpp +++ b/include/tmc/detail/result_each.hpp @@ -9,8 +9,6 @@ // Common implementations of .result_each() awaitable functions // for spawn_many and spawn_tuple -#include "tmc/ex_any.hpp" - #include #include #include @@ -27,6 +25,13 @@ TMC_DECL size_t result_each_await_resume( ptrdiff_t& remaining_count, std::atomic& sync_flags ) noexcept; +// Non-suspending variant of result_each_await_resume(). If a result is ready, +// consumes it and returns its slot index. Returns 64 (the end() sentinel) when +// no submitted results remain, or 65 (the none() sentinel) when results are still pending +// but none is ready. +TMC_DECL size_t +result_each_poll(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept; + } // namespace detail } // namespace tmc diff --git a/include/tmc/detail/result_each.ipp b/include/tmc/detail/result_each.ipp index 95c17850..3553d859 100644 --- a/include/tmc/detail/result_each.ipp +++ b/include/tmc/detail/result_each.ipp @@ -6,6 +6,7 @@ #pragma once #include "tmc/detail/awaitable_customizer.hpp" +#include "tmc/detail/compat.hpp" #include "tmc/detail/impl.hpp" // IWYU pragma: keep #include "tmc/detail/result_each.hpp" @@ -54,7 +55,7 @@ size_t result_each_await_resume( ptrdiff_t& remaining_count, std::atomic& sync_flags ) noexcept { if (remaining_count == 0) { - return 64; + return TMC_PLATFORM_BITS; } size_t resumeState = sync_flags.load(std::memory_order_acquire); assert((resumeState & tmc::detail::task_flags::EACH) != 0); @@ -67,5 +68,29 @@ size_t result_each_await_resume( return slot; } +size_t +result_each_poll(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept { + if (remaining_count == 0) { + // No submitted results remain: same terminal value as await_resume(), + // which the caller compares against end(). + return TMC_PLATFORM_BITS; + } + // The owner holds the EACH (lock) bit while it is running, so children that + // complete concurrently set their result bit but cannot resume us. The + // acquire load synchronizes-with the child's release-store of that bit, + // making its result visible. + size_t state = sync_flags.load(std::memory_order_acquire); + assert((state & tmc::detail::task_flags::EACH) != 0); + size_t slots = state & ~tmc::detail::task_flags::EACH; + if (slots == 0) { + // Results are still pending, but none is ready yet. + return TMC_PLATFORM_BITS + 1; + } + size_t slot = static_cast(std::countr_zero(slots)); + --remaining_count; + sync_flags.fetch_sub(TMC_ONE_BIT << slot, std::memory_order_release); + return slot; +} + } // namespace detail } // namespace tmc diff --git a/include/tmc/mux_many.hpp b/include/tmc/mux_many.hpp index 78f45266..a66037af 100644 --- a/include/tmc/mux_many.hpp +++ b/include/tmc/mux_many.hpp @@ -331,9 +331,26 @@ class mux_many : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// movable). The awaiter is the group itself. mux_many& operator co_await() & noexcept { return *this; } + /// Non-suspending check for a ready result. There are three outcomes: + /// - a result is ready: it is consumed and its index returned (like `co_await`). + /// - results are still pending but none are ready: returns `none()`. + /// - no submitted results remain: returns `end()`. + [[nodiscard]] inline size_t poll() noexcept { + auto slot = tmc::detail::result_each_poll(remaining_count, sync_flags); + if (slot < TMC_PLATFORM_BITS) { + active_slots &= ~(TMC_ONE_BIT << slot); + } + return slot; + } + /// Provides a sentinel value that can be compared against the value returned - /// from co_await. - inline constexpr size_t end() const noexcept { return 64; } + /// from `co_await` or `poll()`. + inline constexpr size_t end() const noexcept { return TMC_PLATFORM_BITS; } + + /// Provides a sentinel value returned by `poll()` to indicate that no result is ready + /// right now, but may become ready later. This is distinct from `end()`, which + /// indicates that all submitted results have been consumed. + inline constexpr size_t none() const noexcept { return TMC_PLATFORM_BITS + 1; } /// Returns the capacity of the mux, equal to the `Count` template argument. /// This is the maximum number of awaitables that may be active concurrently. diff --git a/include/tmc/mux_tuple.hpp b/include/tmc/mux_tuple.hpp index 57e123b7..eb825d4d 100644 --- a/include/tmc/mux_tuple.hpp +++ b/include/tmc/mux_tuple.hpp @@ -222,9 +222,26 @@ class mux_tuple : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// movable). The awaiter is the group itself. mux_tuple& operator co_await() & noexcept { return *this; } + /// Non-suspending check for a ready result. There are three outcomes: + /// - a result is ready: it is consumed and its index returned (like `co_await`). + /// - results are still pending but none are ready: returns `none()`. + /// - no submitted results remain: returns `end()`. + [[nodiscard]] inline size_t poll() noexcept { + auto slot = tmc::detail::result_each_poll(remaining_count, sync_flags); + if (slot < TMC_PLATFORM_BITS) { + active_slots &= ~(TMC_ONE_BIT << slot); + } + return slot; + } + /// Provides a sentinel value that can be compared against the value returned - /// from co_await. - inline constexpr size_t end() const noexcept { return 64; } + /// from `co_await` or `poll()`. + inline constexpr size_t end() const noexcept { return TMC_PLATFORM_BITS; } + + /// Provides a sentinel value returned by `poll()` to indicate that no result is ready + /// right now, but may become ready later. This is distinct from `end()`, which + /// indicates that all submitted results have been consumed. + inline constexpr size_t none() const noexcept { return TMC_PLATFORM_BITS + 1; } /// Returns the capacity of the mux, equal to the number of `Result` template arguments. /// This is the maximum number of awaitables that may be active concurrently. From e2a2fc3838d8c3d19431118339d793699b6be21f Mon Sep 17 00:00:00 2001 From: tzcnt Date: Thu, 9 Jul 2026 21:29:38 -0700 Subject: [PATCH 2/4] rename header to mux_shared --- include/tmc/detail/{result_each.hpp => mux_shared.hpp} | 2 +- include/tmc/detail/{result_each.ipp => mux_shared.ipp} | 2 +- include/tmc/mux_many.hpp | 2 +- include/tmc/mux_tuple.hpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename include/tmc/detail/{result_each.hpp => mux_shared.hpp} (97%) rename include/tmc/detail/{result_each.ipp => mux_shared.ipp} (98%) diff --git a/include/tmc/detail/result_each.hpp b/include/tmc/detail/mux_shared.hpp similarity index 97% rename from include/tmc/detail/result_each.hpp rename to include/tmc/detail/mux_shared.hpp index c0f2ea82..390ad405 100644 --- a/include/tmc/detail/result_each.hpp +++ b/include/tmc/detail/mux_shared.hpp @@ -36,5 +36,5 @@ result_each_poll(ptrdiff_t& remaining_count, std::atomic& sync_flags) no } // namespace tmc #if !defined(TMC_STANDALONE_COMPILATION) || defined(TMC_IMPL) -#include "tmc/detail/result_each.ipp" +#include "tmc/detail/mux_shared.ipp" #endif diff --git a/include/tmc/detail/result_each.ipp b/include/tmc/detail/mux_shared.ipp similarity index 98% rename from include/tmc/detail/result_each.ipp rename to include/tmc/detail/mux_shared.ipp index 3553d859..c6c19d12 100644 --- a/include/tmc/detail/result_each.ipp +++ b/include/tmc/detail/mux_shared.ipp @@ -8,7 +8,7 @@ #include "tmc/detail/awaitable_customizer.hpp" #include "tmc/detail/compat.hpp" #include "tmc/detail/impl.hpp" // IWYU pragma: keep -#include "tmc/detail/result_each.hpp" +#include "tmc/detail/mux_shared.hpp" #include #include diff --git a/include/tmc/mux_many.hpp b/include/tmc/mux_many.hpp index a66037af..a1ffe7a1 100644 --- a/include/tmc/mux_many.hpp +++ b/include/tmc/mux_many.hpp @@ -10,7 +10,7 @@ #include "tmc/detail/compat.hpp" #include "tmc/detail/concepts_awaitable.hpp" // IWYU pragma: keep #include "tmc/detail/concepts_work_item.hpp" -#include "tmc/detail/result_each.hpp" +#include "tmc/detail/mux_shared.hpp" #include "tmc/detail/thread_locals.hpp" #include "tmc/detail/tsan.hpp" #include "tmc/ex_any.hpp" diff --git a/include/tmc/mux_tuple.hpp b/include/tmc/mux_tuple.hpp index eb825d4d..6a163e8a 100644 --- a/include/tmc/mux_tuple.hpp +++ b/include/tmc/mux_tuple.hpp @@ -10,7 +10,7 @@ #include "tmc/detail/compat.hpp" #include "tmc/detail/concepts_awaitable.hpp" // IWYU pragma: keep #include "tmc/detail/concepts_work_item.hpp" -#include "tmc/detail/result_each.hpp" +#include "tmc/detail/mux_shared.hpp" #include "tmc/detail/thread_locals.hpp" #include "tmc/detail/tsan.hpp" #include "tmc/detail/tuple_helpers.hpp" From 820db2519bc22810605c106b515a85012c18da1e Mon Sep 17 00:00:00 2001 From: tzcnt Date: Thu, 9 Jul 2026 21:34:42 -0700 Subject: [PATCH 3/4] rename shared mux functions --- include/tmc/detail/mux_shared.hpp | 16 +++++----------- include/tmc/detail/mux_shared.ipp | 10 ++++------ include/tmc/mux_many.hpp | 6 +++--- include/tmc/mux_tuple.hpp | 6 +++--- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/include/tmc/detail/mux_shared.hpp b/include/tmc/detail/mux_shared.hpp index 390ad405..86643e92 100644 --- a/include/tmc/detail/mux_shared.hpp +++ b/include/tmc/detail/mux_shared.hpp @@ -6,8 +6,7 @@ #pragma once #include "tmc/detail/impl.hpp" // IWYU pragma: keep -// Common implementations of .result_each() awaitable functions -// for spawn_many and spawn_tuple +// Common implementations of awaitable functions for mux_many and mux_tuple #include #include @@ -16,21 +15,16 @@ namespace tmc { namespace detail { -TMC_DECL bool result_each_await_suspend( +TMC_DECL bool mux_await_suspend( ptrdiff_t remaining_count, std::coroutine_handle<> Outer, std::coroutine_handle<>& continuation, std::atomic& sync_flags ) noexcept; -TMC_DECL size_t result_each_await_resume( - ptrdiff_t& remaining_count, std::atomic& sync_flags -) noexcept; +TMC_DECL size_t +mux_await_resume(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept; -// Non-suspending variant of result_each_await_resume(). If a result is ready, -// consumes it and returns its slot index. Returns 64 (the end() sentinel) when -// no submitted results remain, or 65 (the none() sentinel) when results are still pending -// but none is ready. TMC_DECL size_t -result_each_poll(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept; +mux_poll(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept; } // namespace detail } // namespace tmc diff --git a/include/tmc/detail/mux_shared.ipp b/include/tmc/detail/mux_shared.ipp index c6c19d12..0dbcb00c 100644 --- a/include/tmc/detail/mux_shared.ipp +++ b/include/tmc/detail/mux_shared.ipp @@ -18,7 +18,7 @@ namespace tmc { namespace detail { -bool result_each_await_suspend( +bool mux_await_suspend( ptrdiff_t remaining_count, std::coroutine_handle<> Outer, std::coroutine_handle<>& continuation, std::atomic& sync_flags ) noexcept { @@ -51,9 +51,8 @@ bool result_each_await_suspend( } } -size_t result_each_await_resume( - ptrdiff_t& remaining_count, std::atomic& sync_flags -) noexcept { +size_t +mux_await_resume(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept { if (remaining_count == 0) { return TMC_PLATFORM_BITS; } @@ -68,8 +67,7 @@ size_t result_each_await_resume( return slot; } -size_t -result_each_poll(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept { +size_t mux_poll(ptrdiff_t& remaining_count, std::atomic& sync_flags) noexcept { if (remaining_count == 0) { // No submitted results remain: same terminal value as await_resume(), // which the caller compares against end(). diff --git a/include/tmc/mux_many.hpp b/include/tmc/mux_many.hpp index a1ffe7a1..6048a375 100644 --- a/include/tmc/mux_many.hpp +++ b/include/tmc/mux_many.hpp @@ -308,7 +308,7 @@ class mux_many : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { // mux_many can be awaited in a different context than where it was // created. Therefore, capture the continuation_executor at the await point. continuation_executor = tmc::detail::this_thread::executor(); - return tmc::detail::result_each_await_suspend( + return tmc::detail::mux_await_suspend( remaining_count, Outer, continuation, sync_flags ); } @@ -320,7 +320,7 @@ class mux_many : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// submission. When no submitted results remain, the index returned will be /// equal to the value of `end()`. [[nodiscard]] inline size_t await_resume() noexcept { - auto slot = tmc::detail::result_each_await_resume(remaining_count, sync_flags); + auto slot = tmc::detail::mux_await_resume(remaining_count, sync_flags); if (slot != end()) { active_slots &= ~(TMC_ONE_BIT << slot); } @@ -336,7 +336,7 @@ class mux_many : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// - results are still pending but none are ready: returns `none()`. /// - no submitted results remain: returns `end()`. [[nodiscard]] inline size_t poll() noexcept { - auto slot = tmc::detail::result_each_poll(remaining_count, sync_flags); + auto slot = tmc::detail::mux_poll(remaining_count, sync_flags); if (slot < TMC_PLATFORM_BITS) { active_slots &= ~(TMC_ONE_BIT << slot); } diff --git a/include/tmc/mux_tuple.hpp b/include/tmc/mux_tuple.hpp index 6a163e8a..18d6d0ba 100644 --- a/include/tmc/mux_tuple.hpp +++ b/include/tmc/mux_tuple.hpp @@ -201,7 +201,7 @@ class mux_tuple : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { // mux_tuple can be awaited in a different context than where it was // created. Therefore, capture the continuation_executor at the await point. continuation_executor = tmc::detail::this_thread::executor(); - return tmc::detail::result_each_await_suspend( + return tmc::detail::mux_await_suspend( remaining_count, Outer, continuation, sync_flags ); } @@ -211,7 +211,7 @@ class mux_tuple : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// exactly once per submission. When no submitted results remain, the index /// returned will be equal to the value of `end()`. [[nodiscard]] inline size_t await_resume() noexcept { - auto slot = tmc::detail::result_each_await_resume(remaining_count, sync_flags); + auto slot = tmc::detail::mux_await_resume(remaining_count, sync_flags); if (slot != end()) { active_slots &= ~(TMC_ONE_BIT << slot); } @@ -227,7 +227,7 @@ class mux_tuple : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// - results are still pending but none are ready: returns `none()`. /// - no submitted results remain: returns `end()`. [[nodiscard]] inline size_t poll() noexcept { - auto slot = tmc::detail::result_each_poll(remaining_count, sync_flags); + auto slot = tmc::detail::mux_poll(remaining_count, sync_flags); if (slot < TMC_PLATFORM_BITS) { active_slots &= ~(TMC_ONE_BIT << slot); } From b763b1ddfeaa69561d4f495d04f97e9452b6c05d Mon Sep 17 00:00:00 2001 From: tzcnt Date: Fri, 10 Jul 2026 11:10:59 -0700 Subject: [PATCH 4/4] rename none() -> pending() (matches Rust Future) --- include/tmc/mux_many.hpp | 6 +++--- include/tmc/mux_tuple.hpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/tmc/mux_many.hpp b/include/tmc/mux_many.hpp index 6048a375..cd6a044c 100644 --- a/include/tmc/mux_many.hpp +++ b/include/tmc/mux_many.hpp @@ -333,7 +333,7 @@ class mux_many : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// Non-suspending check for a ready result. There are three outcomes: /// - a result is ready: it is consumed and its index returned (like `co_await`). - /// - results are still pending but none are ready: returns `none()`. + /// - results are still pending but none are ready: returns `pending()`. /// - no submitted results remain: returns `end()`. [[nodiscard]] inline size_t poll() noexcept { auto slot = tmc::detail::mux_poll(remaining_count, sync_flags); @@ -348,9 +348,9 @@ class mux_many : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { inline constexpr size_t end() const noexcept { return TMC_PLATFORM_BITS; } /// Provides a sentinel value returned by `poll()` to indicate that no result is ready - /// right now, but may become ready later. This is distinct from `end()`, which + /// right now, but will become ready later. This is distinct from `end()`, which /// indicates that all submitted results have been consumed. - inline constexpr size_t none() const noexcept { return TMC_PLATFORM_BITS + 1; } + inline constexpr size_t pending() const noexcept { return TMC_PLATFORM_BITS + 1; } /// Returns the capacity of the mux, equal to the `Count` template argument. /// This is the maximum number of awaitables that may be active concurrently. diff --git a/include/tmc/mux_tuple.hpp b/include/tmc/mux_tuple.hpp index 18d6d0ba..fc2d3eba 100644 --- a/include/tmc/mux_tuple.hpp +++ b/include/tmc/mux_tuple.hpp @@ -224,7 +224,7 @@ class mux_tuple : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { /// Non-suspending check for a ready result. There are three outcomes: /// - a result is ready: it is consumed and its index returned (like `co_await`). - /// - results are still pending but none are ready: returns `none()`. + /// - results are still pending but none are ready: returns `pending()`. /// - no submitted results remain: returns `end()`. [[nodiscard]] inline size_t poll() noexcept { auto slot = tmc::detail::mux_poll(remaining_count, sync_flags); @@ -239,9 +239,9 @@ class mux_tuple : private tmc::detail::AwaitTagNoGroupCoAwaitLvalue { inline constexpr size_t end() const noexcept { return TMC_PLATFORM_BITS; } /// Provides a sentinel value returned by `poll()` to indicate that no result is ready - /// right now, but may become ready later. This is distinct from `end()`, which + /// right now, but will become ready later. This is distinct from `end()`, which /// indicates that all submitted results have been consumed. - inline constexpr size_t none() const noexcept { return TMC_PLATFORM_BITS + 1; } + inline constexpr size_t pending() const noexcept { return TMC_PLATFORM_BITS + 1; } /// Returns the capacity of the mux, equal to the number of `Result` template arguments. /// This is the maximum number of awaitables that may be active concurrently.