/proc/self/cwd/source/common/event/timer_impl.cc
Line | Count | Source |
1 | | #include "source/common/event/timer_impl.h" |
2 | | |
3 | | #include <chrono> |
4 | | |
5 | | #include "source/common/common/assert.h" |
6 | | |
7 | | #include "event2/event.h" |
8 | | |
9 | | namespace Envoy { |
10 | | namespace Event { |
11 | | |
12 | | TimerImpl::TimerImpl(Libevent::BasePtr& libevent, TimerCb cb, Dispatcher& dispatcher) |
13 | 39.3k | : cb_(cb), dispatcher_(dispatcher) { |
14 | 39.3k | ASSERT(cb_); |
15 | 39.3k | evtimer_assign( |
16 | 39.3k | &raw_event_, libevent.get(), |
17 | 39.3k | [](evutil_socket_t, short, void* arg) -> void { |
18 | 39.3k | TimerImpl* timer = static_cast<TimerImpl*>(arg); |
19 | 39.3k | if (timer->object_ == nullptr) { |
20 | 39.3k | timer->cb_(); |
21 | 39.3k | return; |
22 | 39.3k | } |
23 | 39.3k | ScopeTrackerScopeState scope(timer->object_, timer->dispatcher_); |
24 | 39.3k | timer->object_ = nullptr; |
25 | 39.3k | timer->cb_(); |
26 | 39.3k | }, |
27 | 39.3k | this); |
28 | 39.3k | } |
29 | | |
30 | 24.0k | void TimerImpl::disableTimer() { |
31 | 24.0k | ASSERT(dispatcher_.isThreadSafe()); |
32 | 24.0k | event_del(&raw_event_); |
33 | 24.0k | } |
34 | | |
35 | 66.4k | void TimerImpl::enableTimer(const std::chrono::milliseconds d, const ScopeTrackedObject* object) { |
36 | 66.4k | timeval tv; |
37 | 66.4k | TimerUtils::durationToTimeval(d, tv); |
38 | 66.4k | internalEnableTimer(tv, object); |
39 | 66.4k | } |
40 | | |
41 | | void TimerImpl::enableHRTimer(const std::chrono::microseconds d, |
42 | 2.29k | const ScopeTrackedObject* object = nullptr) { |
43 | 2.29k | timeval tv; |
44 | 2.29k | TimerUtils::durationToTimeval(d, tv); |
45 | 2.29k | internalEnableTimer(tv, object); |
46 | 2.29k | } |
47 | | |
48 | 68.6k | void TimerImpl::internalEnableTimer(const timeval& tv, const ScopeTrackedObject* object) { |
49 | 68.6k | ASSERT(dispatcher_.isThreadSafe()); |
50 | 68.6k | object_ = object; |
51 | | |
52 | 68.6k | event_add(&raw_event_, &tv); |
53 | 68.6k | } |
54 | | |
55 | 353 | bool TimerImpl::enabled() { |
56 | 353 | ASSERT(dispatcher_.isThreadSafe()); |
57 | 353 | return 0 != evtimer_pending(&raw_event_, nullptr); |
58 | 353 | } |
59 | | |
60 | | } // namespace Event |
61 | | } // namespace Envoy |