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