From 24b0543ee0afaad7477e8be09612b30d91d4df2b Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Tue, 21 Jun 2022 11:38:32 +0200 Subject: [PATCH] Delete ProcessThread creation from test TimeController as unused Bug: webrtc:7219 Change-Id: Ia34f24a804b8a1e06b089774e37cac6e6d749e82 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/266366 Reviewed-by: Artem Titov Reviewed-by: Mirko Bonadei Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#37311} --- api/BUILD.gn | 1 - api/test/DEPS | 1 - api/test/time_controller.h | 4 - test/time_controller/BUILD.gn | 4 - .../external_time_controller.cc | 104 ---------- .../external_time_controller.h | 4 - test/time_controller/real_time_controller.cc | 5 - test/time_controller/real_time_controller.h | 3 - .../simulated_process_thread.cc | 188 ------------------ .../simulated_process_thread.h | 73 ------- .../simulated_time_controller.cc | 15 -- .../simulated_time_controller.h | 8 +- 12 files changed, 1 insertion(+), 409 deletions(-) delete mode 100644 test/time_controller/simulated_process_thread.cc delete mode 100644 test/time_controller/simulated_process_thread.h diff --git a/api/BUILD.gn b/api/BUILD.gn index 6ce43218d1..bb2c150a44 100644 --- a/api/BUILD.gn +++ b/api/BUILD.gn @@ -778,7 +778,6 @@ rtc_source_set("time_controller") { ] deps = [ - "../modules/utility", "../rtc_base", "../rtc_base:threading", "../rtc_base/synchronization:yield_policy", diff --git a/api/test/DEPS b/api/test/DEPS index 48889d528f..270b274c5f 100644 --- a/api/test/DEPS +++ b/api/test/DEPS @@ -30,7 +30,6 @@ specific_include_rules = { "+modules/audio_processing/include/audio_processing.h", ], "time_controller\.h": [ - "+modules/utility/include/process_thread.h", "+rtc_base/synchronization/yield_policy.h", "+system_wrappers/include/clock.h", ], diff --git a/api/test/time_controller.h b/api/test/time_controller.h index 17aa0db80f..121f65cea9 100644 --- a/api/test/time_controller.h +++ b/api/test/time_controller.h @@ -17,7 +17,6 @@ #include "api/task_queue/task_queue_factory.h" #include "api/units/time_delta.h" #include "api/units/timestamp.h" -#include "modules/utility/include/process_thread.h" #include "rtc_base/synchronization/yield_policy.h" #include "rtc_base/thread.h" #include "system_wrappers/include/clock.h" @@ -41,9 +40,6 @@ class TimeController { // is destroyed. std::unique_ptr CreateTaskQueueFactory(); - // Creates a process thread. - virtual std::unique_ptr CreateProcessThread( - const char* thread_name) = 0; // Creates an rtc::Thread instance. If `socket_server` is nullptr, a default // noop socket server is created. // Returned thread is not null and started. diff --git a/test/time_controller/BUILD.gn b/test/time_controller/BUILD.gn index bdb271d0f4..eff029f366 100644 --- a/test/time_controller/BUILD.gn +++ b/test/time_controller/BUILD.gn @@ -15,8 +15,6 @@ rtc_library("time_controller") { "external_time_controller.h", "real_time_controller.cc", "real_time_controller.h", - "simulated_process_thread.cc", - "simulated_process_thread.h", "simulated_task_queue.cc", "simulated_task_queue.h", "simulated_thread.cc", @@ -33,8 +31,6 @@ rtc_library("time_controller") { "../../api/task_queue:to_queued_task", "../../api/units:time_delta", "../../api/units:timestamp", - "../../modules:module_api", - "../../modules/utility:utility", "../../rtc_base", "../../rtc_base:checks", "../../rtc_base:null_socket_server", diff --git a/test/time_controller/external_time_controller.cc b/test/time_controller/external_time_controller.cc index e374d45fd9..9962a03ded 100644 --- a/test/time_controller/external_time_controller.cc +++ b/test/time_controller/external_time_controller.cc @@ -19,110 +19,12 @@ #include "api/task_queue/task_queue_factory.h" #include "api/units/time_delta.h" #include "api/units/timestamp.h" -#include "modules/include/module.h" -#include "modules/utility/include/process_thread.h" #include "rtc_base/checks.h" #include "rtc_base/synchronization/yield_policy.h" #include "test/time_controller/simulated_time_controller.h" namespace webrtc { -// Wraps a ProcessThread so that it can reschedule the time controller whenever -// an external call changes the ProcessThread's state. For example, when a new -// module is registered, the ProcessThread may need to be called sooner than the -// time controller's currently-scheduled deadline. -class ExternalTimeController::ProcessThreadWrapper : public ProcessThread { - public: - ProcessThreadWrapper(ExternalTimeController* parent, - std::unique_ptr thread) - : parent_(parent), thread_(std::move(thread)) {} - - void Start() override { - parent_->UpdateTime(); - thread_->Start(); - parent_->ScheduleNext(); - } - - void Stop() override { - parent_->UpdateTime(); - thread_->Stop(); - parent_->ScheduleNext(); - } - - void WakeUp(Module* module) override { - parent_->UpdateTime(); - thread_->WakeUp(GetWrapper(module)); - parent_->ScheduleNext(); - } - - void PostTask(std::unique_ptr task) override { - parent_->UpdateTime(); - thread_->PostTask(std::move(task)); - parent_->ScheduleNext(); - } - - void PostDelayedTask(std::unique_ptr task, - uint32_t milliseconds) override { - parent_->UpdateTime(); - thread_->PostDelayedTask(std::move(task), milliseconds); - parent_->ScheduleNext(); - } - - void RegisterModule(Module* module, const rtc::Location& from) override { - parent_->UpdateTime(); - module_wrappers_.emplace(module, new ModuleWrapper(module, this)); - thread_->RegisterModule(GetWrapper(module), from); - parent_->ScheduleNext(); - } - - void DeRegisterModule(Module* module) override { - parent_->UpdateTime(); - thread_->DeRegisterModule(GetWrapper(module)); - parent_->ScheduleNext(); - module_wrappers_.erase(module); - } - - private: - class ModuleWrapper : public Module { - public: - ModuleWrapper(Module* module, ProcessThreadWrapper* thread) - : module_(module), thread_(thread) {} - - int64_t TimeUntilNextProcess() override { - return module_->TimeUntilNextProcess(); - } - - void Process() override { module_->Process(); } - - void ProcessThreadAttached(ProcessThread* process_thread) override { - if (process_thread) { - module_->ProcessThreadAttached(thread_); - } else { - module_->ProcessThreadAttached(nullptr); - } - } - - private: - Module* module_; - ProcessThreadWrapper* thread_; - }; - - void Delete() override { - // ProcessThread shouldn't be deleted as a TaskQueue. - RTC_DCHECK_NOTREACHED(); - } - - ModuleWrapper* GetWrapper(Module* module) { - auto it = module_wrappers_.find(module); - RTC_DCHECK(it != module_wrappers_.end()); - return it->second.get(); - } - - ExternalTimeController* const parent_; - std::unique_ptr thread_; - std::map> module_wrappers_; -}; - // Wraps a TaskQueue so that it can reschedule the time controller whenever // an external call schedules a new task. class ExternalTimeController::TaskQueueWrapper : public TaskQueueBase { @@ -187,12 +89,6 @@ TaskQueueFactory* ExternalTimeController::GetTaskQueueFactory() { return this; } -std::unique_ptr ExternalTimeController::CreateProcessThread( - const char* thread_name) { - return std::make_unique( - this, impl_.CreateProcessThread(thread_name)); -} - void ExternalTimeController::AdvanceTime(TimeDelta duration) { alarm_->Sleep(duration); } diff --git a/test/time_controller/external_time_controller.h b/test/time_controller/external_time_controller.h index 80528e1403..a67f2557b4 100644 --- a/test/time_controller/external_time_controller.h +++ b/test/time_controller/external_time_controller.h @@ -19,7 +19,6 @@ #include "api/test/time_controller.h" #include "api/units/time_delta.h" #include "api/units/timestamp.h" -#include "modules/utility/include/process_thread.h" #include "system_wrappers/include/clock.h" #include "test/time_controller/simulated_time_controller.h" @@ -35,8 +34,6 @@ class ExternalTimeController : public TimeController, public TaskQueueFactory { // Implementation of TimeController. Clock* GetClock() override; TaskQueueFactory* GetTaskQueueFactory() override; - std::unique_ptr CreateProcessThread( - const char* thread_name) override; void AdvanceTime(TimeDelta duration) override; std::unique_ptr CreateThread( const std::string& name, @@ -49,7 +46,6 @@ class ExternalTimeController : public TimeController, public TaskQueueFactory { TaskQueueFactory::Priority priority) const override; private: - class ProcessThreadWrapper; class TaskQueueWrapper; // Executes any tasks scheduled at or before the current time. May call diff --git a/test/time_controller/real_time_controller.cc b/test/time_controller/real_time_controller.cc index 2e741cf20c..7cc750d6d4 100644 --- a/test/time_controller/real_time_controller.cc +++ b/test/time_controller/real_time_controller.cc @@ -44,11 +44,6 @@ TaskQueueFactory* RealTimeController::GetTaskQueueFactory() { return task_queue_factory_.get(); } -std::unique_ptr RealTimeController::CreateProcessThread( - const char* thread_name) { - return ProcessThread::Create(thread_name); -} - std::unique_ptr RealTimeController::CreateThread( const std::string& name, std::unique_ptr socket_server) { diff --git a/test/time_controller/real_time_controller.h b/test/time_controller/real_time_controller.h index bbee6ef967..5f02eaf85f 100644 --- a/test/time_controller/real_time_controller.h +++ b/test/time_controller/real_time_controller.h @@ -16,7 +16,6 @@ #include "api/task_queue/task_queue_factory.h" #include "api/test/time_controller.h" #include "api/units/time_delta.h" -#include "modules/utility/include/process_thread.h" #include "system_wrappers/include/clock.h" namespace webrtc { @@ -26,8 +25,6 @@ class RealTimeController : public TimeController { Clock* GetClock() override; TaskQueueFactory* GetTaskQueueFactory() override; - std::unique_ptr CreateProcessThread( - const char* thread_name) override; std::unique_ptr CreateThread( const std::string& name, std::unique_ptr socket_server) override; diff --git a/test/time_controller/simulated_process_thread.cc b/test/time_controller/simulated_process_thread.cc deleted file mode 100644 index e001841ac0..0000000000 --- a/test/time_controller/simulated_process_thread.cc +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 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. - */ - -#include "test/time_controller/simulated_process_thread.h" - -#include -#include - -namespace webrtc { -namespace { -// Helper function to remove from a std container by value. -template -bool RemoveByValue(C* vec, typename C::value_type val) { - auto it = std::find(vec->begin(), vec->end(), val); - if (it == vec->end()) - return false; - vec->erase(it); - return true; -} -} // namespace -SimulatedProcessThread::SimulatedProcessThread( - sim_time_impl::SimulatedTimeControllerImpl* handler, - absl::string_view name) - : handler_(handler), name_(new char[name.size()]) { - std::copy_n(name.begin(), name.size(), name_); -} - -SimulatedProcessThread::~SimulatedProcessThread() { - handler_->Unregister(this); - delete[] name_; -} - -void SimulatedProcessThread::RunReady(Timestamp at_time) { - CurrentTaskQueueSetter set_current(this); - MutexLock lock(&lock_); - std::vector ready_modules; - for (auto it = delayed_modules_.begin(); - it != delayed_modules_.end() && it->first <= at_time; - it = delayed_modules_.erase(it)) { - for (auto module : it->second) { - ready_modules.push_back(module); - } - } - for (auto* module : ready_modules) { - module->Process(); - delayed_modules_[GetNextTime(module, at_time)].push_back(module); - } - - for (auto it = delayed_tasks_.begin(); - it != delayed_tasks_.end() && it->first <= at_time; - it = delayed_tasks_.erase(it)) { - for (auto& task : it->second) { - queue_.push_back(std::move(task)); - } - } - while (!queue_.empty()) { - std::unique_ptr task = std::move(queue_.front()); - queue_.pop_front(); - lock_.Unlock(); - bool should_delete = task->Run(); - RTC_CHECK(should_delete); - lock_.Lock(); - } - RTC_DCHECK(queue_.empty()); - if (!delayed_modules_.empty()) { - next_run_time_ = delayed_modules_.begin()->first; - } else { - next_run_time_ = Timestamp::PlusInfinity(); - } - if (!delayed_tasks_.empty()) { - next_run_time_ = std::min(next_run_time_, delayed_tasks_.begin()->first); - } -} -void SimulatedProcessThread::Start() { - std::vector starting; - { - MutexLock lock(&lock_); - if (process_thread_running_) - return; - process_thread_running_ = true; - starting.swap(stopped_modules_); - } - for (auto& module : starting) - module->ProcessThreadAttached(this); - - Timestamp at_time = handler_->CurrentTime(); - MutexLock lock(&lock_); - for (auto& module : starting) - delayed_modules_[GetNextTime(module, at_time)].push_back(module); - - if (!queue_.empty()) { - next_run_time_ = Timestamp::MinusInfinity(); - } else if (!delayed_modules_.empty()) { - next_run_time_ = delayed_modules_.begin()->first; - } else { - next_run_time_ = Timestamp::PlusInfinity(); - } -} - -void SimulatedProcessThread::Stop() { - std::vector stopping; - { - MutexLock lock(&lock_); - process_thread_running_ = false; - - for (auto& delayed : delayed_modules_) { - for (auto mod : delayed.second) - stopped_modules_.push_back(mod); - } - delayed_modules_.clear(); - - stopping = stopped_modules_; - } - for (auto& module : stopping) - module->ProcessThreadAttached(nullptr); -} - -void SimulatedProcessThread::WakeUp(Module* module) { - MutexLock lock(&lock_); - for (auto it = delayed_modules_.begin(); it != delayed_modules_.end(); ++it) { - if (RemoveByValue(&it->second, module)) - break; - } - Timestamp next_time = GetNextTime(module, handler_->CurrentTime()); - delayed_modules_[next_time].push_back(module); - next_run_time_ = std::min(next_run_time_, next_time); -} - -void SimulatedProcessThread::RegisterModule(Module* module, - const rtc::Location& from) { - module->ProcessThreadAttached(this); - MutexLock lock(&lock_); - if (!process_thread_running_) { - stopped_modules_.push_back(module); - } else { - Timestamp next_time = GetNextTime(module, handler_->CurrentTime()); - delayed_modules_[next_time].push_back(module); - next_run_time_ = std::min(next_run_time_, next_time); - } -} - -void SimulatedProcessThread::DeRegisterModule(Module* module) { - bool modules_running; - { - MutexLock lock(&lock_); - if (!process_thread_running_) { - RemoveByValue(&stopped_modules_, module); - } else { - for (auto& pair : delayed_modules_) { - if (RemoveByValue(&pair.second, module)) - break; - } - } - modules_running = process_thread_running_; - } - if (modules_running) - module->ProcessThreadAttached(nullptr); -} - -void SimulatedProcessThread::PostTask(std::unique_ptr task) { - MutexLock lock(&lock_); - queue_.emplace_back(std::move(task)); - next_run_time_ = Timestamp::MinusInfinity(); -} - -void SimulatedProcessThread::PostDelayedTask(std::unique_ptr task, - uint32_t milliseconds) { - MutexLock lock(&lock_); - Timestamp target_time = - handler_->CurrentTime() + TimeDelta::Millis(milliseconds); - delayed_tasks_[target_time].push_back(std::move(task)); - next_run_time_ = std::min(next_run_time_, target_time); -} - -Timestamp SimulatedProcessThread::GetNextTime(Module* module, - Timestamp at_time) { - CurrentTaskQueueSetter set_current(this); - return at_time + TimeDelta::Millis(module->TimeUntilNextProcess()); -} - -} // namespace webrtc diff --git a/test/time_controller/simulated_process_thread.h b/test/time_controller/simulated_process_thread.h deleted file mode 100644 index 9a0efacca3..0000000000 --- a/test/time_controller/simulated_process_thread.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 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 TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_ -#define TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_ - -#include -#include -#include -#include -#include - -#include "rtc_base/synchronization/mutex.h" -#include "test/time_controller/simulated_time_controller.h" - -namespace webrtc { - -class SimulatedProcessThread : public ProcessThread, - public sim_time_impl::SimulatedSequenceRunner { - public: - SimulatedProcessThread(sim_time_impl::SimulatedTimeControllerImpl* handler, - absl::string_view name); - virtual ~SimulatedProcessThread(); - void RunReady(Timestamp at_time) override; - - Timestamp GetNextRunTime() const override { - MutexLock lock(&lock_); - return next_run_time_; - } - - TaskQueueBase* GetAsTaskQueue() override { return this; } - - // ProcessThread interface - void Start() override; - void Stop() override; - void WakeUp(Module* module) override; - void RegisterModule(Module* module, const rtc::Location& from) override; - void DeRegisterModule(Module* module) override; - void PostTask(std::unique_ptr task) override; - void PostDelayedTask(std::unique_ptr task, - uint32_t milliseconds) override; - - private: - void Delete() override { - // ProcessThread shouldn't be deleted as a TaskQueue. - RTC_DCHECK_NOTREACHED(); - } - Timestamp GetNextTime(Module* module, Timestamp at_time); - - sim_time_impl::SimulatedTimeControllerImpl* const handler_; - // Using char* to be debugger friendly. - char* name_; - mutable Mutex lock_; - Timestamp next_run_time_ RTC_GUARDED_BY(lock_) = Timestamp::PlusInfinity(); - - std::deque> queue_; - std::map>> delayed_tasks_ - RTC_GUARDED_BY(lock_); - - bool process_thread_running_ RTC_GUARDED_BY(lock_) = false; - std::vector stopped_modules_ RTC_GUARDED_BY(lock_); - std::map> delayed_modules_ - RTC_GUARDED_BY(lock_); -}; -} // namespace webrtc - -#endif // TEST_TIME_CONTROLLER_SIMULATED_PROCESS_THREAD_H_ diff --git a/test/time_controller/simulated_time_controller.cc b/test/time_controller/simulated_time_controller.cc index b92444b704..1ed2b30dc8 100644 --- a/test/time_controller/simulated_time_controller.cc +++ b/test/time_controller/simulated_time_controller.cc @@ -18,7 +18,6 @@ #include #include "absl/strings/string_view.h" -#include "test/time_controller/simulated_process_thread.h" #include "test/time_controller/simulated_task_queue.h" #include "test/time_controller/simulated_thread.h" @@ -50,19 +49,10 @@ SimulatedTimeControllerImpl::CreateTaskQueue( auto mutable_this = const_cast(this); auto task_queue = std::unique_ptr( new SimulatedTaskQueue(mutable_this, name)); - ; mutable_this->Register(task_queue.get()); return task_queue; } -std::unique_ptr SimulatedTimeControllerImpl::CreateProcessThread( - const char* thread_name) { - auto process_thread = - std::make_unique(this, thread_name); - Register(process_thread.get()); - return process_thread; -} - std::unique_ptr SimulatedTimeControllerImpl::CreateThread( const std::string& name, std::unique_ptr socket_server) { @@ -192,11 +182,6 @@ TaskQueueFactory* GlobalSimulatedTimeController::GetTaskQueueFactory() { return &impl_; } -std::unique_ptr -GlobalSimulatedTimeController::CreateProcessThread(const char* thread_name) { - return impl_.CreateProcessThread(thread_name); -} - std::unique_ptr GlobalSimulatedTimeController::CreateThread( const std::string& name, std::unique_ptr socket_server) { diff --git a/test/time_controller/simulated_time_controller.h b/test/time_controller/simulated_time_controller.h index b79de3ba8c..b3f127899d 100644 --- a/test/time_controller/simulated_time_controller.h +++ b/test/time_controller/simulated_time_controller.h @@ -20,8 +20,6 @@ #include "api/sequence_checker.h" #include "api/test/time_controller.h" #include "api/units/timestamp.h" -#include "modules/include/module.h" -#include "modules/utility/include/process_thread.h" #include "rtc_base/fake_clock.h" #include "rtc_base/platform_thread_types.h" #include "rtc_base/synchronization/mutex.h" @@ -58,9 +56,7 @@ class SimulatedTimeControllerImpl : public TaskQueueFactory, // except that if this method is called from a task, the task queue running // that task is skipped. void YieldExecution() RTC_LOCKS_EXCLUDED(time_lock_, lock_) override; - // Create process thread with the name `thread_name`. - std::unique_ptr CreateProcessThread(const char* thread_name) - RTC_LOCKS_EXCLUDED(time_lock_, lock_); + // Create thread using provided `socket_server`. std::unique_ptr CreateThread( const std::string& name, @@ -131,8 +127,6 @@ class GlobalSimulatedTimeController : public TimeController { Clock* GetClock() override; TaskQueueFactory* GetTaskQueueFactory() override; - std::unique_ptr CreateProcessThread( - const char* thread_name) override; std::unique_ptr CreateThread( const std::string& name, std::unique_ptr socket_server) override;