-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathsleep.cpp
43 lines (31 loc) · 1.17 KB
/
sleep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <userver/engine/sleep.hpp>
#include <userver/engine/task/cancel.hpp>
#include <engine/task/task_context.hpp>
#include <userver/utils/fast_scope_guard.hpp>
USERVER_NAMESPACE_BEGIN
namespace engine {
namespace impl {
namespace {
class CommonSleepWaitStrategy final : public WaitStrategy {
public:
CommonSleepWaitStrategy() = default;
EarlyWakeup SetupWakeups() override { return EarlyWakeup{false}; }
void DisableWakeups() noexcept override {}
};
} // namespace
} // namespace impl
void InterruptibleSleepUntil(Deadline deadline) {
auto& current = current_task::GetCurrentTaskContext();
const utils::FastScopeGuard reset_background([¤t, previous_background_flag = current.IsBackground()](
) noexcept { current.SetBackground(previous_background_flag); });
current.SetBackground(true);
impl::CommonSleepWaitStrategy wait_manager{};
current.Sleep(wait_manager, deadline);
}
void SleepUntil(Deadline deadline) {
TaskCancellationBlocker block_cancel;
InterruptibleSleepUntil(deadline);
}
void Yield() { SleepUntil(Deadline::Passed()); }
} // namespace engine
USERVER_NAMESPACE_END