Repository navigation
Expand file tree
/
Copy pathDelayedNotifier.h
More file actions
135 lines (119 loc) · 3.85 KB
/
Copy pathDelayedNotifier.h
File metadata and controls
135 lines (119 loc) · 3.85 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2024-2026 Board of Regents of the University of Wisconsin System
// SPDX-License-Identifier: BSD-2-Clause
#pragma once
#include <cassert>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
// DelayedNotifier calls scheduled callables after a delay relative to when
// they are scheduled. The delay is fixed at the time of scheduling, and
// actions are always called on a background thread, except when the delay is
// zero, in which case the action is called on the scheduling thread.
// Member functions are thread-safe.
class DelayedNotifier {
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;
struct Item {
TimePoint when;
std::function<void()> what;
Item(TimePoint timeout, std::function<void()> action)
: when(timeout), what(action) {}
};
// Priority queue with soonest item at top
struct LaterItem {
bool operator()(const Item &lhs, const Item &rhs) const {
return lhs.when > rhs.when;
}
};
using PrioQ = std::priority_queue<Item, std::vector<Item>, LaterItem>;
mutable std::mutex mut_;
std::condition_variable cv_;
std::chrono::microseconds delay_{};
PrioQ scheduledItems_;
bool stopRequested_ = false;
std::thread notifyThread_;
std::once_flag threadStarted_;
TimePoint NextTimeout() const {
if (scheduledItems_.empty()) {
return TimePoint::max();
}
return scheduledItems_.top().when;
}
void IssueNotifications() {
std::unique_lock<std::mutex> lock(mut_);
for (;;) {
const auto timeout = NextTimeout();
cv_.wait_until(lock, timeout, [&] {
return NextTimeout() < timeout || stopRequested_;
});
if (stopRequested_) {
return;
}
const auto now = Clock::now();
while (NextTimeout() <= now) {
std::function<void()> func = scheduledItems_.top().what;
scheduledItems_.pop();
lock.unlock();
func();
lock.lock();
}
}
}
public:
~DelayedNotifier() {
if (notifyThread_.joinable()) {
{
std::lock_guard<std::mutex> lock(mut_);
stopRequested_ = true;
}
cv_.notify_one();
notifyThread_.join();
}
}
std::chrono::microseconds Delay() const {
std::lock_guard<std::mutex> lock(mut_);
return delay_;
}
// Set the delay; applies to actions scheduled in the future only.
void Delay(std::chrono::microseconds delay) {
assert(delay.count() >= 0);
std::lock_guard<std::mutex> lock(mut_);
delay_ = delay;
}
void Schedule(std::function<void()> action) {
assert(action);
bool callSync{};
bool needToInterruptWait{};
{
std::lock_guard<std::mutex> lock(mut_);
if (delay_.count() == 0) {
callSync = true;
} else {
const auto prevTimeout = NextTimeout();
const auto timeout = Clock::now() + delay_;
scheduledItems_.emplace(timeout, action);
needToInterruptWait = timeout < prevTimeout;
}
}
if (callSync) {
action();
} else {
std::call_once(threadStarted_, [this] { // Lazy-start thread
notifyThread_ = std::thread([this] { IssueNotifications(); });
});
}
if (needToInterruptWait) {
cv_.notify_one();
}
}
void CancelAll() {
std::lock_guard<std::mutex> lock(mut_);
while (!scheduledItems_.empty()) {
scheduledItems_.pop();
}
}
};