userver: userver/engine/sleep.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 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 InterruptibleSleepUntil(Deadline::FromTimePoint(time_point));
39}
40
41/// Suspends execution for at least a specified amount of time
42template <typename Rep, typename Period>
43void SleepFor(const std::chrono::duration<Rep, Period>& duration) {
44 SleepUntil(Deadline::FromDuration(duration));
45}
46
47/// Suspends execution until the specified time point is reached
48template <typename Clock, typename Duration>
49void SleepUntil(const std::chrono::time_point<Clock, Duration>& time_point) {
50 SleepUntil(Deadline::FromTimePoint(time_point));
51}
52
53} // namespace engine
54
55USERVER_NAMESPACE_END