forked from DataDog/dd-trace-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreaded_event_scheduler.h
64 lines (50 loc) · 1.63 KB
/
threaded_event_scheduler.h
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
#pragma once
// This component provides a `class`, `ThreadedEventScheduler`, that implements
// the `EventScheduler` interface in terms of a dedicated event dispatching
// thread. It is the default implementation used if
// `DatadogAgent::event_scheduler` is not specified.
#include <datadog/event_scheduler.h>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <queue>
#include <thread>
#include <vector>
namespace datadog {
namespace tracing {
class ThreadedEventScheduler : public EventScheduler {
struct EventConfig {
std::function<void()> callback;
std::chrono::steady_clock::duration interval;
bool cancelled;
EventConfig(std::function<void()> callback,
std::chrono::steady_clock::duration interval);
};
struct ScheduledRun {
std::chrono::steady_clock::time_point when;
std::shared_ptr<const EventConfig> config;
};
struct GreaterThan {
bool operator()(const ScheduledRun&, const ScheduledRun&) const;
};
std::mutex mutex_;
ScheduledRun current_;
std::condition_variable schedule_or_shutdown_;
bool running_current_;
std::condition_variable current_done_;
std::priority_queue<ScheduledRun, std::vector<ScheduledRun>, GreaterThan>
upcoming_;
bool shutting_down_;
std::thread dispatcher_;
void run();
public:
ThreadedEventScheduler();
~ThreadedEventScheduler();
Cancel schedule_recurring_event(std::chrono::steady_clock::duration interval,
std::function<void()> callback) override;
std::string config() const override;
};
} // namespace tracing
} // namespace datadog