|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// EMSCRIPTEN does not currently support std::jthread or std::stop_source|token. |
| 4 | +#ifndef EMSCRIPTEN |
| 5 | + |
| 6 | + #include "coro/concepts/awaitable.hpp" |
| 7 | + #include "coro/detail/task_self_deleting.hpp" |
| 8 | + #include "coro/event.hpp" |
| 9 | + #include "coro/mutex.hpp" |
| 10 | + #include "coro/task.hpp" |
| 11 | + |
| 12 | + #include <atomic> |
| 13 | + #include <cassert> |
| 14 | + #include <coroutine> |
| 15 | + #include <stop_token> |
| 16 | + #include <utility> |
| 17 | + #include <vector> |
| 18 | + |
| 19 | +namespace coro |
| 20 | +{ |
| 21 | + |
| 22 | +namespace detail |
| 23 | +{ |
| 24 | + |
| 25 | +template<concepts::awaitable awaitable, typename return_type> |
| 26 | +static auto make_when_any_task( |
| 27 | + awaitable a, |
| 28 | + coro::mutex& m, |
| 29 | + std::atomic<bool>& return_value_set, |
| 30 | + coro::event& notify, |
| 31 | + std::optional<return_type>& return_value) -> coro::task<void> |
| 32 | +{ |
| 33 | + auto result = co_await static_cast<awaitable&&>(a); |
| 34 | + co_await m.lock(); |
| 35 | + // Its important to only touch return_value and notify once since their lifetimes will be destroyed |
| 36 | + // after being set ane notified the first time. |
| 37 | + if (return_value_set.load(std::memory_order::acquire) == false) |
| 38 | + { |
| 39 | + return_value_set.store(true, std::memory_order::release); |
| 40 | + return_value = std::move(result); |
| 41 | + notify.set(); |
| 42 | + } |
| 43 | + |
| 44 | + co_return; |
| 45 | +} |
| 46 | + |
| 47 | +template< |
| 48 | + std::ranges::range range_type, |
| 49 | + concepts::awaitable awaitable_type = std::ranges::range_value_t<range_type>, |
| 50 | + typename return_type = typename concepts::awaitable_traits<awaitable_type>::awaiter_return_type, |
| 51 | + typename return_type_base = std::remove_reference_t<return_type>> |
| 52 | +static auto make_when_any_controller_task( |
| 53 | + range_type awaitables, coro::event& notify, std::optional<return_type_base>& return_value) |
| 54 | + -> coro::detail::task_self_deleting |
| 55 | +{ |
| 56 | + // These must live for as long as the longest running when_any task since each task tries to see |
| 57 | + // if it was the first to complete. Only the very first task to complete will set the return_value |
| 58 | + // and notify. |
| 59 | + coro::mutex m{}; |
| 60 | + std::atomic<bool> return_value_set{false}; |
| 61 | + |
| 62 | + // This detatched task will maintain the lifetime of all the when_any tasks. |
| 63 | + std::vector<coro::task<void>> tasks{}; |
| 64 | + |
| 65 | + if constexpr (std::ranges::sized_range<range_type>) |
| 66 | + { |
| 67 | + tasks.reserve(std::size(awaitables)); |
| 68 | + } |
| 69 | + |
| 70 | + for (auto&& a : awaitables) |
| 71 | + { |
| 72 | + tasks.emplace_back(make_when_any_task<awaitable_type, return_type_base>( |
| 73 | + std::move(a), m, return_value_set, notify, return_value)); |
| 74 | + } |
| 75 | + |
| 76 | + co_await coro::when_all(std::move(tasks)); |
| 77 | + co_return; |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace detail |
| 81 | + |
| 82 | +template< |
| 83 | + std::ranges::range range_type, |
| 84 | + concepts::awaitable awaitable_type = std::ranges::range_value_t<range_type>, |
| 85 | + typename return_type = typename concepts::awaitable_traits<awaitable_type>::awaiter_return_type, |
| 86 | + typename return_type_base = std::remove_reference_t<return_type>> |
| 87 | +[[nodiscard]] auto when_any(std::stop_source stop_source, range_type awaitables) -> coro::task<return_type_base> |
| 88 | +{ |
| 89 | + // Using an std::optional to prevent the need to default construct the type on the stack. |
| 90 | + std::optional<return_type_base> return_value{std::nullopt}; |
| 91 | + coro::event notify{}; |
| 92 | + |
| 93 | + auto controller_task = |
| 94 | + detail::make_when_any_controller_task(std::forward<range_type>(awaitables), notify, return_value); |
| 95 | + controller_task.handle().resume(); |
| 96 | + |
| 97 | + co_await notify; |
| 98 | + stop_source.request_stop(); |
| 99 | + co_return std::move(return_value.value()); |
| 100 | +} |
| 101 | + |
| 102 | +template< |
| 103 | + std::ranges::range range_type, |
| 104 | + concepts::awaitable awaitable_type = std::ranges::range_value_t<range_type>, |
| 105 | + typename return_type = typename concepts::awaitable_traits<awaitable_type>::awaiter_return_type, |
| 106 | + typename return_type_base = std::remove_reference_t<return_type>> |
| 107 | +[[nodiscard]] auto when_any(range_type awaitables) -> coro::task<return_type_base> |
| 108 | +{ |
| 109 | + std::optional<return_type_base> return_value{std::nullopt}; |
| 110 | + coro::event notify{}; |
| 111 | + |
| 112 | + auto controller_task = |
| 113 | + detail::make_when_any_controller_task(std::forward<range_type>(awaitables), notify, return_value); |
| 114 | + controller_task.handle().resume(); |
| 115 | + |
| 116 | + co_await notify; |
| 117 | + co_return std::move(return_value.value()); |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace coro |
| 121 | + |
| 122 | +#endif // EMSCRIPTEN |
0 commit comments