From c0ce454868e21a2a694f2c89c1246e4aa4f71d26 Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Tue, 9 Aug 2022 11:24:52 +0200 Subject: [PATCH] Delete QueuedTask and ToQueuedTask as no longer needed Bug: webrtc:14245 Change-Id: I4b36c8d6f0709202e01d22386644c20cad58450f Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/269403 Reviewed-by: Tomas Gunnarsson Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#37718} --- BUILD.gn | 1 - api/task_queue/BUILD.gn | 22 --- api/task_queue/queued_task.h | 32 ---- api/task_queue/task_queue_base.cc | 66 -------- api/task_queue/task_queue_base.h | 48 +----- api/task_queue/to_queued_task.h | 111 ------------- api/task_queue/to_queued_task_unittest.cc | 148 ------------------ rtc_base/task_queue.h | 1 - rtc_base/task_utils/BUILD.gn | 10 -- .../task_utils/pending_task_safety_flag.h | 16 -- rtc_base/task_utils/to_queued_task.h | 16 -- 11 files changed, 3 insertions(+), 468 deletions(-) delete mode 100644 api/task_queue/queued_task.h delete mode 100644 api/task_queue/to_queued_task.h delete mode 100644 api/task_queue/to_queued_task_unittest.cc delete mode 100644 rtc_base/task_utils/pending_task_safety_flag.h delete mode 100644 rtc_base/task_utils/to_queued_task.h diff --git a/BUILD.gn b/BUILD.gn index 04db39abd0..31ee99b9d0 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -557,7 +557,6 @@ if (rtc_include_tests && !build_with_chromium) { "api/audio_codecs/test:audio_codecs_api_unittests", "api/numerics:numerics_unittests", "api/task_queue:pending_task_safety_flag_unittests", - "api/task_queue:to_queued_task_unittests", "api/transport:stun_unittest", "api/video/test:rtc_api_video_unittests", "api/video_codecs/test:video_codecs_api_unittests", diff --git a/api/task_queue/BUILD.gn b/api/task_queue/BUILD.gn index 62a4a61595..8daa01d26b 100644 --- a/api/task_queue/BUILD.gn +++ b/api/task_queue/BUILD.gn @@ -11,7 +11,6 @@ import("../../webrtc.gni") rtc_library("task_queue") { visibility = [ "*" ] public = [ - "queued_task.h", "task_queue_base.h", "task_queue_factory.h", ] @@ -99,15 +98,6 @@ rtc_library("default_task_queue_factory") { } } -rtc_source_set("to_queued_task") { - visibility = [ "*" ] - sources = [ "to_queued_task.h" ] - deps = [ - ":task_queue", - "../../api/task_queue:pending_task_safety_flag", - ] -} - rtc_library("pending_task_safety_flag") { visibility = [ "*" ] sources = [ @@ -135,23 +125,11 @@ if (rtc_include_tests) { ] } - rtc_library("to_queued_task_unittests") { - testonly = true - sources = [ "to_queued_task_unittest.cc" ] - deps = [ - ":to_queued_task", - "../../api/task_queue", - "../../test:test_support", - ] - absl_deps = [ "//third_party/abseil-cpp/absl/memory" ] - } - rtc_library("pending_task_safety_flag_unittests") { testonly = true sources = [ "pending_task_safety_flag_unittest.cc" ] deps = [ ":pending_task_safety_flag", - ":to_queued_task", "../../rtc_base:logging", "../../rtc_base:rtc_event", "../../rtc_base:rtc_task_queue", diff --git a/api/task_queue/queued_task.h b/api/task_queue/queued_task.h deleted file mode 100644 index 27a5eda5a5..0000000000 --- a/api/task_queue/queued_task.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2018 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ -#ifndef API_TASK_QUEUE_QUEUED_TASK_H_ -#define API_TASK_QUEUE_QUEUED_TASK_H_ - -namespace webrtc { - -// Base interface for asynchronously executed tasks. -// The interface basically consists of a single function, Run(), that executes -// on the target queue. For more details see the Run() method and TaskQueue. -class QueuedTask { - public: - virtual ~QueuedTask() = default; - - // Main routine that will run when the task is executed on the desired queue. - // The task should return `true` to indicate that it should be deleted or - // `false` to indicate that the queue should consider ownership of the task - // having been transferred. Returning `false` can be useful if a task has - // re-posted itself to a different queue or is otherwise being re-used. - virtual bool Run() = 0; -}; - -} // namespace webrtc - -#endif // API_TASK_QUEUE_QUEUED_TASK_H_ diff --git a/api/task_queue/task_queue_base.cc b/api/task_queue/task_queue_base.cc index 11231a0b44..ecdc7f7691 100644 --- a/api/task_queue/task_queue_base.cc +++ b/api/task_queue/task_queue_base.cc @@ -79,69 +79,3 @@ TaskQueueBase::CurrentTaskQueueSetter::~CurrentTaskQueueSetter() { #else #error Unsupported platform #endif - -// Functions to support transition from std::unique_ptr -// representation of a task to absl::AnyInvocable representation. In -// the base interface older and newer functions call each other. This way -// TaskQueue would work when classes derived from TaskQueueBase implements -// either old set of Post functions (before the transition) or new set of Post -// functions. Thus callers of the interface can be updated independently of all -// of the implementations of that TaskQueueBase interface. - -// TODO(bugs.webrtc.org/14245): Delete these functions when transition is -// complete. -namespace webrtc { -namespace { -class Task : public QueuedTask { - public: - explicit Task(absl::AnyInvocable task) : task_(std::move(task)) {} - ~Task() override = default; - - bool Run() override { - std::move(task_)(); - return true; - } - - private: - absl::AnyInvocable task_; -}; - -std::unique_ptr ToLegacy(absl::AnyInvocable task) { - return std::make_unique(std::move(task)); -} - -absl::AnyInvocable FromLegacy(std::unique_ptr task) { - return [task = std::move(task)]() mutable { - if (!task->Run()) { - task.release(); - } - }; -} - -} // namespace - -void TaskQueueBase::PostTask(std::unique_ptr task) { - PostTask(FromLegacy(std::move(task))); -} - -void TaskQueueBase::PostTask(absl::AnyInvocable task) { - PostTask(ToLegacy(std::move(task))); -} - -void TaskQueueBase::PostDelayedTask(std::unique_ptr task, - uint32_t milliseconds) { - PostDelayedTask(FromLegacy(std::move(task)), TimeDelta::Millis(milliseconds)); -} - -void TaskQueueBase::PostDelayedTask(absl::AnyInvocable task, - TimeDelta delay) { - PostDelayedTask(ToLegacy(std::move(task)), delay.ms()); -} - -void TaskQueueBase::PostDelayedHighPrecisionTask( - absl::AnyInvocable task, - TimeDelta delay) { - PostDelayedHighPrecisionTask(ToLegacy(std::move(task)), delay.ms()); -} - -} // namespace webrtc diff --git a/api/task_queue/task_queue_base.h b/api/task_queue/task_queue_base.h index 870637c9b5..a2cff9c738 100644 --- a/api/task_queue/task_queue_base.h +++ b/api/task_queue/task_queue_base.h @@ -14,7 +14,6 @@ #include #include "absl/functional/any_invocable.h" -#include "api/task_queue/queued_task.h" #include "api/units/time_delta.h" #include "rtc_base/system/rtc_export.h" #include "rtc_base/thread_annotations.h" @@ -57,14 +56,7 @@ class RTC_LOCKABLE RTC_EXPORT TaskQueueBase { // This may vary from one implementation to the next so assumptions about // lifetimes of pending tasks should not be made. // May be called on any thread or task queue, including this task queue. - // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all - // derived classes. - virtual void PostTask(absl::AnyInvocable task); - - // Deprecated, use PostTask variant above in new code. - // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the - // function above. - virtual void PostTask(std::unique_ptr task); + virtual void PostTask(absl::AnyInvocable task) = 0; // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever // possible. @@ -89,16 +81,8 @@ class RTC_LOCKABLE RTC_EXPORT TaskQueueBase { // https://crbug.com/webrtc/13583 for more information. // // May be called on any thread or task queue, including this task queue. - // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all - // derived classes. virtual void PostDelayedTask(absl::AnyInvocable task, - TimeDelta delay); - - // Deprecated, use PostDelayedTask variant above in new code. - // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the - // function above. - virtual void PostDelayedTask(std::unique_ptr task, - uint32_t milliseconds); + TimeDelta delay) = 0; // Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever // possible. @@ -116,18 +100,8 @@ class RTC_LOCKABLE RTC_EXPORT TaskQueueBase { // battery, when the timer precision can be as poor as 15 ms. // // May be called on any thread or task queue, including this task queue. - // TODO(bugs.webrtc.org/14245): Make pure virtual when implemented in all - // derived classes. virtual void PostDelayedHighPrecisionTask(absl::AnyInvocable task, - TimeDelta delay); - - // Deprecated, use `PostDelayedHighPrecisionTask` variant above in new code. - // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the - // function above. - virtual void PostDelayedHighPrecisionTask(std::unique_ptr task, - uint32_t milliseconds) { - PostDelayedTask(std::move(task), milliseconds); - } + TimeDelta delay) = 0; // As specified by `precision`, calls either PostDelayedTask() or // PostDelayedHighPrecisionTask(). @@ -144,22 +118,6 @@ class RTC_LOCKABLE RTC_EXPORT TaskQueueBase { } } - // Deprecated, use `PostDelayedTaskWithPrecision` variant above in new code. - // TODO(bugs.webrtc.org/14245): Delete when all usage is updated to the - // function above. - void PostDelayedTaskWithPrecision(DelayPrecision precision, - std::unique_ptr task, - uint32_t milliseconds) { - switch (precision) { - case DelayPrecision::kLow: - PostDelayedTask(std::move(task), milliseconds); - break; - case DelayPrecision::kHigh: - PostDelayedHighPrecisionTask(std::move(task), milliseconds); - break; - } - } - // Returns the task queue that is running the current thread. // Returns nullptr if this thread is not associated with any task queue. // May be called on any thread or task queue, including this task queue. diff --git a/api/task_queue/to_queued_task.h b/api/task_queue/to_queued_task.h deleted file mode 100644 index 658eecc04a..0000000000 --- a/api/task_queue/to_queued_task.h +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright 2019 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef API_TASK_QUEUE_TO_QUEUED_TASK_H_ -#define API_TASK_QUEUE_TO_QUEUED_TASK_H_ - -#include -#include -#include - -#include "api/task_queue/pending_task_safety_flag.h" -#include "api/task_queue/queued_task.h" - -namespace webrtc { -namespace webrtc_new_closure_impl { -// Simple implementation of QueuedTask for use with lambdas. -template -class ClosureTask : public QueuedTask { - public: - explicit ClosureTask(Closure&& closure) - : closure_(std::forward(closure)) {} - - private: - bool Run() override { - closure_(); - return true; - } - - typename std::decay::type closure_; -}; - -template -class SafetyClosureTask : public QueuedTask { - public: - explicit SafetyClosureTask(rtc::scoped_refptr safety, - Closure&& closure) - : closure_(std::forward(closure)), - safety_flag_(std::move(safety)) {} - - private: - bool Run() override { - if (safety_flag_->alive()) - closure_(); - return true; - } - - typename std::decay::type closure_; - rtc::scoped_refptr safety_flag_; -}; - -// Extends ClosureTask to also allow specifying cleanup code. -// This is useful when using lambdas if guaranteeing cleanup, even if a task -// was dropped (queue is too full), is required. -template -class ClosureTaskWithCleanup : public ClosureTask { - public: - ClosureTaskWithCleanup(Closure&& closure, Cleanup&& cleanup) - : ClosureTask(std::forward(closure)), - cleanup_(std::forward(cleanup)) {} - ~ClosureTaskWithCleanup() override { cleanup_(); } - - private: - typename std::decay::type cleanup_; -}; -} // namespace webrtc_new_closure_impl - -// Convenience function to construct closures that can be passed directly -// to methods that support std::unique_ptr but not template -// based parameters. -template -std::unique_ptr ToQueuedTask(Closure&& closure) { - return std::make_unique>( - std::forward(closure)); -} - -template -std::unique_ptr ToQueuedTask( - rtc::scoped_refptr safety, - Closure&& closure) { - return std::make_unique>( - std::move(safety), std::forward(closure)); -} - -template -std::unique_ptr ToQueuedTask(const ScopedTaskSafety& safety, - Closure&& closure) { - return ToQueuedTask(safety.flag(), std::forward(closure)); -} - -template ::type>::type, - ScopedTaskSafety>::value>::type* = nullptr> -std::unique_ptr ToQueuedTask(Closure&& closure, Cleanup&& cleanup) { - return std::make_unique< - webrtc_new_closure_impl::ClosureTaskWithCleanup>( - std::forward(closure), std::forward(cleanup)); -} - -} // namespace webrtc - -#endif // API_TASK_QUEUE_TO_QUEUED_TASK_H_ diff --git a/api/task_queue/to_queued_task_unittest.cc b/api/task_queue/to_queued_task_unittest.cc deleted file mode 100644 index aa11041801..0000000000 --- a/api/task_queue/to_queued_task_unittest.cc +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright 2019 The WebRTC project authors. All Rights Reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "api/task_queue/to_queued_task.h" - -#include - -#include "absl/memory/memory.h" -#include "api/task_queue/queued_task.h" -#include "test/gmock.h" -#include "test/gtest.h" - -namespace webrtc { -namespace { - -using ::testing::InSequence; -using ::testing::MockFunction; - -void RunTask(std::unique_ptr task) { - // Simulate how task queue suppose to run tasks. - QueuedTask* raw = task.release(); - if (raw->Run()) - delete raw; -} - -TEST(ToQueuedTaskTest, AcceptsLambda) { - bool run = false; - std::unique_ptr task = ToQueuedTask([&run] { run = true; }); - EXPECT_FALSE(run); - RunTask(std::move(task)); - EXPECT_TRUE(run); -} - -TEST(ToQueuedTaskTest, AcceptsCopyableClosure) { - struct CopyableClosure { - CopyableClosure(int* num_copies, int* num_moves, int* num_runs) - : num_copies(num_copies), num_moves(num_moves), num_runs(num_runs) {} - CopyableClosure(const CopyableClosure& other) - : num_copies(other.num_copies), - num_moves(other.num_moves), - num_runs(other.num_runs) { - ++*num_copies; - } - CopyableClosure(CopyableClosure&& other) - : num_copies(other.num_copies), - num_moves(other.num_moves), - num_runs(other.num_runs) { - ++*num_moves; - } - void operator()() { ++*num_runs; } - - int* num_copies; - int* num_moves; - int* num_runs; - }; - - int num_copies = 0; - int num_moves = 0; - int num_runs = 0; - - std::unique_ptr task; - { - CopyableClosure closure(&num_copies, &num_moves, &num_runs); - task = ToQueuedTask(closure); - // Destroy closure to check with msan task has own copy. - } - EXPECT_EQ(num_copies, 1); - EXPECT_EQ(num_runs, 0); - RunTask(std::move(task)); - EXPECT_EQ(num_copies, 1); - EXPECT_EQ(num_moves, 0); - EXPECT_EQ(num_runs, 1); -} - -TEST(ToQueuedTaskTest, AcceptsMoveOnlyClosure) { - struct MoveOnlyClosure { - MoveOnlyClosure(int* num_moves, std::function trigger) - : num_moves(num_moves), trigger(std::move(trigger)) {} - MoveOnlyClosure(const MoveOnlyClosure&) = delete; - MoveOnlyClosure(MoveOnlyClosure&& other) - : num_moves(other.num_moves), trigger(std::move(other.trigger)) { - ++*num_moves; - } - void operator()() { trigger(); } - - int* num_moves; - std::function trigger; - }; - - int num_moves = 0; - MockFunction run; - - auto task = ToQueuedTask(MoveOnlyClosure(&num_moves, run.AsStdFunction())); - EXPECT_EQ(num_moves, 1); - EXPECT_CALL(run, Call); - RunTask(std::move(task)); - EXPECT_EQ(num_moves, 1); -} - -TEST(ToQueuedTaskTest, AcceptsMoveOnlyCleanup) { - struct MoveOnlyClosure { - MoveOnlyClosure(const MoveOnlyClosure&) = delete; - MoveOnlyClosure(MoveOnlyClosure&&) = default; - void operator()() { trigger(); } - - std::function trigger; - }; - - MockFunction run; - MockFunction cleanup; - - auto task = ToQueuedTask(MoveOnlyClosure{run.AsStdFunction()}, - MoveOnlyClosure{cleanup.AsStdFunction()}); - - // Expect run closure to complete before cleanup closure. - InSequence in_sequence; - EXPECT_CALL(run, Call); - EXPECT_CALL(cleanup, Call); - RunTask(std::move(task)); -} - -TEST(ToQueuedTaskTest, PendingTaskSafetyFlag) { - rtc::scoped_refptr flag = - PendingTaskSafetyFlag::Create(); - - int count = 0; - // Create two identical tasks that increment the `count`. - auto task1 = ToQueuedTask(flag, [&count]() { ++count; }); - auto task2 = ToQueuedTask(flag, [&count]() { ++count; }); - - EXPECT_EQ(0, count); - RunTask(std::move(task1)); - EXPECT_EQ(1, count); - flag->SetNotAlive(); - // Now task2 should actually not run. - RunTask(std::move(task2)); - EXPECT_EQ(1, count); -} - -} // namespace -} // namespace webrtc diff --git a/rtc_base/task_queue.h b/rtc_base/task_queue.h index d0c05515b0..0ef0ffe992 100644 --- a/rtc_base/task_queue.h +++ b/rtc_base/task_queue.h @@ -18,7 +18,6 @@ #include "absl/functional/any_invocable.h" #include "absl/memory/memory.h" -#include "api/task_queue/queued_task.h" #include "api/task_queue/task_queue_base.h" #include "api/task_queue/task_queue_factory.h" #include "rtc_base/system/rtc_export.h" diff --git a/rtc_base/task_utils/BUILD.gn b/rtc_base/task_utils/BUILD.gn index fef30eb0e2..1b3a3b5fbc 100644 --- a/rtc_base/task_utils/BUILD.gn +++ b/rtc_base/task_utils/BUILD.gn @@ -26,16 +26,6 @@ rtc_library("repeating_task") { absl_deps = [ "//third_party/abseil-cpp/absl/functional:any_invocable" ] } -rtc_library("pending_task_safety_flag") { - sources = [ "pending_task_safety_flag.h" ] - deps = [ "../../api/task_queue:pending_task_safety_flag" ] -} - -rtc_source_set("to_queued_task") { - sources = [ "to_queued_task.h" ] - deps = [ "../../api/task_queue:to_queued_task" ] -} - if (rtc_include_tests) { rtc_library("repeating_task_unittests") { testonly = true diff --git a/rtc_base/task_utils/pending_task_safety_flag.h b/rtc_base/task_utils/pending_task_safety_flag.h deleted file mode 100644 index 83212760b5..0000000000 --- a/rtc_base/task_utils/pending_task_safety_flag.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2020 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_ -#define RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_ - -#include "api/task_queue/pending_task_safety_flag.h" - -#endif // RTC_BASE_TASK_UTILS_PENDING_TASK_SAFETY_FLAG_H_ diff --git a/rtc_base/task_utils/to_queued_task.h b/rtc_base/task_utils/to_queued_task.h deleted file mode 100644 index 049fb018d7..0000000000 --- a/rtc_base/task_utils/to_queued_task.h +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2019 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_ -#define RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_ - -#include "api/task_queue/to_queued_task.h" - -#endif // RTC_BASE_TASK_UTILS_TO_QUEUED_TASK_H_