forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.cpp
61 lines (47 loc) · 1.32 KB
/
events.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <torch/csrc/monitor/events.h>
#include <algorithm>
#include <mutex>
#include <sstream>
#include <unordered_set>
#include <vector>
namespace torch {
namespace monitor {
namespace {
class EventHandlers {
public:
void registerEventHandler(std::shared_ptr<EventHandler> handler) noexcept {
std::unique_lock<std::mutex> lock(mu_);
handlers_.emplace_back(std::move(handler));
}
void unregisterEventHandler(
const std::shared_ptr<EventHandler>& handler) noexcept {
std::unique_lock<std::mutex> lock(mu_);
auto it = std::find(handlers_.begin(), handlers_.end(), handler);
handlers_.erase(it);
}
void logEvent(const Event& e) {
std::unique_lock<std::mutex> lock(mu_);
for (auto& handler : handlers_) {
handler->handle(e);
}
}
static EventHandlers& get() noexcept {
static EventHandlers ehs;
return ehs;
}
private:
std::mutex mu_{};
std::vector<std::shared_ptr<EventHandler>> handlers_{};
};
} // namespace
void logEvent(const Event& e) {
EventHandlers::get().logEvent(e);
}
void registerEventHandler(std::shared_ptr<EventHandler> p) {
EventHandlers::get().registerEventHandler(std::move(p));
}
void unregisterEventHandler(const std::shared_ptr<EventHandler>& p) {
EventHandlers::get().unregisterEventHandler(p);
}
} // namespace monitor
} // namespace torch