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>
36void InterruptibleSleepUntil(const std::chrono::time_point<Clock, Duration>& time_point) {
37 engine::InterruptibleSleepUntil(Deadline::FromTimePoint(time_point));
38}
39
40/// Suspends execution for at least a specified amount of time, ignores
41/// cancellation. Use engine::InterruptibleSleepFor to interrupt sleep by
42/// cancellation request.
43template <typename Rep, typename Period>
44void SleepFor(const std::chrono::duration<Rep, Period>& duration) {
45 engine::SleepUntil(Deadline::FromDuration(duration));
46}
47
48/// Suspends execution until the specified time point is reached, ignores
49/// cancellation. Use engine::InterruptibleSleepUntil to interrupt sleep by
50/// cancellation request.
51template <typename Clock, typename Duration>
52void SleepUntil(const std::chrono::time_point<Clock, Duration>& time_point) {
53 engine::SleepUntil(Deadline::FromTimePoint(time_point));
54}
55
56} // namespace engine
57
58USERVER_NAMESPACE_END