YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
await.hpp
Go to the documentation of this file.
1#pragma once
2
7
8namespace yaclib {
9
10template <typename V, typename E>
15
16template <typename Waited, typename = std::enable_if_t<is_waitable_v<Waited>>>
18 YACLIB_ASSERT(waited.Valid());
19 return detail::AwaitAwaiter{waited.GetHandle()};
20}
21
22template <typename... Waited, typename = std::enable_if_t<(... && is_waitable_v<Waited>)>>
23YACLIB_INLINE auto Await(Waited&... waited) noexcept {
24 using namespace detail;
25 static constexpr auto kSharedCount = Count<SharedHandle, typename Waited::Handle...>;
26 using Awaiter = std::conditional_t<kSharedCount == 0, MultiAwaitAwaiter<AwaitEvent>,
28 YACLIB_ASSERT(... && waited.Valid());
29 return Awaiter{waited.GetHandle()...};
30}
31
33 typename = std::enable_if_t<is_waitable_v<Value>>>
34YACLIB_INLINE auto Await(Iterator begin, std::size_t count) noexcept {
35 using namespace detail;
36 static constexpr auto kShared = std::is_same_v<typename Value::Handle, SharedHandle>;
37 using Awaiter =
38 std::conditional_t<kShared, MultiAwaitAwaiter<DynamicSharedEvent<AwaitEvent>>, MultiAwaitAwaiter<AwaitEvent>>;
39 return Awaiter{begin, count};
40}
41
42template <typename Iterator,
43 typename = std::enable_if_t<is_waitable_v<typename std::iterator_traits<Iterator>::value_type>>>
45 // We don't use std::distance because we want to alert the user to the fact that it can be expensive.
46 // Maybe the user has the size of the range, otherwise it is suggested to call Await(begin, distance(begin, end))
47 return Await(begin, static_cast<std::size_t>(end - begin));
48}
49
50template <typename V, typename E>
51YACLIB_INLINE auto operator co_await(FutureBase<V, E>&& future) noexcept {
52 YACLIB_ASSERT(future.Valid());
53 return detail::AwaitSingleAwaiter<false, V, E>{std::move(future.GetCore())};
54}
55
56template <typename V, typename E>
57YACLIB_INLINE auto operator co_await(const SharedFutureBase<V, E>& future) noexcept {
58 YACLIB_ASSERT(future.Valid());
60}
61
62template <typename V, typename E>
63YACLIB_INLINE auto operator co_await(Task<V, E>&& task) noexcept {
64 YACLIB_ASSERT(task.Valid());
65 return detail::TransferSingleAwaiter{std::move(task.GetCore())};
66}
67
68} // namespace yaclib
Provides a mechanism to access the result of async operations.
Definition future.hpp:20
Provides a mechanism to schedule the some async operations TODO(MBkkt) add description.
Definition task.hpp:25
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
constexpr auto Count
YACLIB_INLINE auto Await(Task< V, E > &task) noexcept
Definition await.hpp:11
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25