Commit 56accd7d authored by Yuta Kitamura's avatar Yuta Kitamura Committed by Commit Bot

Move contents of web_task_runner.h to platform/scheduler/.

This moves functions defined in web_task_runner.h to two different
headers in platform/scheduler/public: post_cancellable_task.h and
post_cross_thread_task.h. The header web_task_runner.h remains in
the same place, just having the forwarding #include statements, so
the existing code wouldn't break.

Minor adjustments are made to the moved code, like: (1) using
PLATFORM_EXPORT rather than BLINK_PLATFORM_EXPORT, (2) using
base::TimeDelta instead of WTF::TimeDelta, and (3) revising comments.

web_task_runner_test.cc is also moved, and is simply renamed to
post_cancellable_task_unittest.cc because it only contains tests for
PostCancellableTask().

Bug: 826203
Change-Id: I1fa4f35d278975fbca1a7c27fecf37342b0c76c8
Reviewed-on: https://chromium-review.googlesource.com/c/1339503Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Commit-Queue: Yuta Kitamura <yutak@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608732}
parent 86662386
...@@ -1366,7 +1366,6 @@ jumbo_component("platform") { ...@@ -1366,7 +1366,6 @@ jumbo_component("platform") {
"web_mouse_event.cc", "web_mouse_event.cc",
"web_mouse_wheel_event.cc", "web_mouse_wheel_event.cc",
"web_pointer_event.cc", "web_pointer_event.cc",
"web_task_runner.cc",
"web_task_runner.h", "web_task_runner.h",
"web_text_input_info.cc", "web_text_input_info.cc",
"web_thread_supporting_gc.cc", "web_thread_supporting_gc.cc",
...@@ -1806,7 +1805,6 @@ jumbo_source_set("blink_platform_unittests_sources") { ...@@ -1806,7 +1805,6 @@ jumbo_source_set("blink_platform_unittests_sources") {
"uuid_test.cc", "uuid_test.cc",
"web_icon_sizes_parser_test.cc", "web_icon_sizes_parser_test.cc",
"web_screen_info_test.cc", "web_screen_info_test.cc",
"web_task_runner_test.cc",
"web_vector_test.cc", "web_vector_test.cc",
"weborigin/known_ports_test.cc", "weborigin/known_ports_test.cc",
"weborigin/kurl_test.cc", "weborigin/kurl_test.cc",
......
...@@ -30,7 +30,7 @@ namespace blink { ...@@ -30,7 +30,7 @@ namespace blink {
template <typename FunctionType, typename... Ps> template <typename FunctionType, typename... Ps>
WTF::CrossThreadFunction<base::MakeUnboundRunType<FunctionType, Ps...>> WTF::CrossThreadFunction<base::MakeUnboundRunType<FunctionType, Ps...>>
CrossThreadBind(FunctionType function, Ps&&... parameters) { CrossThreadBind(FunctionType&& function, Ps&&... parameters) {
static_assert( static_assert(
WTF::internal::CheckGCedTypeRestrictions<std::index_sequence_for<Ps...>, WTF::internal::CheckGCedTypeRestrictions<std::index_sequence_for<Ps...>,
std::decay_t<Ps>...>::ok, std::decay_t<Ps>...>::ok,
......
...@@ -23,6 +23,8 @@ blink_platform_sources("scheduler") { ...@@ -23,6 +23,8 @@ blink_platform_sources("scheduler") {
"common/metrics_helper.h", "common/metrics_helper.h",
"common/pollable_thread_safe_flag.cc", "common/pollable_thread_safe_flag.cc",
"common/pollable_thread_safe_flag.h", "common/pollable_thread_safe_flag.h",
"common/post_cancellable_task.cc",
"common/post_cross_thread_task.cc",
"common/process_state.cc", "common/process_state.cc",
"common/process_state.h", "common/process_state.h",
"common/scheduler_helper.cc", "common/scheduler_helper.cc",
...@@ -106,6 +108,8 @@ blink_platform_sources("scheduler") { ...@@ -106,6 +108,8 @@ blink_platform_sources("scheduler") {
"public/frame_status.h", "public/frame_status.h",
"public/page_lifecycle_state.h", "public/page_lifecycle_state.h",
"public/page_scheduler.h", "public/page_scheduler.h",
"public/post_cancellable_task.h",
"public/post_cross_thread_task.h",
"public/scheduling_lifecycle_state.h", "public/scheduling_lifecycle_state.h",
"public/thread.h", "public/thread.h",
"public/thread_cpu_throttler.h", "public/thread_cpu_throttler.h",
...@@ -178,6 +182,7 @@ jumbo_source_set("unit_tests") { ...@@ -178,6 +182,7 @@ jumbo_source_set("unit_tests") {
"common/idle_canceled_delayed_task_sweeper_unittest.cc", "common/idle_canceled_delayed_task_sweeper_unittest.cc",
"common/idle_helper_unittest.cc", "common/idle_helper_unittest.cc",
"common/metrics_helper_unittest.cc", "common/metrics_helper_unittest.cc",
"common/post_cancellable_task_unittest.cc",
"common/scheduler_helper_unittest.cc", "common/scheduler_helper_unittest.cc",
"common/thread_load_tracker_unittest.cc", "common/thread_load_tracker_unittest.cc",
"common/throttling/budget_pool_unittest.cc", "common/throttling/budget_pool_unittest.cc",
......
// Copyright 2015 The Chromium Authors. All rights reserved. // Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/renderer/platform/web_task_runner.h" #include "third_party/blink/renderer/platform/scheduler/public/post_cancellable_task.h"
#include "base/bind_helpers.h" #include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h" #include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/thread_safe_ref_counted.h"
namespace blink { namespace blink {
namespace {
void RunCrossThreadClosure(CrossThreadClosure task) {
std::move(task).Run();
}
} // namespace
class TaskHandle::Runner : public WTF::ThreadSafeRefCounted<Runner> { class TaskHandle::Runner : public WTF::ThreadSafeRefCounted<Runner> {
public: public:
explicit Runner(base::OnceClosure task) explicit Runner(base::OnceClosure task)
...@@ -124,26 +117,6 @@ TaskHandle::TaskHandle(scoped_refptr<Runner> runner) ...@@ -124,26 +117,6 @@ TaskHandle::TaskHandle(scoped_refptr<Runner> runner)
DCHECK(runner_); DCHECK(runner_);
} }
// Use a custom function for base::Bind instead of WTF::Bind to
// avoid copying the closure later in the call chain. Copying the bound state
// can lead to data races with ref counted objects like StringImpl. See
// crbug.com/679915 for more details.
void PostCrossThreadTask(base::SequencedTaskRunner& task_runner,
const base::Location& location,
CrossThreadClosure task) {
task_runner.PostDelayedTask(
location, base::BindOnce(&RunCrossThreadClosure, std::move(task)),
base::TimeDelta());
}
void PostDelayedCrossThreadTask(base::SequencedTaskRunner& task_runner,
const base::Location& location,
CrossThreadClosure task,
TimeDelta delay) {
task_runner.PostDelayedTask(
location, base::BindOnce(&RunCrossThreadClosure, std::move(task)), delay);
}
TaskHandle PostCancellableTask(base::SequencedTaskRunner& task_runner, TaskHandle PostCancellableTask(base::SequencedTaskRunner& task_runner,
const base::Location& location, const base::Location& location,
base::OnceClosure task) { base::OnceClosure task) {
...@@ -159,7 +132,7 @@ TaskHandle PostCancellableTask(base::SequencedTaskRunner& task_runner, ...@@ -159,7 +132,7 @@ TaskHandle PostCancellableTask(base::SequencedTaskRunner& task_runner,
TaskHandle PostDelayedCancellableTask(base::SequencedTaskRunner& task_runner, TaskHandle PostDelayedCancellableTask(base::SequencedTaskRunner& task_runner,
const base::Location& location, const base::Location& location,
base::OnceClosure task, base::OnceClosure task,
TimeDelta delay) { base::TimeDelta delay) {
DCHECK(task_runner.RunsTasksInCurrentSequence()); DCHECK(task_runner.RunsTasksInCurrentSequence());
scoped_refptr<TaskHandle::Runner> runner = scoped_refptr<TaskHandle::Runner> runner =
base::AdoptRef(new TaskHandle::Runner(std::move(task))); base::AdoptRef(new TaskHandle::Runner(std::move(task)));
......
...@@ -2,10 +2,12 @@ ...@@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/renderer/platform/web_task_runner.h" #include "third_party/blink/renderer/platform/scheduler/public/post_cancellable_task.h"
#include "base/memory/weak_ptr.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/scheduler/test/fake_task_runner.h" #include "third_party/blink/renderer/platform/scheduler/test/fake_task_runner.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink { namespace blink {
namespace { namespace {
...@@ -54,7 +56,7 @@ TEST(WebTaskRunnerTest, PostCancellableTaskTest) { ...@@ -54,7 +56,7 @@ TEST(WebTaskRunnerTest, PostCancellableTaskTest) {
count = 0; count = 0;
handle = PostDelayedCancellableTask( handle = PostDelayedCancellableTask(
*task_runner, FROM_HERE, WTF::Bind(&Increment, WTF::Unretained(&count)), *task_runner, FROM_HERE, WTF::Bind(&Increment, WTF::Unretained(&count)),
TimeDelta::FromMilliseconds(1)); base::TimeDelta::FromMilliseconds(1));
EXPECT_EQ(0, count); EXPECT_EQ(0, count);
EXPECT_TRUE(handle.IsActive()); EXPECT_TRUE(handle.IsActive());
task_runner->RunUntilIdle(); task_runner->RunUntilIdle();
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "base/bind.h"
namespace blink {
namespace {
void RunCrossThreadClosure(WTF::CrossThreadClosure task) {
std::move(task).Run();
}
} // namespace
// In those functions, we must use plain base::BindOnce() because:
//
// * WTF::Bind() does thread checks which isn't compatible with our use case.
// * CrossThreadBind() returns WTF::CrossThreadFunction which isn't
// convertible to base::OnceClosure (this is actually a chicken-and-egg;
// we need base::BindOnce() as an escape hatch).
void PostCrossThreadTask(base::SequencedTaskRunner& task_runner,
const base::Location& location,
WTF::CrossThreadClosure task) {
task_runner.PostDelayedTask(
location, base::BindOnce(&RunCrossThreadClosure, std::move(task)),
base::TimeDelta());
}
void PostDelayedCrossThreadTask(base::SequencedTaskRunner& task_runner,
const base::Location& location,
WTF::CrossThreadClosure task,
base::TimeDelta delay) {
task_runner.PostDelayedTask(
location, base::BindOnce(&RunCrossThreadClosure, std::move(task)), delay);
}
} // namespace blink
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_POST_CANCELLABLE_TASK_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_POST_CANCELLABLE_TASK_H_
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequenced_task_runner.h"
#include "base/time/time.h"
#include "third_party/blink/renderer/platform/platform_export.h"
namespace blink {
// TaskHandle is associated to a task posted by PostCancellableTask() or
// PostCancellableDelayedTask() and cancels the associated task on
// TaskHandle::cancel() call or on TaskHandle destruction.
class PLATFORM_EXPORT TaskHandle {
public:
class Runner;
TaskHandle();
~TaskHandle();
TaskHandle(TaskHandle&&);
TaskHandle& operator=(TaskHandle&&);
// Returns true if the task will run later. Returns false if the task is
// cancelled or the task is run already.
// This function is not thread safe. Call this on the thread that has posted
// the task.
bool IsActive() const;
// Cancels the task invocation. Do nothing if the task is cancelled or run
// already.
// This function is not thread safe. Call this on the thread that has posted
// the task.
void Cancel();
private:
TaskHandle(const TaskHandle&) = delete;
TaskHandle& operator=(const TaskHandle&) = delete;
friend PLATFORM_EXPORT TaskHandle
PostCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure) WARN_UNUSED_RESULT;
friend PLATFORM_EXPORT TaskHandle
PostDelayedCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure,
base::TimeDelta delay) WARN_UNUSED_RESULT;
explicit TaskHandle(scoped_refptr<Runner>);
scoped_refptr<Runner> runner_;
};
// For same-thread cancellable task posting. Returns a TaskHandle object for
// cancellation.
PLATFORM_EXPORT TaskHandle PostCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure)
WARN_UNUSED_RESULT;
PLATFORM_EXPORT TaskHandle
PostDelayedCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure,
base::TimeDelta delay) WARN_UNUSED_RESULT;
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_POST_CANCELLABLE_TASK_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_POST_CROSS_THREAD_TASK_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_POST_CROSS_THREAD_TASK_H_
#include "base/location.h"
#include "base/sequenced_task_runner.h"
#include "base/time/time.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink {
// For cross-thread posting. Can be called from any thread.
PLATFORM_EXPORT void PostCrossThreadTask(base::SequencedTaskRunner&,
const base::Location&,
WTF::CrossThreadClosure);
PLATFORM_EXPORT void PostDelayedCrossThreadTask(base::SequencedTaskRunner&,
const base::Location&,
WTF::CrossThreadClosure,
base::TimeDelta delay);
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_POST_CROSS_THREAD_TASK_H_
...@@ -5,80 +5,10 @@ ...@@ -5,80 +5,10 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_TASK_RUNNER_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_TASK_RUNNER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_TASK_RUNNER_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_TASK_RUNNER_H_
#include <memory> #include "third_party/blink/renderer/platform/scheduler/public/post_cancellable_task.h"
#include "base/callback.h" #include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "base/location.h"
#include "base/sequenced_task_runner.h"
#include "third_party/blink/public/platform/web_common.h"
#include "third_party/blink/renderer/platform/wtf/compiler.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/time.h"
namespace blink { // The contents of this file are moved to the above headers. For future
// includes, use either of them.
// TaskHandle is associated to a task posted by PostCancellableTask() or
// PostCancellableDelayedTask() and cancels the associated task on
// TaskHandle::cancel() call or on TaskHandle destruction.
class BLINK_PLATFORM_EXPORT TaskHandle {
public:
// Returns true if the task will run later. Returns false if the task is
// cancelled or the task is run already.
// This function is not thread safe. Call this on the thread that has posted
// the task.
bool IsActive() const;
// Cancels the task invocation. Do nothing if the task is cancelled or run
// already.
// This function is not thread safe. Call this on the thread that has posted
// the task.
void Cancel();
TaskHandle();
~TaskHandle();
TaskHandle(TaskHandle&&);
TaskHandle& operator=(TaskHandle&&);
class Runner;
private:
friend BLINK_PLATFORM_EXPORT WARN_UNUSED_RESULT TaskHandle
PostCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure);
friend BLINK_PLATFORM_EXPORT WARN_UNUSED_RESULT TaskHandle
PostDelayedCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure,
TimeDelta delay);
explicit TaskHandle(scoped_refptr<Runner>);
scoped_refptr<Runner> runner_;
};
// For cross-thread posting. Can be called from any thread.
BLINK_PLATFORM_EXPORT void PostCrossThreadTask(base::SequencedTaskRunner&,
const base::Location&,
CrossThreadClosure);
BLINK_PLATFORM_EXPORT void PostDelayedCrossThreadTask(
base::SequencedTaskRunner&,
const base::Location&,
CrossThreadClosure,
TimeDelta delay);
// For same-thread cancellable task posting. Returns a TaskHandle object for
// cancellation.
BLINK_PLATFORM_EXPORT WARN_UNUSED_RESULT TaskHandle
PostCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure);
BLINK_PLATFORM_EXPORT WARN_UNUSED_RESULT TaskHandle
PostDelayedCancellableTask(base::SequencedTaskRunner&,
const base::Location&,
base::OnceClosure,
TimeDelta delay);
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_TASK_RUNNER_H_ #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WEB_TASK_RUNNER_H_
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