userver: userver/engine/sleep.hpp Source File
Loading...
Searching...
No Matches
sleep.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/sleep.hpp
4/// @brief Time-based coroutine suspension helpers
5
6#include <chrono>
7
8#include <userver/engine/deadline.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace engine {
13
14/// @brief Suspends execution for a brief period of time, possibly allowing
15/// other tasks to execute
16void Yield();
17
18/// @cond
19/// Recursion stoppers/specializations
20void InterruptibleSleepUntil(Deadline);
21void SleepUntil(Deadline);
22/// @endcond
23
24/// Suspends execution for at least the specified amount of time or until the
25/// current task is cancelled. If already cancelled, the call is equivalent to
26/// Yield.
27template <typename Rep, typename Period>
28void InterruptibleSleepFor(const std::chrono::duration<Rep, Period>& duration) {
29 engine::InterruptibleSleepUntil(Deadline::FromDuration(duration));
30}
31
32/// Suspends execution until the specified time point is reached or until the
33/// current task is cancelled. If already cancelled, the call is equivalent to
34/// Yield.
35template <typename Clock, typename Duration>
37 const std::chrono::time_point<Clock, Duration>& time_point) {
38 engine::InterruptibleSleepUntil(Deadline::FromTimePoint(time_point));
39}
40
41/// Suspends execution for at least a specified amount of time, ignores
42/// cancellation. Use engine::InterruptibleSleepFor to interrupt sleep by
43/// cancellation request.
44template <typename Rep, typename Period>
45void SleepFor(const std::chrono::duration<Rep, Period>& duration) {
46 engine::SleepUntil(Deadline::FromDuration(duration));
47}
48
49/// Suspends execution until the specified time point is reached, ignores
50/// cancellation. Use engine::InterruptibleSleepUntil to interrupt sleep by
51/// cancellation request.
52template <typename Clock, typename Duration>
53void SleepUntil(const std::chrono::time_point<Clock, Duration>& time_point) {
54 engine::SleepUntil(Deadline::FromTimePoint(time_point));
55}
56
57} // namespace engine
58
59USERVER_NAMESPACE_END