From 0b31f416fa05a8dfee95b9e29d518027fa568235 Mon Sep 17 00:00:00 2001 From: Changcheng Liu Date: Wed, 31 Jul 2019 16:54:26 +0800 Subject: [PATCH] msg/async/Event: simplify EventCenter::process_events implementation The original implementation makes it's hard to understand: 1) Whether timer event should be executed. 2) How long should epoll wait for timeout. Signed-off-by: Changcheng Liu --- src/msg/async/Event.cc | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/msg/async/Event.cc b/src/msg/async/Event.cc index 6b5e4c7c3d8..145bc5c4006 100644 --- a/src/msg/async/Event.cc +++ b/src/msg/async/Event.cc @@ -367,35 +367,26 @@ int EventCenter::process_events(unsigned timeout_microseconds, ceph::timespan * int numevents; bool trigger_time = false; auto now = clock_type::now(); + clock_type::time_point end_time = now + std::chrono::microseconds(timeout_microseconds); auto it = time_events.begin(); - bool blocking = pollers.empty() && !external_num_events.load(); - // If exists external events or poller, don't block - if (!blocking) { - if (it != time_events.end() && now >= it->first) - trigger_time = true; - tv.tv_sec = 0; - tv.tv_usec = 0; - } else { - clock_type::time_point shortest; - shortest = now + std::chrono::microseconds(timeout_microseconds); - - if (it != time_events.end() && shortest >= it->first) { - ldout(cct, 30) << __func__ << " shortest is " << shortest << " it->first is " << it->first << dendl; - shortest = it->first; - trigger_time = true; - if (shortest > now) { - timeout_microseconds = std::chrono::duration_cast( - shortest - now).count(); - } else { - shortest = now; - timeout_microseconds = 0; - } + if (it != time_events.end() && end_time >= it->first) { + trigger_time = true; + end_time = it->first; + + if (end_time > now) { + timeout_microseconds = std::chrono::duration_cast(end_time - now).count(); + } else { + timeout_microseconds = 0; } - tv.tv_sec = timeout_microseconds / 1000000; - tv.tv_usec = timeout_microseconds % 1000000; } + bool blocking = pollers.empty() && !external_num_events.load(); + if (!blocking) + timeout_microseconds = 0; + tv.tv_sec = timeout_microseconds / 1000000; + tv.tv_usec = timeout_microseconds % 1000000; + ldout(cct, 30) << __func__ << " wait second " << tv.tv_sec << " usec " << tv.tv_usec << dendl; vector fired_events; numevents = driver->event_wait(fired_events, &tv); -- 2.39.5