YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
wait_for.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <yaclib/config.hpp>
7
8#include <chrono>
9#include <cstddef>
10
11namespace yaclib {
12
13/**
14 * Wait until the specified timeout duration has elapsed or \ref Ready becomes true
15 *
16 * The behavior is undefined if \ref Valid is false before the call to this function.
17 * This function may block for longer than timeout_duration due to scheduling or resource contention delays.
18 * \param timeout_duration maximum duration to block for
19 * \param fs futures to wait
20 * \return The result of \ref Ready upon exiting
21 */
22template <typename Event = detail::MutexEvent, typename Rep, typename Period, typename... V, typename... E>
23YACLIB_INLINE bool WaitFor(const std::chrono::duration<Rep, Period>& timeout_duration,
24 FutureBase<V, E>&... fs) noexcept {
25 YACLIB_ASSERT(... && fs.Valid());
26 return detail::WaitCore<Event>(timeout_duration, UpCast<detail::BaseCore>(*fs.GetCore())...);
27}
28
29/**
30 * Wait until the specified timeout duration has elapsed or \ref Ready becomes true
31 *
32 * The behavior is undefined if \ref Valid is false before the call to this function.
33 * This function may block for longer than timeout_duration due to scheduling or resource contention delays.
34 * \param timeout_duration maximum duration to block for
35 * \param begin iterator to futures to wait
36 * \param end iterator to futures to wait
37 * \return The result of \ref Ready upon exiting
38 */
39template <typename Event = detail::MutexEvent, typename Rep, typename Period, typename Iterator>
40YACLIB_INLINE std::enable_if_t<!is_future_base_v<Iterator>, bool> WaitFor(
41 const std::chrono::duration<Rep, Period>& timeout_duration, Iterator begin, Iterator end) noexcept {
42 // We don't use std::distance because we want to alert the user to the fact that it can be expensive.
43 // Maybe the user has the size of the range, otherwise it is suggested to call Wait*(..., begin, distance(begin, end))
44 return detail::WaitIterator<Event>(timeout_duration, begin, static_cast<std::size_t>(end - begin));
45}
46
47/**
48 * Wait until the specified timeout duration has elapsed or \ref Ready becomes true
49 *
50 * The behavior is undefined if \ref Valid is false before the call to this function.
51 * This function may block for longer than timeout_duration due to scheduling or resource contention delays.
52 * \param timeout_duration maximum duration to block for
53 * \param begin iterator to futures to wait
54 * \param count of futures to wait
55 * \return The result of \ref Ready upon exiting
56 */
57template <typename Event = detail::MutexEvent, typename Rep, typename Period, typename Iterator>
58YACLIB_INLINE bool WaitFor(const std::chrono::duration<Rep, Period>& timeout_duration, Iterator begin,
59 std::size_t count) noexcept {
60 return detail::WaitIterator<Event>(timeout_duration, begin, count);
61}
62
63} // namespace yaclib
Provides a mechanism to access the result of async operations.
Definition future.hpp:20
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
YACLIB_INLINE bool WaitFor(const std::chrono::duration< Rep, Period > &timeout_duration, FutureBase< V, E > &... fs) noexcept
Wait until the specified timeout duration has elapsed or Ready becomes true.
Definition wait_for.hpp:23
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25