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