Commit 080fe51c authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[base] Migrate MessagePumpLibEvent to DoSomeWork

This is a simple one and gets rid of a redundant call to
TimeTicks::Now() thanks to the new API :)

Bug: 885371
Change-Id: I3dccde418849992ad0bce18297c79eea6f98f072
Reviewed-on: https://chromium-review.googlesource.com/c/1485125
Commit-Queue: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarkylechar <kylechar@chromium.org>
Auto-Submit: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636910}
parent ea960352
......@@ -205,36 +205,33 @@ void MessagePumpLibevent::Run(Delegate* delegate) {
mac::ScopedNSAutoreleasePool autorelease_pool;
#endif
bool did_work = delegate->DoWork();
Delegate::NextWorkInfo next_work_info = delegate->DoSomeWork();
bool more_work_is_plausible = next_work_info.is_immediate();
if (!keep_running_)
break;
event_base_loop(event_base_, EVLOOP_NONBLOCK);
did_work |= processed_io_events_;
more_work_is_plausible |= processed_io_events_;
processed_io_events_ = false;
if (!keep_running_)
break;
did_work |= delegate->DoDelayedWork(&delayed_work_time_);
if (!keep_running_)
break;
if (did_work)
if (more_work_is_plausible)
continue;
did_work = delegate->DoIdleWork();
more_work_is_plausible = delegate->DoIdleWork();
if (!keep_running_)
break;
if (did_work)
if (more_work_is_plausible)
continue;
// EVLOOP_ONCE tells libevent to only block once,
// but to service all pending events when it wakes up.
if (delayed_work_time_.is_null()) {
if (next_work_info.delayed_run_time.is_max()) {
event_base_loop(event_base_, EVLOOP_ONCE);
} else {
TimeDelta delay = delayed_work_time_ - TimeTicks::Now();
const TimeDelta delay = next_work_info.remaining_delay();
if (delay > TimeDelta()) {
struct timeval poll_tv;
poll_tv.tv_sec = delay.InSeconds();
......@@ -244,10 +241,6 @@ void MessagePumpLibevent::Run(Delegate* delegate) {
event_add(timer_event.get(), &poll_tv);
event_base_loop(event_base_, EVLOOP_ONCE);
event_del(timer_event.get());
} else {
// It looks like delayed_work_time_ indicates a time in the past, so we
// need to call DoDelayedWork now.
delayed_work_time_ = TimeTicks();
}
}
......@@ -272,10 +265,10 @@ void MessagePumpLibevent::ScheduleWork() {
void MessagePumpLibevent::ScheduleDelayedWork(
const TimeTicks& delayed_work_time) {
// We know that we can't be blocked on Wait right now since this method can
// only be called on the same thread as Run, so we only need to update our
// record of how long to sleep when we do sleep.
delayed_work_time_ = delayed_work_time;
// We know that we can't be blocked on Run()'s |timer_event| right now since
// this method can only be called on the same thread as Run(). Hence we have
// nothing to do here, this thread will sleep in Run() with the correct
// timeout when it's out of immediate tasks.
}
bool MessagePumpLibevent::Init() {
......
......@@ -12,7 +12,6 @@
#include "base/message_loop/message_pump.h"
#include "base/message_loop/watchable_io_message_pump_posix.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
// Declare structs we need from libevent.h rather than including it
struct event_base;
......@@ -100,9 +99,6 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump,
// This flag is set if libevent has processed I/O events.
bool processed_io_events_;
// The time at which we should call DoDelayedWork.
TimeTicks delayed_work_time_;
// Libevent dispatcher. Watches all sockets registered with it, and sends
// readiness callbacks when a socket is ready for I/O.
event_base* event_base_;
......
......@@ -4,13 +4,21 @@
#include "base/message_loop/message_pump.h"
#include <type_traits>
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_pump_for_io.h"
#include "base/message_loop/message_pump_for_ui.h"
#include "base/test/bind_test_util.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX) && !defined(OS_NACL_SFI)
#include "base/message_loop/message_pump_libevent.h"
#endif
using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Invoke;
......@@ -38,6 +46,10 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) {
return true;
#elif defined(OS_WIN)
return true;
#elif defined(OS_POSIX) && !defined(OS_NACL_SFI)
// MessagePumpLibevent was migrated (ref. message_pump_for_ui.h and
// |use_libevent| in base/BUILD.gn for enabled conditions).
return std::is_same<MessagePumpForUI, MessagePumpLibevent>::value;
#else
// TODO(gab): Complete migration of all UI pumps to DoSomeWork() as part
// of crbug.com/885371.
......@@ -47,6 +59,10 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) {
case MessageLoopBase::Type::TYPE_IO:
#if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS))
return true;
#elif defined(OS_POSIX) && !defined(OS_NACL_SFI)
// MessagePumpLibevent was migrated (ref. message_pump_for_io.h and
// |use_libevent| in base/BUILD.gn for enabled conditions).
return std::is_same<MessagePumpForIO, MessagePumpLibevent>::value;
#else
// TODO(gab): Complete migration of all IO pumps to DoSomeWork() as part
// of crbug.com/885371.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment