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 <tommi@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/main@{#37718}
This commit is contained in:
parent
9e2f0c2e26
commit
c0ce454868
1
BUILD.gn
1
BUILD.gn
@ -557,7 +557,6 @@ if (rtc_include_tests && !build_with_chromium) {
|
|||||||
"api/audio_codecs/test:audio_codecs_api_unittests",
|
"api/audio_codecs/test:audio_codecs_api_unittests",
|
||||||
"api/numerics:numerics_unittests",
|
"api/numerics:numerics_unittests",
|
||||||
"api/task_queue:pending_task_safety_flag_unittests",
|
"api/task_queue:pending_task_safety_flag_unittests",
|
||||||
"api/task_queue:to_queued_task_unittests",
|
|
||||||
"api/transport:stun_unittest",
|
"api/transport:stun_unittest",
|
||||||
"api/video/test:rtc_api_video_unittests",
|
"api/video/test:rtc_api_video_unittests",
|
||||||
"api/video_codecs/test:video_codecs_api_unittests",
|
"api/video_codecs/test:video_codecs_api_unittests",
|
||||||
|
|||||||
@ -11,7 +11,6 @@ import("../../webrtc.gni")
|
|||||||
rtc_library("task_queue") {
|
rtc_library("task_queue") {
|
||||||
visibility = [ "*" ]
|
visibility = [ "*" ]
|
||||||
public = [
|
public = [
|
||||||
"queued_task.h",
|
|
||||||
"task_queue_base.h",
|
"task_queue_base.h",
|
||||||
"task_queue_factory.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") {
|
rtc_library("pending_task_safety_flag") {
|
||||||
visibility = [ "*" ]
|
visibility = [ "*" ]
|
||||||
sources = [
|
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") {
|
rtc_library("pending_task_safety_flag_unittests") {
|
||||||
testonly = true
|
testonly = true
|
||||||
sources = [ "pending_task_safety_flag_unittest.cc" ]
|
sources = [ "pending_task_safety_flag_unittest.cc" ]
|
||||||
deps = [
|
deps = [
|
||||||
":pending_task_safety_flag",
|
":pending_task_safety_flag",
|
||||||
":to_queued_task",
|
|
||||||
"../../rtc_base:logging",
|
"../../rtc_base:logging",
|
||||||
"../../rtc_base:rtc_event",
|
"../../rtc_base:rtc_event",
|
||||||
"../../rtc_base:rtc_task_queue",
|
"../../rtc_base:rtc_task_queue",
|
||||||
|
|||||||
@ -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_
|
|
||||||
@ -79,69 +79,3 @@ TaskQueueBase::CurrentTaskQueueSetter::~CurrentTaskQueueSetter() {
|
|||||||
#else
|
#else
|
||||||
#error Unsupported platform
|
#error Unsupported platform
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Functions to support transition from std::unique_ptr<QueuedTask>
|
|
||||||
// representation of a task to absl::AnyInvocable<void() &&> 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<void() &&> task) : task_(std::move(task)) {}
|
|
||||||
~Task() override = default;
|
|
||||||
|
|
||||||
bool Run() override {
|
|
||||||
std::move(task_)();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
absl::AnyInvocable<void() &&> task_;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::unique_ptr<QueuedTask> ToLegacy(absl::AnyInvocable<void() &&> task) {
|
|
||||||
return std::make_unique<Task>(std::move(task));
|
|
||||||
}
|
|
||||||
|
|
||||||
absl::AnyInvocable<void() &&> FromLegacy(std::unique_ptr<QueuedTask> task) {
|
|
||||||
return [task = std::move(task)]() mutable {
|
|
||||||
if (!task->Run()) {
|
|
||||||
task.release();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void TaskQueueBase::PostTask(std::unique_ptr<QueuedTask> task) {
|
|
||||||
PostTask(FromLegacy(std::move(task)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void TaskQueueBase::PostTask(absl::AnyInvocable<void() &&> task) {
|
|
||||||
PostTask(ToLegacy(std::move(task)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void TaskQueueBase::PostDelayedTask(std::unique_ptr<QueuedTask> task,
|
|
||||||
uint32_t milliseconds) {
|
|
||||||
PostDelayedTask(FromLegacy(std::move(task)), TimeDelta::Millis(milliseconds));
|
|
||||||
}
|
|
||||||
|
|
||||||
void TaskQueueBase::PostDelayedTask(absl::AnyInvocable<void() &&> task,
|
|
||||||
TimeDelta delay) {
|
|
||||||
PostDelayedTask(ToLegacy(std::move(task)), delay.ms());
|
|
||||||
}
|
|
||||||
|
|
||||||
void TaskQueueBase::PostDelayedHighPrecisionTask(
|
|
||||||
absl::AnyInvocable<void() &&> task,
|
|
||||||
TimeDelta delay) {
|
|
||||||
PostDelayedHighPrecisionTask(ToLegacy(std::move(task)), delay.ms());
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace webrtc
|
|
||||||
|
|||||||
@ -14,7 +14,6 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "absl/functional/any_invocable.h"
|
#include "absl/functional/any_invocable.h"
|
||||||
#include "api/task_queue/queued_task.h"
|
|
||||||
#include "api/units/time_delta.h"
|
#include "api/units/time_delta.h"
|
||||||
#include "rtc_base/system/rtc_export.h"
|
#include "rtc_base/system/rtc_export.h"
|
||||||
#include "rtc_base/thread_annotations.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
|
// This may vary from one implementation to the next so assumptions about
|
||||||
// lifetimes of pending tasks should not be made.
|
// lifetimes of pending tasks should not be made.
|
||||||
// May be called on any thread or task queue, including this task queue.
|
// 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
|
virtual void PostTask(absl::AnyInvocable<void() &&> task) = 0;
|
||||||
// derived classes.
|
|
||||||
virtual void PostTask(absl::AnyInvocable<void() &&> 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<QueuedTask> task);
|
|
||||||
|
|
||||||
// Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
|
// Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
|
||||||
// possible.
|
// possible.
|
||||||
@ -89,16 +81,8 @@ class RTC_LOCKABLE RTC_EXPORT TaskQueueBase {
|
|||||||
// https://crbug.com/webrtc/13583 for more information.
|
// https://crbug.com/webrtc/13583 for more information.
|
||||||
//
|
//
|
||||||
// May be called on any thread or task queue, including this task queue.
|
// 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<void() &&> task,
|
virtual void PostDelayedTask(absl::AnyInvocable<void() &&> task,
|
||||||
TimeDelta delay);
|
TimeDelta delay) = 0;
|
||||||
|
|
||||||
// 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<QueuedTask> task,
|
|
||||||
uint32_t milliseconds);
|
|
||||||
|
|
||||||
// Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
|
// Prefer PostDelayedTask() over PostDelayedHighPrecisionTask() whenever
|
||||||
// possible.
|
// possible.
|
||||||
@ -116,18 +100,8 @@ class RTC_LOCKABLE RTC_EXPORT TaskQueueBase {
|
|||||||
// battery, when the timer precision can be as poor as 15 ms.
|
// 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.
|
// 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<void() &&> task,
|
virtual void PostDelayedHighPrecisionTask(absl::AnyInvocable<void() &&> task,
|
||||||
TimeDelta delay);
|
TimeDelta delay) = 0;
|
||||||
|
|
||||||
// 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<QueuedTask> task,
|
|
||||||
uint32_t milliseconds) {
|
|
||||||
PostDelayedTask(std::move(task), milliseconds);
|
|
||||||
}
|
|
||||||
|
|
||||||
// As specified by `precision`, calls either PostDelayedTask() or
|
// As specified by `precision`, calls either PostDelayedTask() or
|
||||||
// PostDelayedHighPrecisionTask().
|
// 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<QueuedTask> 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 the task queue that is running the current thread.
|
||||||
// Returns nullptr if this thread is not associated with any task queue.
|
// 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.
|
// May be called on any thread or task queue, including this task queue.
|
||||||
|
|||||||
@ -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 <memory>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#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 <typename Closure>
|
|
||||||
class ClosureTask : public QueuedTask {
|
|
||||||
public:
|
|
||||||
explicit ClosureTask(Closure&& closure)
|
|
||||||
: closure_(std::forward<Closure>(closure)) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool Run() override {
|
|
||||||
closure_();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
typename std::decay<Closure>::type closure_;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Closure>
|
|
||||||
class SafetyClosureTask : public QueuedTask {
|
|
||||||
public:
|
|
||||||
explicit SafetyClosureTask(rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
|
|
||||||
Closure&& closure)
|
|
||||||
: closure_(std::forward<Closure>(closure)),
|
|
||||||
safety_flag_(std::move(safety)) {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool Run() override {
|
|
||||||
if (safety_flag_->alive())
|
|
||||||
closure_();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
typename std::decay<Closure>::type closure_;
|
|
||||||
rtc::scoped_refptr<PendingTaskSafetyFlag> 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 <typename Closure, typename Cleanup>
|
|
||||||
class ClosureTaskWithCleanup : public ClosureTask<Closure> {
|
|
||||||
public:
|
|
||||||
ClosureTaskWithCleanup(Closure&& closure, Cleanup&& cleanup)
|
|
||||||
: ClosureTask<Closure>(std::forward<Closure>(closure)),
|
|
||||||
cleanup_(std::forward<Cleanup>(cleanup)) {}
|
|
||||||
~ClosureTaskWithCleanup() override { cleanup_(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
typename std::decay<Cleanup>::type cleanup_;
|
|
||||||
};
|
|
||||||
} // namespace webrtc_new_closure_impl
|
|
||||||
|
|
||||||
// Convenience function to construct closures that can be passed directly
|
|
||||||
// to methods that support std::unique_ptr<QueuedTask> but not template
|
|
||||||
// based parameters.
|
|
||||||
template <typename Closure>
|
|
||||||
std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure) {
|
|
||||||
return std::make_unique<webrtc_new_closure_impl::ClosureTask<Closure>>(
|
|
||||||
std::forward<Closure>(closure));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Closure>
|
|
||||||
std::unique_ptr<QueuedTask> ToQueuedTask(
|
|
||||||
rtc::scoped_refptr<PendingTaskSafetyFlag> safety,
|
|
||||||
Closure&& closure) {
|
|
||||||
return std::make_unique<webrtc_new_closure_impl::SafetyClosureTask<Closure>>(
|
|
||||||
std::move(safety), std::forward<Closure>(closure));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Closure>
|
|
||||||
std::unique_ptr<QueuedTask> ToQueuedTask(const ScopedTaskSafety& safety,
|
|
||||||
Closure&& closure) {
|
|
||||||
return ToQueuedTask(safety.flag(), std::forward<Closure>(closure));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Closure,
|
|
||||||
typename Cleanup,
|
|
||||||
typename std::enable_if<!std::is_same<
|
|
||||||
typename std::remove_const<
|
|
||||||
typename std::remove_reference<Closure>::type>::type,
|
|
||||||
ScopedTaskSafety>::value>::type* = nullptr>
|
|
||||||
std::unique_ptr<QueuedTask> ToQueuedTask(Closure&& closure, Cleanup&& cleanup) {
|
|
||||||
return std::make_unique<
|
|
||||||
webrtc_new_closure_impl::ClosureTaskWithCleanup<Closure, Cleanup>>(
|
|
||||||
std::forward<Closure>(closure), std::forward<Cleanup>(cleanup));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace webrtc
|
|
||||||
|
|
||||||
#endif // API_TASK_QUEUE_TO_QUEUED_TASK_H_
|
|
||||||
@ -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 <memory>
|
|
||||||
|
|
||||||
#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<QueuedTask> 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<QueuedTask> 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<QueuedTask> 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<void()> 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<void()> trigger;
|
|
||||||
};
|
|
||||||
|
|
||||||
int num_moves = 0;
|
|
||||||
MockFunction<void()> 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<void()> trigger;
|
|
||||||
};
|
|
||||||
|
|
||||||
MockFunction<void()> run;
|
|
||||||
MockFunction<void()> 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<PendingTaskSafetyFlag> 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
|
|
||||||
@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
#include "absl/functional/any_invocable.h"
|
#include "absl/functional/any_invocable.h"
|
||||||
#include "absl/memory/memory.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_base.h"
|
||||||
#include "api/task_queue/task_queue_factory.h"
|
#include "api/task_queue/task_queue_factory.h"
|
||||||
#include "rtc_base/system/rtc_export.h"
|
#include "rtc_base/system/rtc_export.h"
|
||||||
|
|||||||
@ -26,16 +26,6 @@ rtc_library("repeating_task") {
|
|||||||
absl_deps = [ "//third_party/abseil-cpp/absl/functional:any_invocable" ]
|
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) {
|
if (rtc_include_tests) {
|
||||||
rtc_library("repeating_task_unittests") {
|
rtc_library("repeating_task_unittests") {
|
||||||
testonly = true
|
testonly = true
|
||||||
|
|||||||
@ -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_
|
|
||||||
@ -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_
|
|
||||||
Loading…
x
Reference in New Issue
Block a user