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... Waited>
24 const std::chrono::duration<Rep, Period>& timeout_duration, Waited&... fs) noexcept {
25 YACLIB_ASSERT(... && fs.Valid());
26 return detail::WaitCore<Event>(timeout_duration, fs.GetHandle()...);
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 It, typename Sentinel,
40 typename = std::enable_if_t<is_input_range_pair_v<It, Sentinel> &&
42YACLIB_INLINE bool WaitFor(const std::chrono::duration<Rep, Period>& timeout_duration, It begin,
43 Sentinel end) noexcept {
44 static_assert(
46 "Use WaitFor(timeout, begin, std::distance(begin, end)) instead"); // We don't use std::distance because we want to
47 // alert the user to the fact that it can be
48 // expensive.
49
50 return WaitFor<Event, Rep, Period>(timeout_duration, begin, static_cast<std::size_t>(end - begin));
51}
52
53/**
54 * Wait until the specified timeout duration has elapsed or \ref Ready becomes true
55 *
56 * The behavior is undefined if \ref Valid is false before the call to this function.
57 * This function may block for longer than timeout_duration due to scheduling or resource contention delays.
58 * \param timeout_duration maximum duration to block for
59 * \param range of futures to wait
60 * \return The result of \ref Ready upon exiting
61 */
62template <typename Event = detail::MutexEvent, typename Rep, typename Period, typename Range,
63 typename = std::enable_if_t<
66YACLIB_INLINE bool WaitFor(const std::chrono::duration<Rep, Period>& timeout_duration, Range&& range) noexcept {
67 return WaitFor<Event, Rep, Period>(timeout_duration, std::begin(range), std::end(range));
68}
69
70/**
71 * Wait until the specified timeout duration has elapsed or \ref Ready becomes true
72 *
73 * The behavior is undefined if \ref Valid is false before the call to this function.
74 * This function may block for longer than timeout_duration due to scheduling or resource contention delays.
75 * \param timeout_duration maximum duration to block for
76 * \param begin iterator to futures to wait
77 * \param count of futures to wait
78 * \return The result of \ref Ready upon exiting
79 */
80template <typename Event = detail::MutexEvent, typename Rep, typename Period, typename It,
81 typename = std::enable_if_t<is_input_iterator_v<It> &&
83YACLIB_INLINE bool WaitFor(const std::chrono::duration<Rep, Period>& timeout_duration, It begin,
84 std::size_t count) noexcept {
85 return detail::WaitIterator<Event>(timeout_duration, begin, count);
86}
87
88} // namespace yaclib
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
Contract< V, T > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
YACLIB_INLINE std::enable_if_t<(... &&is_waitable_with_timeout_v< Waited >), bool > WaitFor(const std::chrono::duration< Rep, Period > &timeout_duration, Waited &... fs) noexcept
Wait until the specified timeout duration has elapsed or Ready becomes true.
Definition wait_for.hpp:23