Commit 14712af9 authored by Carlos Caballero's avatar Carlos Caballero Committed by Commit Bot

Reland "Remove MessageLoopBase"

This is a reland of 58017e6c with a fix
for base_unittests failing on chromium.memory/win-asan
(https://crbug.com/953465)

Original change's description:
> Remove MessageLoopBase
>
> Bug: 891670
> Change-Id: Iff6a2b4685f455509553779c7c04afd3c6f6c4e6
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1538289
> Commit-Queue: Carlos Caballero <carlscab@google.com>
> Reviewed-by: Gabriel Charette <gab@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#651343}

Bug: 891670
Change-Id: I5271b84caaa204093203284342fe0289843844d5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1570030
Commit-Queue: Carlos Caballero <carlscab@google.com>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#651747}
parent 1929b688
...@@ -56,20 +56,10 @@ MessageLoop::MessageLoop(std::unique_ptr<MessagePump> pump) ...@@ -56,20 +56,10 @@ MessageLoop::MessageLoop(std::unique_ptr<MessagePump> pump)
MessageLoop::~MessageLoop() { MessageLoop::~MessageLoop() {
// Clean up any unprocessed tasks, but take care: deleting a task could // Clean up any unprocessed tasks, but take care: deleting a task could
// result in the addition of more tasks (e.g., via DeleteSoon). We set a // result in the addition of more tasks (e.g., via DeleteSoon). This is taken
// limit on the number of times we will allow a deleted task to generate more // care by the queue as it will prevent further tasks from being posted to its
// tasks. Normally, we should only pass through this loop once or twice. If // associated TaskRunner instances.
// we end up hitting the loop limit, then it is probably due to one task that default_task_queue_->ShutdownTaskQueue();
// is being stubborn. Inspect the queues to see who is left.
bool tasks_remain;
for (int i = 0; i < 100; ++i) {
backend_->DeletePendingTasks();
// If we end up with empty queues, then break out of the loop.
tasks_remain = backend_->HasTasks();
if (!tasks_remain)
break;
}
DCHECK(!tasks_remain);
// If |pump_| is non-null, this message loop has been bound and should be the // If |pump_| is non-null, this message loop has been bound and should be the
// current one on this thread. Otherwise, this loop is being destructed before // current one on this thread. Otherwise, this loop is being destructed before
...@@ -141,24 +131,20 @@ bool MessageLoop::IsType(Type type) const { ...@@ -141,24 +131,20 @@ bool MessageLoop::IsType(Type type) const {
// implementation detail. http://crbug.com/703346 // implementation detail. http://crbug.com/703346
void MessageLoop::AddTaskObserver(TaskObserver* task_observer) { void MessageLoop::AddTaskObserver(TaskObserver* task_observer) {
DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
backend_->AddTaskObserver(task_observer); sequence_manager_->AddTaskObserver(task_observer);
} }
void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) { void MessageLoop::RemoveTaskObserver(TaskObserver* task_observer) {
DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_);
backend_->RemoveTaskObserver(task_observer); sequence_manager_->RemoveTaskObserver(task_observer);
} }
bool MessageLoop::IsBoundToCurrentThread() const { bool MessageLoop::IsBoundToCurrentThread() const {
return backend_->IsBoundToCurrentThread(); return sequence_manager_->IsBoundToCurrentThread();
} }
bool MessageLoop::IsIdleForTesting() { bool MessageLoop::IsIdleForTesting() {
return backend_->IsIdleForTesting(); return sequence_manager_->IsIdleForTesting();
}
MessageLoopBase* MessageLoop::GetMessageLoopBase() {
return backend_.get();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
...@@ -175,8 +161,9 @@ std::unique_ptr<MessageLoop> MessageLoop::CreateUnbound( ...@@ -175,8 +161,9 @@ std::unique_ptr<MessageLoop> MessageLoop::CreateUnbound(
} }
MessageLoop::MessageLoop(Type type, std::unique_ptr<MessagePump> custom_pump) MessageLoop::MessageLoop(Type type, std::unique_ptr<MessagePump> custom_pump)
: backend_(sequence_manager::internal::SequenceManagerImpl::CreateUnbound( : sequence_manager_(
sequence_manager::SequenceManager::Settings{type})), sequence_manager::internal::SequenceManagerImpl::CreateUnbound(
sequence_manager::SequenceManager::Settings{type})),
default_task_queue_(CreateDefaultTaskQueue()), default_task_queue_(CreateDefaultTaskQueue()),
type_(type), type_(type),
custom_pump_(std::move(custom_pump)) { custom_pump_(std::move(custom_pump)) {
...@@ -186,13 +173,9 @@ MessageLoop::MessageLoop(Type type, std::unique_ptr<MessagePump> custom_pump) ...@@ -186,13 +173,9 @@ MessageLoop::MessageLoop(Type type, std::unique_ptr<MessagePump> custom_pump)
scoped_refptr<sequence_manager::TaskQueue> scoped_refptr<sequence_manager::TaskQueue>
MessageLoop::CreateDefaultTaskQueue() { MessageLoop::CreateDefaultTaskQueue() {
sequence_manager::internal::SequenceManagerImpl* manager = auto default_task_queue = sequence_manager_->CreateTaskQueue(
static_cast<sequence_manager::internal::SequenceManagerImpl*>( sequence_manager::TaskQueue::Spec("default_tq"));
backend_.get()); sequence_manager_->SetTaskRunner(default_task_queue->task_runner());
scoped_refptr<sequence_manager::TaskQueue> default_task_queue =
manager->CreateTaskQueueWithType<sequence_manager::TaskQueue>(
sequence_manager::TaskQueue::Spec("default_tq"));
manager->SetTaskRunner(default_task_queue->task_runner());
return default_task_queue; return default_task_queue;
} }
...@@ -208,7 +191,7 @@ void MessageLoop::BindToCurrentThread() { ...@@ -208,7 +191,7 @@ void MessageLoop::BindToCurrentThread() {
DCHECK(!MessageLoopCurrent::IsSet()) DCHECK(!MessageLoopCurrent::IsSet())
<< "should only have one message loop per thread"; << "should only have one message loop per thread";
backend_->BindToCurrentThread(std::move(pump)); sequence_manager_->BindToCurrentThread(std::move(pump));
} }
std::unique_ptr<MessagePump> MessageLoop::CreateMessagePump() { std::unique_ptr<MessagePump> MessageLoop::CreateMessagePump() {
...@@ -220,21 +203,21 @@ std::unique_ptr<MessagePump> MessageLoop::CreateMessagePump() { ...@@ -220,21 +203,21 @@ std::unique_ptr<MessagePump> MessageLoop::CreateMessagePump() {
} }
void MessageLoop::SetTimerSlack(TimerSlack timer_slack) { void MessageLoop::SetTimerSlack(TimerSlack timer_slack) {
backend_->SetTimerSlack(timer_slack); sequence_manager_->SetTimerSlack(timer_slack);
} }
std::string MessageLoop::GetThreadName() const { std::string MessageLoop::GetThreadName() const {
return backend_->GetThreadName(); return sequence_manager_->GetThreadName();
} }
scoped_refptr<SingleThreadTaskRunner> MessageLoop::task_runner() const { scoped_refptr<SingleThreadTaskRunner> MessageLoop::task_runner() const {
return backend_->GetTaskRunner(); return sequence_manager_->GetTaskRunner();
} }
void MessageLoop::SetTaskRunner( void MessageLoop::SetTaskRunner(
scoped_refptr<SingleThreadTaskRunner> task_runner) { scoped_refptr<SingleThreadTaskRunner> task_runner) {
DCHECK(task_runner); DCHECK(task_runner);
backend_->SetTaskRunner(task_runner); sequence_manager_->SetTaskRunner(task_runner);
} }
#if !defined(OS_NACL) #if !defined(OS_NACL)
...@@ -252,7 +235,7 @@ MessageLoopForUI::MessageLoopForUI(Type type) : MessageLoop(type) { ...@@ -252,7 +235,7 @@ MessageLoopForUI::MessageLoopForUI(Type type) : MessageLoop(type) {
#if defined(OS_IOS) #if defined(OS_IOS)
void MessageLoopForUI::Attach() { void MessageLoopForUI::Attach() {
backend_->AttachToMessagePump(); sequence_manager_->AttachToMessagePump();
} }
#endif // defined(OS_IOS) #endif // defined(OS_IOS)
......
...@@ -34,8 +34,7 @@ namespace sequence_manager { ...@@ -34,8 +34,7 @@ namespace sequence_manager {
class TaskQueue; class TaskQueue;
namespace internal { namespace internal {
class SequenceManagerImpl; class SequenceManagerImpl;
class ThreadControllerImpl; } // namespace internal
}
} // namespace sequence_manager } // namespace sequence_manager
// A MessageLoop is used to process events for a particular thread. There is // A MessageLoop is used to process events for a particular thread. There is
...@@ -81,11 +80,8 @@ class ThreadControllerImpl; ...@@ -81,11 +80,8 @@ class ThreadControllerImpl;
// Please be SURE your task is reentrant (nestable) and all global variables // Please be SURE your task is reentrant (nestable) and all global variables
// are stable and accessible before calling SetNestableTasksAllowed(true). // are stable and accessible before calling SetNestableTasksAllowed(true).
class BASE_EXPORT MessageLoopBase { class BASE_EXPORT MessageLoop {
public: public:
MessageLoopBase() = default;
virtual ~MessageLoopBase() = default;
// A MessageLoop has a particular type, which indicates the set of // A MessageLoop has a particular type, which indicates the set of
// asynchronous events it may process in addition to tasks and timers. // asynchronous events it may process in addition to tasks and timers.
// //
...@@ -109,7 +105,7 @@ class BASE_EXPORT MessageLoopBase { ...@@ -109,7 +105,7 @@ class BASE_EXPORT MessageLoopBase {
// TYPE_CUSTOM // TYPE_CUSTOM
// MessagePump was supplied to constructor. // MessagePump was supplied to constructor.
// //
enum Type { enum class Type {
TYPE_DEFAULT, TYPE_DEFAULT,
TYPE_UI, TYPE_UI,
TYPE_CUSTOM, TYPE_CUSTOM,
...@@ -119,106 +115,6 @@ class BASE_EXPORT MessageLoopBase { ...@@ -119,106 +115,6 @@ class BASE_EXPORT MessageLoopBase {
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
}; };
// Returns true if this loop is |type|. This allows subclasses (especially
// those in tests) to specialize how they are identified.
virtual bool IsType(Type type) const = 0;
// Returns the name of the thread this message loop is bound to. This function
// is only valid when this message loop is running, BindToCurrentThread has
// already been called and has an "happens-before" relationship with this call
// (this relationship is obtained implicitly by the MessageLoop's task posting
// system unless calling this very early).
virtual std::string GetThreadName() const = 0;
using DestructionObserver = MessageLoopCurrent::DestructionObserver;
// Add a DestructionObserver, which will start receiving notifications
// immediately.
virtual void AddDestructionObserver(
DestructionObserver* destruction_observer) = 0;
// Remove a DestructionObserver. It is safe to call this method while a
// DestructionObserver is receiving a notification callback.
virtual void RemoveDestructionObserver(
DestructionObserver* destruction_observer) = 0;
// TODO(altimin,yutak): Replace with base::TaskObserver.
using TaskObserver = MessageLoopCurrent::TaskObserver;
// These functions can only be called on the same thread that |this| is
// running on.
// These functions must not be called from a TaskObserver callback.
virtual void AddTaskObserver(TaskObserver* task_observer) = 0;
virtual void RemoveTaskObserver(TaskObserver* task_observer) = 0;
// When this functionality is enabled, the queue time will be recorded for
// posted tasks.
virtual void SetAddQueueTimeToTasks(bool enable) = 0;
// Returns true if this is the active MessageLoop for the current thread.
virtual bool IsBoundToCurrentThread() const = 0;
// Returns true if the message loop is idle (ignoring delayed tasks). This is
// the same condition which triggers DoWork() to return false: i.e.
// out of tasks which can be processed at the current run-level -- there might
// be deferred non-nestable tasks remaining if currently in a nested run
// level.
virtual bool IsIdleForTesting() = 0;
// Returns the MessagePump owned by this MessageLoop if any.
virtual MessagePump* GetMessagePump() const = 0;
// Sets a new TaskRunner for this message loop. If the message loop was
// already bound, this must be called on the thread to which it is bound.
// TODO(alexclarke): Remove this as part of https://crbug.com/825327.
virtual void SetTaskRunner(
scoped_refptr<SingleThreadTaskRunner> task_runner) = 0;
// Gets the TaskRunner associated with this message loop.
// TODO(alexclarke): Remove this as part of https://crbug.com/825327.
virtual scoped_refptr<SingleThreadTaskRunner> GetTaskRunner() = 0;
// Binds the MessageLoop to the current thread using |pump|.
virtual void BindToCurrentThread(std::unique_ptr<MessagePump> pump) = 0;
// Returns true if the MessageLoop retains any tasks inside it.
virtual bool HasTasks() = 0;
// Deletes all tasks associated with this MessageLoop. Note that the tasks
// can post other tasks when destructed.
virtual void DeletePendingTasks() = 0;
protected:
friend class MessageLoop;
friend class MessageLoopForUI;
friend class MessageLoopCurrent;
friend class MessageLoopCurrentForIO;
friend class MessageLoopCurrentForUI;
friend class Thread;
friend class sequence_manager::internal::ThreadControllerImpl;
// Explicitly allow or disallow task execution. Task execution is disallowed
// implicitly when we enter a nested runloop.
virtual void SetTaskExecutionAllowed(bool allowed) = 0;
// Whether task execution is allowed at the moment.
virtual bool IsTaskExecutionAllowed() const = 0;
#if defined(OS_IOS)
virtual void AttachToMessagePump() = 0;
#endif
virtual Type GetType() const = 0;
// Set the timer slack for this message loop.
// TODO(alexclarke): Remove this as part of https://crbug.com/891670.
virtual void SetTimerSlack(TimerSlack timer_slack) = 0;
};
class BASE_EXPORT MessageLoop {
public:
// For migration convenience we define the Type enum.
using Type = MessageLoopBase::Type;
static constexpr Type TYPE_DEFAULT = Type::TYPE_DEFAULT; static constexpr Type TYPE_DEFAULT = Type::TYPE_DEFAULT;
static constexpr Type TYPE_UI = Type::TYPE_UI; static constexpr Type TYPE_UI = Type::TYPE_UI;
static constexpr Type TYPE_CUSTOM = Type::TYPE_CUSTOM; static constexpr Type TYPE_CUSTOM = Type::TYPE_CUSTOM;
...@@ -229,7 +125,7 @@ class BASE_EXPORT MessageLoop { ...@@ -229,7 +125,7 @@ class BASE_EXPORT MessageLoop {
// Normally, it is not necessary to instantiate a MessageLoop. Instead, it // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
// is typical to make use of the current thread's MessageLoop instance. // is typical to make use of the current thread's MessageLoop instance.
explicit MessageLoop(Type type = TYPE_DEFAULT); explicit MessageLoop(Type type = Type::TYPE_DEFAULT);
// Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must
// be non-NULL. // be non-NULL.
explicit MessageLoop(std::unique_ptr<MessagePump> custom_pump); explicit MessageLoop(std::unique_ptr<MessagePump> custom_pump);
...@@ -290,8 +186,6 @@ class BASE_EXPORT MessageLoop { ...@@ -290,8 +186,6 @@ class BASE_EXPORT MessageLoop {
// TODO(alexclarke): Make this const when MessageLoopImpl goes away. // TODO(alexclarke): Make this const when MessageLoopImpl goes away.
bool IsIdleForTesting(); bool IsIdleForTesting();
MessageLoopBase* GetMessageLoopBase();
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
protected: protected:
using MessagePumpFactoryCallback = using MessagePumpFactoryCallback =
...@@ -308,17 +202,16 @@ class BASE_EXPORT MessageLoop { ...@@ -308,17 +202,16 @@ class BASE_EXPORT MessageLoop {
// Configure various members and bind this message loop to the current thread. // Configure various members and bind this message loop to the current thread.
void BindToCurrentThread(); void BindToCurrentThread();
// A raw pointer to the MessagePump handed-off to |backend_|. // A raw pointer to the MessagePump handed-off to |sequence_manager_|.
// Valid for the lifetime of |backend_|. // Valid for the lifetime of |sequence_manager_|.
MessagePump* pump_ = nullptr; MessagePump* pump_ = nullptr;
// The SequenceManager-based implementation of the MessageLoop. // TODO(crbug.com/891670): We shouldn't publicly expose all of
// TODO(crbug.com/891670): MessageLoopBase is now always a // SequenceManagerImpl.
// SequenceManagerImpl, this can be simplified but we also shouldn't publicly const std::unique_ptr<sequence_manager::internal::SequenceManagerImpl>
// expose all of SequenceManagerImpl either. sequence_manager_;
const std::unique_ptr<MessageLoopBase> backend_; // SequenceManager requires an explicit initialisation of the default task
// SequenceManager-based backend requires an explicit initialisation of the // queue.
// default task queue.
const scoped_refptr<sequence_manager::TaskQueue> default_task_queue_; const scoped_refptr<sequence_manager::TaskQueue> default_task_queue_;
private: private:
...@@ -345,6 +238,11 @@ class BASE_EXPORT MessageLoop { ...@@ -345,6 +238,11 @@ class BASE_EXPORT MessageLoop {
std::unique_ptr<MessagePump> CreateMessagePump(); std::unique_ptr<MessagePump> CreateMessagePump();
sequence_manager::internal::SequenceManagerImpl* GetSequenceManagerImpl()
const {
return sequence_manager_.get();
}
const Type type_; const Type type_;
// If set this will be returned by the next call to CreateMessagePump(). // If set this will be returned by the next call to CreateMessagePump().
......
...@@ -19,13 +19,14 @@ namespace base { ...@@ -19,13 +19,14 @@ namespace base {
// MessageLoopCurrent // MessageLoopCurrent
// static // static
MessageLoopBase* MessageLoopCurrent::GetCurrentMessageLoopBase() { sequence_manager::internal::SequenceManagerImpl*
MessageLoopCurrent::GetCurrentSequenceManagerImpl() {
return sequence_manager::internal::SequenceManagerImpl::GetCurrent(); return sequence_manager::internal::SequenceManagerImpl::GetCurrent();
} }
// static // static
MessageLoopCurrent MessageLoopCurrent::Get() { MessageLoopCurrent MessageLoopCurrent::Get() {
return MessageLoopCurrent(GetCurrentMessageLoopBase()); return MessageLoopCurrent(GetCurrentSequenceManagerImpl());
} }
// static // static
...@@ -35,7 +36,7 @@ MessageLoopCurrent MessageLoopCurrent::GetNull() { ...@@ -35,7 +36,7 @@ MessageLoopCurrent MessageLoopCurrent::GetNull() {
// static // static
bool MessageLoopCurrent::IsSet() { bool MessageLoopCurrent::IsSet() {
return !!GetCurrentMessageLoopBase(); return !!GetCurrentSequenceManagerImpl();
} }
void MessageLoopCurrent::AddDestructionObserver( void MessageLoopCurrent::AddDestructionObserver(
...@@ -66,7 +67,7 @@ void MessageLoopCurrent::SetTaskRunner( ...@@ -66,7 +67,7 @@ void MessageLoopCurrent::SetTaskRunner(
} }
bool MessageLoopCurrent::IsBoundToCurrentThread() const { bool MessageLoopCurrent::IsBoundToCurrentThread() const {
return current_ == GetCurrentMessageLoopBase(); return current_ == GetCurrentSequenceManagerImpl();
} }
bool MessageLoopCurrent::IsIdleForTesting() { bool MessageLoopCurrent::IsIdleForTesting() {
...@@ -99,13 +100,13 @@ bool MessageLoopCurrent::NestableTasksAllowed() const { ...@@ -99,13 +100,13 @@ bool MessageLoopCurrent::NestableTasksAllowed() const {
} }
MessageLoopCurrent::ScopedNestableTaskAllower::ScopedNestableTaskAllower() MessageLoopCurrent::ScopedNestableTaskAllower::ScopedNestableTaskAllower()
: loop_(GetCurrentMessageLoopBase()), : sequence_manager_(GetCurrentSequenceManagerImpl()),
old_state_(loop_->IsTaskExecutionAllowed()) { old_state_(sequence_manager_->IsTaskExecutionAllowed()) {
loop_->SetTaskExecutionAllowed(true); sequence_manager_->SetTaskExecutionAllowed(true);
} }
MessageLoopCurrent::ScopedNestableTaskAllower::~ScopedNestableTaskAllower() { MessageLoopCurrent::ScopedNestableTaskAllower::~ScopedNestableTaskAllower() {
loop_->SetTaskExecutionAllowed(old_state_); sequence_manager_->SetTaskExecutionAllowed(old_state_);
} }
bool MessageLoopCurrent::operator==(const MessageLoopCurrent& other) const { bool MessageLoopCurrent::operator==(const MessageLoopCurrent& other) const {
...@@ -119,26 +120,27 @@ bool MessageLoopCurrent::operator==(const MessageLoopCurrent& other) const { ...@@ -119,26 +120,27 @@ bool MessageLoopCurrent::operator==(const MessageLoopCurrent& other) const {
// static // static
MessageLoopCurrentForUI MessageLoopCurrentForUI::Get() { MessageLoopCurrentForUI MessageLoopCurrentForUI::Get() {
MessageLoopBase* loop = GetCurrentMessageLoopBase(); auto* sequence_manager = GetCurrentSequenceManagerImpl();
DCHECK(loop); DCHECK(sequence_manager);
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
DCHECK(loop->IsType(MessageLoop::TYPE_UI) || DCHECK(sequence_manager->IsType(MessageLoop::TYPE_UI) ||
loop->IsType(MessageLoop::TYPE_JAVA)); sequence_manager->IsType(MessageLoop::TYPE_JAVA));
#else // defined(OS_ANDROID) #else // defined(OS_ANDROID)
DCHECK(loop->IsType(MessageLoop::TYPE_UI)); DCHECK(sequence_manager->IsType(MessageLoop::TYPE_UI));
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
return MessageLoopCurrentForUI(loop); return MessageLoopCurrentForUI(sequence_manager);
} }
// static // static
bool MessageLoopCurrentForUI::IsSet() { bool MessageLoopCurrentForUI::IsSet() {
MessageLoopBase* loop = GetCurrentMessageLoopBase(); sequence_manager::internal::SequenceManagerImpl* sequence_manager =
return loop && GetCurrentSequenceManagerImpl();
return sequence_manager &&
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
(loop->IsType(MessageLoop::TYPE_UI) || (sequence_manager->IsType(MessageLoop::TYPE_UI) ||
loop->IsType(MessageLoop::TYPE_JAVA)); sequence_manager->IsType(MessageLoop::TYPE_JAVA));
#else // defined(OS_ANDROID) #else // defined(OS_ANDROID)
loop->IsType(MessageLoop::TYPE_UI); sequence_manager->IsType(MessageLoop::TYPE_UI);
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
} }
...@@ -190,16 +192,16 @@ void MessageLoopCurrentForUI::RemoveMessagePumpObserver( ...@@ -190,16 +192,16 @@ void MessageLoopCurrentForUI::RemoveMessagePumpObserver(
// static // static
MessageLoopCurrentForIO MessageLoopCurrentForIO::Get() { MessageLoopCurrentForIO MessageLoopCurrentForIO::Get() {
MessageLoopBase* loop = GetCurrentMessageLoopBase(); auto* sequence_manager = GetCurrentSequenceManagerImpl();
DCHECK(loop); DCHECK(sequence_manager);
DCHECK(loop->IsType(MessageLoop::TYPE_IO)); DCHECK(sequence_manager->IsType(MessageLoop::TYPE_IO));
return MessageLoopCurrentForIO(loop); return MessageLoopCurrentForIO(sequence_manager);
} }
// static // static
bool MessageLoopCurrentForIO::IsSet() { bool MessageLoopCurrentForIO::IsSet() {
MessageLoopBase* loop = GetCurrentMessageLoopBase(); auto* sequence_manager = GetCurrentSequenceManagerImpl();
return loop && loop->IsType(MessageLoop::TYPE_IO); return sequence_manager && sequence_manager->IsType(MessageLoop::TYPE_IO);
} }
MessagePumpForIO* MessageLoopCurrentForIO::GetMessagePumpForIO() const { MessagePumpForIO* MessageLoopCurrentForIO::GetMessagePumpForIO() const {
......
...@@ -23,7 +23,6 @@ class TestWebThreadBundle; ...@@ -23,7 +23,6 @@ class TestWebThreadBundle;
namespace base { namespace base {
class MessageLoopBase;
class MessageLoopImpl; class MessageLoopImpl;
namespace sequence_manager { namespace sequence_manager {
...@@ -172,7 +171,7 @@ class BASE_EXPORT MessageLoopCurrent { ...@@ -172,7 +171,7 @@ class BASE_EXPORT MessageLoopCurrent {
~ScopedNestableTaskAllower(); ~ScopedNestableTaskAllower();
private: private:
MessageLoopBase* const loop_; sequence_manager::internal::SequenceManagerImpl* const sequence_manager_;
const bool old_state_; const bool old_state_;
}; };
...@@ -187,9 +186,12 @@ class BASE_EXPORT MessageLoopCurrent { ...@@ -187,9 +186,12 @@ class BASE_EXPORT MessageLoopCurrent {
bool IsIdleForTesting(); bool IsIdleForTesting();
protected: protected:
explicit MessageLoopCurrent(MessageLoopBase* current) : current_(current) {} explicit MessageLoopCurrent(
sequence_manager::internal::SequenceManagerImpl* sequence_manager)
: current_(sequence_manager) {}
static MessageLoopBase* GetCurrentMessageLoopBase(); static sequence_manager::internal::SequenceManagerImpl*
GetCurrentSequenceManagerImpl();
friend class MessageLoopImpl; friend class MessageLoopImpl;
friend class MessagePumpLibeventTest; friend class MessagePumpLibeventTest;
...@@ -199,7 +201,7 @@ class BASE_EXPORT MessageLoopCurrent { ...@@ -199,7 +201,7 @@ class BASE_EXPORT MessageLoopCurrent {
friend class MessageLoopTaskRunnerTest; friend class MessageLoopTaskRunnerTest;
friend class web::TestWebThreadBundle; friend class web::TestWebThreadBundle;
MessageLoopBase* current_; sequence_manager::internal::SequenceManagerImpl* current_;
}; };
#if !defined(OS_NACL) #if !defined(OS_NACL)
...@@ -250,7 +252,8 @@ class BASE_EXPORT MessageLoopCurrentForUI : public MessageLoopCurrent { ...@@ -250,7 +252,8 @@ class BASE_EXPORT MessageLoopCurrentForUI : public MessageLoopCurrent {
#endif #endif
private: private:
explicit MessageLoopCurrentForUI(MessageLoopBase* current) explicit MessageLoopCurrentForUI(
sequence_manager::internal::SequenceManagerImpl* current)
: MessageLoopCurrent(current) {} : MessageLoopCurrent(current) {}
MessagePumpForUI* GetMessagePumpForUI() const; MessagePumpForUI* GetMessagePumpForUI() const;
...@@ -306,7 +309,8 @@ class BASE_EXPORT MessageLoopCurrentForIO : public MessageLoopCurrent { ...@@ -306,7 +309,8 @@ class BASE_EXPORT MessageLoopCurrentForIO : public MessageLoopCurrent {
#endif // !defined(OS_NACL_SFI) #endif // !defined(OS_NACL_SFI)
private: private:
explicit MessageLoopCurrentForIO(MessageLoopBase* current) explicit MessageLoopCurrentForIO(
sequence_manager::internal::SequenceManagerImpl* current)
: MessageLoopCurrent(current) {} : MessageLoopCurrent(current) {}
MessagePumpForIO* GetMessagePumpForIO() const; MessagePumpForIO* GetMessagePumpForIO() const;
......
...@@ -2328,20 +2328,8 @@ class PostTaskOnDestroy { ...@@ -2328,20 +2328,8 @@ class PostTaskOnDestroy {
} // namespace } // namespace
// Test that MessageLoop destruction handles a task's destructor posting another // Test that MessageLoop destruction handles a task's destructor posting another
// task by: // task.
// 1) Not getting stuck clearing its task queue. TEST(MessageLoopDestructionTest, DestroysFineWithPostTaskOnDestroy) {
// 2) DCHECKing when clearing pending tasks many times still doesn't yield an
// empty queue.
TEST(MessageLoopDestructionTest, ExpectDeathWithStubbornPostTaskOnDestroy) {
std::unique_ptr<MessageLoop> loop = std::make_unique<MessageLoop>();
EXPECT_DCHECK_DEATH({
PostTaskOnDestroy::PostTaskWithPostingDestructor(1000);
loop.reset();
});
}
TEST(MessageLoopDestructionTest, DestroysFineWithReasonablePostTaskOnDestroy) {
std::unique_ptr<MessageLoop> loop = std::make_unique<MessageLoop>(); std::unique_ptr<MessageLoop> loop = std::make_unique<MessageLoop>();
PostTaskOnDestroy::PostTaskWithPostingDestructor(10); PostTaskOnDestroy::PostTaskWithPostingDestructor(10);
......
...@@ -10,11 +10,13 @@ ...@@ -10,11 +10,13 @@
#include "base/format_macros.h" #include "base/format_macros.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_current.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/synchronization/condition_variable.h" #include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "base/task/sequence_manager/sequence_manager_impl.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -175,12 +177,12 @@ class ScheduleWorkTest : public testing::Test { ...@@ -175,12 +177,12 @@ class ScheduleWorkTest : public testing::Test {
} }
} }
MessageLoopBase* target_message_loop_base() { sequence_manager::internal::SequenceManagerImpl* target_message_loop_base() {
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
if (java_thread_) if (java_thread_)
return java_thread_->message_loop()->GetMessageLoopBase(); return java_thread_->message_loop()->GetSequenceManagerImpl();
#endif #endif
return message_loop_->GetMessageLoopBase(); return MessageLoopCurrent::Get()->GetCurrentSequenceManagerImpl();
} }
private: private:
......
...@@ -28,9 +28,9 @@ namespace base { ...@@ -28,9 +28,9 @@ namespace base {
namespace { namespace {
bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) { bool PumpTypeUsesDoSomeWork(MessageLoop::Type type) {
switch (type) { switch (type) {
case MessageLoopBase::Type::TYPE_DEFAULT: case MessageLoop::Type::TYPE_DEFAULT:
#if defined(OS_IOS) #if defined(OS_IOS)
// iOS uses a MessagePumpCFRunLoop instead of MessagePumpDefault for // iOS uses a MessagePumpCFRunLoop instead of MessagePumpDefault for
// TYPE_DEFAULT. TODO(gab): migrate MessagePumpCFRunLoop too. // TYPE_DEFAULT. TODO(gab): migrate MessagePumpCFRunLoop too.
...@@ -39,7 +39,7 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) { ...@@ -39,7 +39,7 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) {
return true; return true;
#endif #endif
case MessageLoopBase::Type::TYPE_UI: case MessageLoop::Type::TYPE_UI:
#if defined(OS_IOS) #if defined(OS_IOS)
// iOS uses a MessagePumpDefault for UI in unit tests, ref. // iOS uses a MessagePumpDefault for UI in unit tests, ref.
// test_support_ios.mm::CreateMessagePumpForUIForTests(). // test_support_ios.mm::CreateMessagePumpForUIForTests().
...@@ -56,7 +56,7 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) { ...@@ -56,7 +56,7 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) {
return false; return false;
#endif #endif
case MessageLoopBase::Type::TYPE_IO: case MessageLoop::Type::TYPE_IO:
#if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS)) #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(OS_IOS))
return true; return true;
#elif defined(OS_POSIX) && !defined(OS_NACL_SFI) #elif defined(OS_POSIX) && !defined(OS_NACL_SFI)
...@@ -69,9 +69,9 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) { ...@@ -69,9 +69,9 @@ bool PumpTypeUsesDoSomeWork(MessageLoopBase::Type type) {
return false; return false;
#endif #endif
case MessageLoopBase::Type::TYPE_CUSTOM: case MessageLoop::Type::TYPE_CUSTOM:
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
case MessageLoopBase::Type::TYPE_JAVA: case MessageLoop::Type::TYPE_JAVA:
#endif // defined(OS_ANDROID) #endif // defined(OS_ANDROID)
// Not tested in this file. // Not tested in this file.
NOTREACHED(); NOTREACHED();
...@@ -96,7 +96,7 @@ class MockMessagePumpDelegate : public MessagePump::Delegate { ...@@ -96,7 +96,7 @@ class MockMessagePumpDelegate : public MessagePump::Delegate {
DISALLOW_COPY_AND_ASSIGN(MockMessagePumpDelegate); DISALLOW_COPY_AND_ASSIGN(MockMessagePumpDelegate);
}; };
class MessagePumpTest : public ::testing::TestWithParam<MessageLoopBase::Type> { class MessagePumpTest : public ::testing::TestWithParam<MessageLoop::Type> {
public: public:
MessagePumpTest() MessagePumpTest()
: message_pump_(MessageLoop::CreateMessagePumpForType(GetParam())) {} : message_pump_(MessageLoop::CreateMessagePumpForType(GetParam())) {}
......
...@@ -54,6 +54,7 @@ namespace internal { ...@@ -54,6 +54,7 @@ namespace internal {
class RealTimeDomain; class RealTimeDomain;
class TaskQueueImpl; class TaskQueueImpl;
class ThreadControllerImpl;
// The task queue manager provides N task queues and a selector interface for // The task queue manager provides N task queues and a selector interface for
// choosing which task queue to service next. Each task queue consists of two // choosing which task queue to service next. Each task queue consists of two
...@@ -71,8 +72,7 @@ class BASE_EXPORT SequenceManagerImpl ...@@ -71,8 +72,7 @@ class BASE_EXPORT SequenceManagerImpl
: public SequenceManager, : public SequenceManager,
public internal::SequencedTaskSource, public internal::SequencedTaskSource,
public internal::TaskQueueSelector::Observer, public internal::TaskQueueSelector::Observer,
public RunLoop::NestingObserver, public RunLoop::NestingObserver {
public MessageLoopBase {
public: public:
using Observer = SequenceManager::Observer; using Observer = SequenceManager::Observer;
...@@ -97,8 +97,6 @@ class BASE_EXPORT SequenceManagerImpl ...@@ -97,8 +97,6 @@ class BASE_EXPORT SequenceManagerImpl
scoped_refptr<SingleThreadTaskRunner> task_runner, scoped_refptr<SingleThreadTaskRunner> task_runner,
SequenceManager::Settings settings); SequenceManager::Settings settings);
void BindToMessageLoop(MessageLoopBase* message_loop_base);
// SequenceManager implementation: // SequenceManager implementation:
void BindToCurrentThread() override; void BindToCurrentThread() override;
void BindToMessagePump(std::unique_ptr<MessagePump> message_pump) override; void BindToMessagePump(std::unique_ptr<MessagePump> message_pump) override;
...@@ -130,33 +128,31 @@ class BASE_EXPORT SequenceManagerImpl ...@@ -130,33 +128,31 @@ class BASE_EXPORT SequenceManagerImpl
bool HasPendingHighResolutionTasks() override; bool HasPendingHighResolutionTasks() override;
bool OnSystemIdle() override; bool OnSystemIdle() override;
// MessageLoopBase implementation: void AddTaskObserver(MessageLoop::TaskObserver* task_observer);
void AddTaskObserver(MessageLoop::TaskObserver* task_observer) override; void RemoveTaskObserver(MessageLoop::TaskObserver* task_observer);
void RemoveTaskObserver(MessageLoop::TaskObserver* task_observer) override;
void AddDestructionObserver( void AddDestructionObserver(
MessageLoopCurrent::DestructionObserver* destruction_observer) override; MessageLoopCurrent::DestructionObserver* destruction_observer);
void RemoveDestructionObserver( void RemoveDestructionObserver(
MessageLoopCurrent::DestructionObserver* destruction_observer) override; MessageLoopCurrent::DestructionObserver* destruction_observer);
// TODO(alexclarke): Remove this as part of https://crbug.com/825327. // TODO(alexclarke): Remove this as part of https://crbug.com/825327.
void SetTaskRunner( void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner);
scoped_refptr<SingleThreadTaskRunner> task_runner) override;
// TODO(alexclarke): Remove this as part of https://crbug.com/825327. // TODO(alexclarke): Remove this as part of https://crbug.com/825327.
scoped_refptr<SingleThreadTaskRunner> GetTaskRunner() override; scoped_refptr<SingleThreadTaskRunner> GetTaskRunner();
std::string GetThreadName() const override; std::string GetThreadName() const;
bool IsBoundToCurrentThread() const override; bool IsBoundToCurrentThread() const;
MessagePump* GetMessagePump() const override; MessagePump* GetMessagePump() const;
bool IsType(MessageLoop::Type type) const override; bool IsType(MessageLoop::Type type) const;
void SetAddQueueTimeToTasks(bool enable) override; void SetAddQueueTimeToTasks(bool enable);
void SetTaskExecutionAllowed(bool allowed) override; void SetTaskExecutionAllowed(bool allowed);
bool IsTaskExecutionAllowed() const override; bool IsTaskExecutionAllowed() const;
#if defined(OS_IOS) #if defined(OS_IOS)
void AttachToMessagePump() override; void AttachToMessagePump();
#endif #endif
bool IsIdleForTesting() override; bool IsIdleForTesting() override;
void BindToCurrentThread(std::unique_ptr<MessagePump> pump) override; void BindToCurrentThread(std::unique_ptr<MessagePump> pump);
void DeletePendingTasks() override; void DeletePendingTasks();
bool HasTasks() override; bool HasTasks();
MessageLoop::Type GetType() const override; MessageLoop::Type GetType() const;
// Requests that a task to process work is scheduled. // Requests that a task to process work is scheduled.
void ScheduleWork(); void ScheduleWork();
......
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