/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 | 277k | : cb_(cb), dispatcher_(dispatcher) { |
14 | 277k | ASSERT(cb_); |
15 | 277k | evtimer_assign( |
16 | 277k | &raw_event_, libevent.get(), |
17 | 277k | [](evutil_socket_t, short, void* arg) -> void { |
18 | 277k | TimerImpl* timer = static_cast<TimerImpl*>(arg); |
19 | 277k | if (timer->object_ == nullptr) { |
20 | 277k | timer->cb_(); |
21 | 277k | return; |
22 | 277k | } |
23 | 277k | ScopeTrackerScopeState scope(timer->object_, timer->dispatcher_); |
24 | 277k | timer->object_ = nullptr; |
25 | 277k | timer->cb_(); |
26 | 277k | }, |
27 | 277k | this); |
28 | 277k | } |
29 | | |
30 | 159k | void TimerImpl::disableTimer() { |
31 | 159k | ASSERT(dispatcher_.isThreadSafe()); |
32 | 159k | event_del(&raw_event_); |
33 | 159k | } |
34 | | |
35 | 77.1k | void TimerImpl::enableTimer(const std::chrono::milliseconds d, const ScopeTrackedObject* object) { |
36 | 77.1k | timeval tv; |
37 | 77.1k | TimerUtils::durationToTimeval(d, tv); |
38 | 77.1k | internalEnableTimer(tv, object); |
39 | 77.1k | } |
40 | | |
41 | | void TimerImpl::enableHRTimer(const std::chrono::microseconds d, |
42 | 151k | const ScopeTrackedObject* object = nullptr) { |
43 | 151k | timeval tv; |
44 | 151k | TimerUtils::durationToTimeval(d, tv); |
45 | 151k | internalEnableTimer(tv, object); |
46 | 151k | } |
47 | | |
48 | 228k | void TimerImpl::internalEnableTimer(const timeval& tv, const ScopeTrackedObject* object) { |
49 | 228k | ASSERT(dispatcher_.isThreadSafe()); |
50 | 228k | object_ = object; |
51 | | |
52 | 228k | event_add(&raw_event_, &tv); |
53 | 228k | } |
54 | | |
55 | 268 | bool TimerImpl::enabled() { |
56 | 268 | ASSERT(dispatcher_.isThreadSafe()); |
57 | 268 | return 0 != evtimer_pending(&raw_event_, nullptr); |
58 | 268 | } |
59 | | |
60 | | } // namespace Event |
61 | | } // namespace Envoy |