Line data Source code
1 : #include "source/common/event/schedulable_cb_impl.h" 2 : 3 : #include "source/common/common/assert.h" 4 : 5 : #include "event2/event.h" 6 : 7 : namespace Envoy { 8 : namespace Event { 9 : 10 : SchedulableCallbackImpl::SchedulableCallbackImpl(Libevent::BasePtr& libevent, 11 : std::function<void()> cb) 12 8329 : : cb_(cb) { 13 8329 : ASSERT(cb_); 14 8329 : evtimer_assign( 15 8329 : &raw_event_, libevent.get(), 16 8329 : [](evutil_socket_t, short, void* arg) -> void { 17 8329 : SchedulableCallbackImpl* cb = static_cast<SchedulableCallbackImpl*>(arg); 18 8329 : cb->cb_(); 19 8329 : }, 20 8329 : this); 21 8329 : } 22 : 23 4045 : void SchedulableCallbackImpl::scheduleCallbackCurrentIteration() { 24 4045 : if (enabled()) { 25 98 : return; 26 98 : } 27 : // event_active directly adds the event to the end of the work queue so it executes in the current 28 : // iteration of the event loop. 29 3947 : event_active(&raw_event_, EV_TIMEOUT, 0); 30 3947 : } 31 : 32 4238 : void SchedulableCallbackImpl::scheduleCallbackNextIteration() { 33 4238 : if (enabled()) { 34 0 : return; 35 0 : } 36 : // libevent computes the list of timers to move to the work list after polling for fd events, but 37 : // iteration through the work list starts. Zero delay timers added while iterating through the 38 : // work list execute on the next iteration of the event loop. 39 4238 : const timeval zero_tv{}; 40 4238 : event_add(&raw_event_, &zero_tv); 41 4238 : } 42 : 43 3831 : void SchedulableCallbackImpl::cancel() { event_del(&raw_event_); } 44 : 45 8327 : bool SchedulableCallbackImpl::enabled() { return 0 != evtimer_pending(&raw_event_, nullptr); } 46 : 47 : } // namespace Event 48 : } // namespace Envoy