diff --git a/api/task_queue/BUILD.gn b/api/task_queue/BUILD.gn new file mode 100644 index 0000000000..e9dfe66ef7 --- /dev/null +++ b/api/task_queue/BUILD.gn @@ -0,0 +1,16 @@ +# Copyright (c) 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. + +import("../../webrtc.gni") + +rtc_source_set("task_queue") { + visibility = [ "*" ] + public = [ + "queued_task.h", + ] +} diff --git a/api/task_queue/queued_task.h b/api/task_queue/queued_task.h new file mode 100644 index 0000000000..5748628d7b --- /dev/null +++ b/api/task_queue/queued_task.h @@ -0,0 +1,32 @@ +/* + * 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/modules/utility/BUILD.gn b/modules/utility/BUILD.gn index 1d780b0e4a..4354f0cf94 100644 --- a/modules/utility/BUILD.gn +++ b/modules/utility/BUILD.gn @@ -26,10 +26,10 @@ rtc_static_library("utility") { deps = [ "..:module_api", + "../../api/task_queue", "../../common_audio", "../../rtc_base:checks", "../../rtc_base:rtc_base_approved", - "../../rtc_base:rtc_task_queue", "../../rtc_base/system:arch", "../../system_wrappers", ] diff --git a/modules/utility/include/mock/mock_process_thread.h b/modules/utility/include/mock/mock_process_thread.h index fb88e40074..6826f5275c 100644 --- a/modules/utility/include/mock/mock_process_thread.h +++ b/modules/utility/include/mock/mock_process_thread.h @@ -28,14 +28,14 @@ class MockProcessThread : public ProcessThread { MOCK_METHOD0(Start, void()); MOCK_METHOD0(Stop, void()); MOCK_METHOD1(WakeUp, void(Module* module)); - MOCK_METHOD1(PostTask, void(rtc::QueuedTask* task)); + MOCK_METHOD1(PostTask, void(QueuedTask* task)); MOCK_METHOD2(RegisterModule, void(Module* module, const rtc::Location&)); MOCK_METHOD1(DeRegisterModule, void(Module* module)); // MOCK_METHOD1 gets confused with mocking this method, so we work around it // by overriding the method from the interface and forwarding the call to a // mocked, simpler method. - void PostTask(std::unique_ptr task) /*override*/ { + void PostTask(std::unique_ptr task) /*override*/ { PostTask(task.get()); } }; diff --git a/modules/utility/include/process_thread.h b/modules/utility/include/process_thread.h index 8c6919660a..9b02a7ea9a 100644 --- a/modules/utility/include/process_thread.h +++ b/modules/utility/include/process_thread.h @@ -13,15 +13,7 @@ #include -#if defined(WEBRTC_WIN) -// Due to a bug in the std::unique_ptr implementation that ships with MSVS, -// we need the full definition of QueuedTask, on Windows. -#include "rtc_base/task_queue.h" -#else -namespace rtc { -class QueuedTask; -} -#endif +#include "api/task_queue/queued_task.h" namespace rtc { class Location; @@ -59,7 +51,7 @@ class ProcessThread { // construction thread of the ProcessThread instance, if the task did not // get a chance to run (e.g. posting the task while shutting down or when // the thread never runs). - virtual void PostTask(std::unique_ptr task) = 0; + virtual void PostTask(std::unique_ptr task) = 0; // Adds a module that will start to receive callbacks on the worker thread. // Can be called from any thread. diff --git a/modules/utility/source/process_thread_impl.cc b/modules/utility/source/process_thread_impl.cc index fbe60bbc85..5c2f0abd1f 100644 --- a/modules/utility/source/process_thread_impl.cc +++ b/modules/utility/source/process_thread_impl.cc @@ -14,7 +14,6 @@ #include "modules/include/module.h" #include "rtc_base/checks.h" -#include "rtc_base/task_queue.h" #include "rtc_base/time_utils.h" #include "rtc_base/trace_event.h" @@ -105,7 +104,7 @@ void ProcessThreadImpl::WakeUp(Module* module) { wake_up_.Set(); } -void ProcessThreadImpl::PostTask(std::unique_ptr task) { +void ProcessThreadImpl::PostTask(std::unique_ptr task) { // Allowed to be called on any thread. { rtc::CritScope lock(&lock_); @@ -204,7 +203,7 @@ bool ProcessThreadImpl::Process() { } while (!queue_.empty()) { - rtc::QueuedTask* task = queue_.front(); + QueuedTask* task = queue_.front(); queue_.pop(); lock_.Leave(); task->Run(); diff --git a/modules/utility/source/process_thread_impl.h b/modules/utility/source/process_thread_impl.h index e927c1d533..7339fe1f87 100644 --- a/modules/utility/source/process_thread_impl.h +++ b/modules/utility/source/process_thread_impl.h @@ -16,13 +16,13 @@ #include #include +#include "api/task_queue/queued_task.h" #include "modules/include/module.h" #include "modules/utility/include/process_thread.h" #include "rtc_base/critical_section.h" #include "rtc_base/event.h" #include "rtc_base/location.h" #include "rtc_base/platform_thread.h" -#include "rtc_base/task_queue.h" #include "rtc_base/thread_checker.h" namespace webrtc { @@ -36,7 +36,7 @@ class ProcessThreadImpl : public ProcessThread { void Stop() override; void WakeUp(Module* module) override; - void PostTask(std::unique_ptr task) override; + void PostTask(std::unique_ptr task) override; void RegisterModule(Module* module, const rtc::Location& from) override; void DeRegisterModule(Module* module) override; @@ -80,7 +80,7 @@ class ProcessThreadImpl : public ProcessThread { std::unique_ptr thread_; ModuleList modules_; - std::queue queue_; + std::queue queue_; bool stop_; const char* thread_name_; }; diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 67b42903ef..cd9d1799f4 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -533,6 +533,7 @@ rtc_source_set("rtc_task_queue_api") { deps = [ ":macromagic", ":ptr_util", + "../api/task_queue", "system:rtc_export", "//third_party/abseil-cpp/absl/memory", ] diff --git a/rtc_base/task_queue.h b/rtc_base/task_queue.h index 0f2d219955..41fcf41953 100644 --- a/rtc_base/task_queue.h +++ b/rtc_base/task_queue.h @@ -17,6 +17,7 @@ #include #include "absl/memory/memory.h" +#include "api/task_queue/queued_task.h" #include "rtc_base/constructor_magic.h" #include "rtc_base/scoped_ref_ptr.h" #include "rtc_base/system/rtc_export.h" @@ -24,24 +25,9 @@ namespace rtc { -// 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: - QueuedTask() {} - virtual ~QueuedTask() {} - - // 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; - - private: - RTC_DISALLOW_COPY_AND_ASSIGN(QueuedTask); -}; +// TODO(danilchap): Remove the alias when all of webrtc is updated to use +// webrtc::QueuedTask directly. +using ::webrtc::QueuedTask; // Simple implementation of QueuedTask for use with rtc::Bind and lambdas. template