Commit 69e7ddc5 authored by Gabriel Charette's avatar Gabriel Charette Committed by Commit Bot

[base MessageLoopCurrent] Introduce ScopedAllowApplicationTasksInNativeNestedLoop

As a new name for ScopedNestableTaskAllower. A migration will follow
to move callers either to the new name or to
RunLoop::Type::kNestableTasksAllowed.

R=fdoray@chromium.org

Bug: 781352
Change-Id: I5208655072728b4305e70815ccb933173ff0842e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2225834
Commit-Queue: Gabriel Charette <gab@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Auto-Submit: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#775160}
parent 1df887dc
...@@ -81,15 +81,17 @@ void MessageLoopCurrent::SetAddQueueTimeToTasks(bool enable) { ...@@ -81,15 +81,17 @@ void MessageLoopCurrent::SetAddQueueTimeToTasks(bool enable) {
current_->SetAddQueueTimeToTasks(enable); current_->SetAddQueueTimeToTasks(enable);
} }
MessageLoopCurrent::ScopedNestableTaskAllower::ScopedNestableTaskAllower() MessageLoopCurrent::ScopedAllowApplicationTasksInNativeNestedLoop::
ScopedAllowApplicationTasksInNativeNestedLoop()
: sequence_manager_(GetCurrentSequenceManagerImpl()), : sequence_manager_(GetCurrentSequenceManagerImpl()),
old_state_(sequence_manager_->IsTaskExecutionAllowed()) { previous_state_(sequence_manager_->IsTaskExecutionAllowed()) {
TRACE_EVENT_BEGIN0("base", "ScopedNestableTaskAllower"); TRACE_EVENT_BEGIN0("base", "ScopedNestableTaskAllower");
sequence_manager_->SetTaskExecutionAllowed(true); sequence_manager_->SetTaskExecutionAllowed(true);
} }
MessageLoopCurrent::ScopedNestableTaskAllower::~ScopedNestableTaskAllower() { MessageLoopCurrent::ScopedAllowApplicationTasksInNativeNestedLoop::
sequence_manager_->SetTaskExecutionAllowed(old_state_); ~ScopedAllowApplicationTasksInNativeNestedLoop() {
sequence_manager_->SetTaskExecutionAllowed(previous_state_);
TRACE_EVENT_END0("base", "ScopedNestableTaskAllower"); TRACE_EVENT_END0("base", "ScopedNestableTaskAllower");
} }
......
...@@ -134,22 +134,24 @@ class BASE_EXPORT MessageLoopCurrent { ...@@ -134,22 +134,24 @@ class BASE_EXPORT MessageLoopCurrent {
// Otherwise, it will get executed right after task #1 completes at "thread // Otherwise, it will get executed right after task #1 completes at "thread
// message loop level". // message loop level".
// //
// Prefer RunLoop::Type::kNestableTasksAllowed when nesting is requested by // Use RunLoop::Type::kNestableTasksAllowed when nesting is triggered by the
// the application. // application RunLoop rather than by native code.
// class BASE_EXPORT ScopedAllowApplicationTasksInNativeNestedLoop {
// TODO(https://crbug.com/781352): Remove usage of this class alongside
// RunLoop and rename it to ScopedApplicationTasksAllowedInNativeNestedLoop(?)
// for remaining use cases.
class BASE_EXPORT ScopedNestableTaskAllower {
public: public:
ScopedNestableTaskAllower(); ScopedAllowApplicationTasksInNativeNestedLoop();
~ScopedNestableTaskAllower(); ~ScopedAllowApplicationTasksInNativeNestedLoop();
private: private:
sequence_manager::internal::SequenceManagerImpl* const sequence_manager_; sequence_manager::internal::SequenceManagerImpl* const sequence_manager_;
const bool old_state_; const bool previous_state_;
}; };
// TODO(https://crbug.com/781352): Remove usage of this old class. Either
// renaming it to ScopedAllowApplicationTasksInNativeNestedLoop when truly
// native or migrating it to RunLoop::Type::kNestableTasksAllowed otherwise.
using ScopedNestableTaskAllower =
ScopedAllowApplicationTasksInNativeNestedLoop;
// Returns true if nestable tasks are allowed on the current loop at this time // Returns true if nestable tasks are allowed on the current loop at this time
// (i.e. if a nested loop would start from the callee's point in the stack, // (i.e. if a nested loop would start from the callee's point in the stack,
// would it be allowed to run application tasks). // would it be allowed to run application tasks).
......
...@@ -273,7 +273,8 @@ class MessageLoopTest : public ::testing::Test {}; ...@@ -273,7 +273,8 @@ class MessageLoopTest : public ::testing::Test {};
#if defined(OS_WIN) #if defined(OS_WIN)
void SubPumpFunc(OnceClosure on_done) { void SubPumpFunc(OnceClosure on_done) {
MessageLoopCurrent::ScopedNestableTaskAllower allow_nestable_tasks; MessageLoopCurrent::ScopedAllowApplicationTasksInNativeNestedLoop
allow_nestable_tasks;
MSG msg; MSG msg;
while (::GetMessage(&msg, NULL, 0, 0)) { while (::GetMessage(&msg, NULL, 0, 0)) {
::TranslateMessage(&msg); ::TranslateMessage(&msg);
...@@ -289,7 +290,8 @@ const wchar_t kMessageBoxTitle[] = L"MessageLoop Unit Test"; ...@@ -289,7 +290,8 @@ const wchar_t kMessageBoxTitle[] = L"MessageLoop Unit Test";
// implicit message loops. // implicit message loops.
void MessageBoxFunc(TaskList* order, int cookie, bool is_reentrant) { void MessageBoxFunc(TaskList* order, int cookie, bool is_reentrant) {
order->RecordStart(MESSAGEBOX, cookie); order->RecordStart(MESSAGEBOX, cookie);
Optional<MessageLoopCurrent::ScopedNestableTaskAllower> maybe_allow_nesting; Optional<MessageLoopCurrent::ScopedAllowApplicationTasksInNativeNestedLoop>
maybe_allow_nesting;
if (is_reentrant) if (is_reentrant)
maybe_allow_nesting.emplace(); maybe_allow_nesting.emplace();
::MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK); ::MessageBox(NULL, L"Please wait...", kMessageBoxTitle, MB_OK);
...@@ -955,10 +957,7 @@ namespace { ...@@ -955,10 +957,7 @@ namespace {
void FuncThatRuns(TaskList* order, int cookie, RunLoop* run_loop) { void FuncThatRuns(TaskList* order, int cookie, RunLoop* run_loop) {
order->RecordStart(RUNS, cookie); order->RecordStart(RUNS, cookie);
{ run_loop->Run();
MessageLoopCurrent::ScopedNestableTaskAllower allow;
run_loop->Run();
}
order->RecordEnd(RUNS, cookie); order->RecordEnd(RUNS, cookie);
} }
...@@ -974,10 +973,11 @@ TEST_P(MessageLoopTypedTest, QuitNow) { ...@@ -974,10 +973,11 @@ TEST_P(MessageLoopTypedTest, QuitNow) {
TaskList order; TaskList order;
RunLoop run_loop; RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&FuncThatRuns, &order, 1, Unretained(&run_loop))); FROM_HERE,
BindOnce(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop)));
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
BindOnce(&OrderedFunc, &order, 2)); BindOnce(&OrderedFunc, &order, 2));
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
...@@ -1009,7 +1009,7 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitTop) { ...@@ -1009,7 +1009,7 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitTop) {
TaskList order; TaskList order;
RunLoop outer_run_loop; RunLoop outer_run_loop;
RunLoop nested_run_loop; RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE,
...@@ -1039,7 +1039,7 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitNested) { ...@@ -1039,7 +1039,7 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitNested) {
TaskList order; TaskList order;
RunLoop outer_run_loop; RunLoop outer_run_loop;
RunLoop nested_run_loop; RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE,
...@@ -1104,7 +1104,7 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitBogus) { ...@@ -1104,7 +1104,7 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitBogus) {
TaskList order; TaskList order;
RunLoop outer_run_loop; RunLoop outer_run_loop;
RunLoop nested_run_loop; RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
RunLoop bogus_run_loop; RunLoop bogus_run_loop;
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
...@@ -1137,10 +1137,10 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitDeep) { ...@@ -1137,10 +1137,10 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitDeep) {
TaskList order; TaskList order;
RunLoop outer_run_loop; RunLoop outer_run_loop;
RunLoop nested_loop1; RunLoop nested_loop1(RunLoop::Type::kNestableTasksAllowed);
RunLoop nested_loop2; RunLoop nested_loop2(RunLoop::Type::kNestableTasksAllowed);
RunLoop nested_loop3; RunLoop nested_loop3(RunLoop::Type::kNestableTasksAllowed);
RunLoop nested_loop4; RunLoop nested_loop4(RunLoop::Type::kNestableTasksAllowed);
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&FuncThatRuns, &order, 1, Unretained(&nested_loop1))); FROM_HERE, BindOnce(&FuncThatRuns, &order, 1, Unretained(&nested_loop1)));
...@@ -1249,10 +1249,11 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitOrderAfter) { ...@@ -1249,10 +1249,11 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitOrderAfter) {
TaskList order; TaskList order;
RunLoop run_loop; RunLoop nested_run_loop(RunLoop::Type::kNestableTasksAllowed);
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, BindOnce(&FuncThatRuns, &order, 1, Unretained(&run_loop))); FROM_HERE,
BindOnce(&FuncThatRuns, &order, 1, Unretained(&nested_run_loop)));
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
BindOnce(&OrderedFunc, &order, 2)); BindOnce(&OrderedFunc, &order, 2));
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
...@@ -1260,13 +1261,13 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitOrderAfter) { ...@@ -1260,13 +1261,13 @@ TEST_P(MessageLoopTypedTest, RunLoopQuitOrderAfter) {
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
BindOnce(&OrderedFunc, &order, 3)); BindOnce(&OrderedFunc, &order, 3));
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, run_loop.QuitClosure()); // has no affect FROM_HERE, nested_run_loop.QuitClosure()); // has no affect
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
BindOnce(&OrderedFunc, &order, 4)); BindOnce(&OrderedFunc, &order, 4));
ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
BindOnce(&FuncThatQuitsNow)); BindOnce(&FuncThatQuitsNow));
run_loop.allow_quit_current_deprecated_ = true; nested_run_loop.allow_quit_current_deprecated_ = true;
RunLoop outer_run_loop; RunLoop outer_run_loop;
outer_run_loop.Run(); outer_run_loop.Run();
...@@ -1368,7 +1369,7 @@ TEST_P(MessageLoopTypedTest, NestableTasksAllowedExplicitlyInScope) { ...@@ -1368,7 +1369,7 @@ TEST_P(MessageLoopTypedTest, NestableTasksAllowedExplicitlyInScope) {
BindOnce( BindOnce(
[](RunLoop* run_loop) { [](RunLoop* run_loop) {
{ {
MessageLoopCurrent::ScopedNestableTaskAllower MessageLoopCurrent::ScopedAllowApplicationTasksInNativeNestedLoop
allow_nestable_tasks; allow_nestable_tasks;
EXPECT_TRUE(MessageLoopCurrent::Get()->NestableTasksAllowed()); EXPECT_TRUE(MessageLoopCurrent::Get()->NestableTasksAllowed());
} }
...@@ -1572,8 +1573,8 @@ TEST_F(MessageLoopTest, PostImmediateTaskFromSystemPump) { ...@@ -1572,8 +1573,8 @@ TEST_F(MessageLoopTest, PostImmediateTaskFromSystemPump) {
// work_deduplicator.cc(50): OnWorkStarted // work_deduplicator.cc(50): OnWorkStarted
// 2) SubPumpFunc entered: // 2) SubPumpFunc entered:
// message_loop_unittest.cc(278): SubPumpFunc // message_loop_unittest.cc(278): SubPumpFunc
// 3) ScopedNestableTaskAllower triggers nested ScheduleWork: // 3) ScopedAllowApplicationTasksInNativeNestedLoop triggers nested
// work_deduplicator.cc(34): OnWorkRequested // ScheduleWork: work_deduplicator.cc(34): OnWorkRequested
// 4) Nested system loop starts and pumps internal kMsgHaveWork: // 4) Nested system loop starts and pumps internal kMsgHaveWork:
// message_loop_unittest.cc(282): SubPumpFunc : Got Message // message_loop_unittest.cc(282): SubPumpFunc : Got Message
// message_pump_win.cc(302): HandleWorkMessage // message_pump_win.cc(302): HandleWorkMessage
...@@ -1606,9 +1607,9 @@ TEST_F(MessageLoopTest, PostImmediateTaskFromSystemPump) { ...@@ -1606,9 +1607,9 @@ TEST_F(MessageLoopTest, PostImmediateTaskFromSystemPump) {
// 11) Nested application task completes and SubPumpFunc unwinds: // 11) Nested application task completes and SubPumpFunc unwinds:
// work_deduplicator.cc(58): WillCheckForMoreWork // work_deduplicator.cc(58): WillCheckForMoreWork
// work_deduplicator.cc(67): DidCheckForMoreWork // work_deduplicator.cc(67): DidCheckForMoreWork
// 12) ~ScopedNestableTaskAllower() makes sure WorkDeduplicator knows we're // 12) ~ScopedAllowApplicationTasksInNativeNestedLoop() makes sure
// back in DoWork() (not relevant in this test but important overall). // WorkDeduplicator knows we're back in DoWork() (not relevant in this test
// work_deduplicator.cc(50): OnWorkStarted // but important overall). work_deduplicator.cc(50): OnWorkStarted
// 13) Application task which ran SubPumpFunc completes and test finishes. // 13) Application task which ran SubPumpFunc completes and test finishes.
// work_deduplicator.cc(67): DidCheckForMoreWork // work_deduplicator.cc(67): DidCheckForMoreWork
TEST_F(MessageLoopTest, PostDelayedTaskFromSystemPump) { TEST_F(MessageLoopTest, PostDelayedTaskFromSystemPump) {
...@@ -1672,11 +1673,11 @@ TEST_F(MessageLoopTest, RepostingWmQuitDoesntStarveUpcomingNativeLoop) { ...@@ -1672,11 +1673,11 @@ TEST_F(MessageLoopTest, RepostingWmQuitDoesntStarveUpcomingNativeLoop) {
// This test ensures that application tasks are being processed by the native // This test ensures that application tasks are being processed by the native
// subpump despite the kMsgHaveWork event having already been consumed by the // subpump despite the kMsgHaveWork event having already been consumed by the
// time the subpump is entered. This is subtly enforced by // time the subpump is entered. This is subtly enforced by
// MessageLoopCurrent::ScopedNestableTaskAllower which will ScheduleWork() // MessageLoopCurrent::ScopedAllowApplicationTasksInNativeNestedLoop which
// upon construction (and if it's absent, the MessageLoop shouldn't process // will ScheduleWork() upon construction (and if it's absent, the MessageLoop
// application tasks so kMsgHaveWork is irrelevant). // shouldn't process application tasks so kMsgHaveWork is irrelevant). Note:
// Note: This test also fails prior to the fix for https://crbug.com/888559 // This test also fails prior to the fix for https://crbug.com/888559 (in
// (in fact, the last two tasks are sufficient as a regression test), probably // fact, the last two tasks are sufficient as a regression test), probably
// because of a dangling kMsgHaveWork recreating the effect from // because of a dangling kMsgHaveWork recreating the effect from
// MessageLoopTest.NativeMsgProcessingDoesntStealWmQuit. // MessageLoopTest.NativeMsgProcessingDoesntStealWmQuit.
...@@ -2065,7 +2066,8 @@ LRESULT CALLBACK TestWndProcThunk(HWND hwnd, ...@@ -2065,7 +2066,8 @@ LRESULT CALLBACK TestWndProcThunk(HWND hwnd,
case 2: case 2:
// Since we're about to enter a modal loop, tell the message loop that we // Since we're about to enter a modal loop, tell the message loop that we
// intend to nest tasks. // intend to nest tasks.
MessageLoopCurrent::ScopedNestableTaskAllower allow_nestable_tasks; MessageLoopCurrent::ScopedAllowApplicationTasksInNativeNestedLoop
allow_nestable_tasks;
bool did_run = false; bool did_run = false;
ThreadTaskRunnerHandle::Get()->PostTask( ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&EndTest, &did_run, hwnd)); FROM_HERE, base::BindOnce(&EndTest, &did_run, hwnd));
......
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