From 56f9f0eed19fc878fcf8c04f3c45ba2cb10d87da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dino=20Radakovi=C4=87?= Date: Thu, 15 Mar 2018 15:19:16 +0100 Subject: [PATCH] Make task_queue_ injectable by wrapping it into a std::unique_ptr and adding an optional arg to the constructor of RtcEventLogImpl. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:9004 Change-Id: I46336ba4f6464d806f0fb8549f98faea69a5f748 Reviewed-on: https://webrtc-review.googlesource.com/61420 Commit-Queue: Dino Radaković Reviewed-by: Björn Terelius Reviewed-by: Elad Alon Cr-Commit-Position: refs/heads/master@{#22456} --- logging/rtc_event_log/rtc_event_log_impl.cc | 64 +++++++++++---------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/logging/rtc_event_log/rtc_event_log_impl.cc b/logging/rtc_event_log/rtc_event_log_impl.cc index f1582e820a..77d3822c5a 100644 --- a/logging/rtc_event_log/rtc_event_log_impl.cc +++ b/logging/rtc_event_log/rtc_event_log_impl.cc @@ -81,7 +81,10 @@ std::unique_ptr CreateEncoder( class RtcEventLogImpl final : public RtcEventLog { public: - explicit RtcEventLogImpl(std::unique_ptr event_encoder); + explicit RtcEventLogImpl( + std::unique_ptr event_encoder, + std::unique_ptr task_queue = + rtc::MakeUnique("rtc_event_log")); ~RtcEventLogImpl() override; // TODO(eladalon): We should change these name to reflect that what we're @@ -93,19 +96,19 @@ class RtcEventLogImpl final : public RtcEventLog { void Log(std::unique_ptr event) override; private: - void LogToMemory(std::unique_ptr event) RTC_RUN_ON(&task_queue_); - void LogEventsFromMemoryToOutput() RTC_RUN_ON(&task_queue_); + void LogToMemory(std::unique_ptr event) RTC_RUN_ON(task_queue_); + void LogEventsFromMemoryToOutput() RTC_RUN_ON(task_queue_); - void StopOutput() RTC_RUN_ON(&task_queue_); + void StopOutput() RTC_RUN_ON(task_queue_); void WriteConfigsAndHistoryToOutput(const std::string& encoded_configs, const std::string& encoded_history) - RTC_RUN_ON(&task_queue_); - void WriteToOutput(const std::string& output_string) RTC_RUN_ON(&task_queue_); + RTC_RUN_ON(task_queue_); + void WriteToOutput(const std::string& output_string) RTC_RUN_ON(task_queue_); - void StopLoggingInternal() RTC_RUN_ON(&task_queue_); + void StopLoggingInternal() RTC_RUN_ON(task_queue_); - void ScheduleOutput() RTC_RUN_ON(&task_queue_); + void ScheduleOutput() RTC_RUN_ON(task_queue_); // Make sure that the event log is "managed" - created/destroyed, as well // as started/stopped - from the same thread/task-queue. @@ -113,34 +116,35 @@ class RtcEventLogImpl final : public RtcEventLog { // History containing all past configuration events. std::deque> config_history_ - RTC_GUARDED_BY(task_queue_); + RTC_GUARDED_BY(*task_queue_); // History containing the most recent (non-configuration) events (~10s). - std::deque> history_ RTC_GUARDED_BY(task_queue_); + std::deque> history_ RTC_GUARDED_BY(*task_queue_); - size_t max_size_bytes_ RTC_GUARDED_BY(task_queue_); - size_t written_bytes_ RTC_GUARDED_BY(task_queue_); + size_t max_size_bytes_ RTC_GUARDED_BY(*task_queue_); + size_t written_bytes_ RTC_GUARDED_BY(*task_queue_); std::unique_ptr event_encoder_ - RTC_GUARDED_BY(task_queue_); - std::unique_ptr event_output_ RTC_GUARDED_BY(task_queue_); + RTC_GUARDED_BY(*task_queue_); + std::unique_ptr event_output_ RTC_GUARDED_BY(*task_queue_); - size_t num_config_events_written_ RTC_GUARDED_BY(task_queue_); - int64_t output_period_ms_ RTC_GUARDED_BY(task_queue_); - int64_t last_output_ms_ RTC_GUARDED_BY(task_queue_); - bool output_scheduled_ RTC_GUARDED_BY(task_queue_); + size_t num_config_events_written_ RTC_GUARDED_BY(*task_queue_); + int64_t output_period_ms_ RTC_GUARDED_BY(*task_queue_); + int64_t last_output_ms_ RTC_GUARDED_BY(*task_queue_); + bool output_scheduled_ RTC_GUARDED_BY(*task_queue_); // Since we are posting tasks bound to |this|, it is critical that the event // log and it's members outlive the |task_queue_|. Keep the "task_queue_| // last to ensure it destructs first, or else tasks living on the queue might // access other members after they've been torn down. - rtc::TaskQueue task_queue_; + std::unique_ptr task_queue_; RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogImpl); }; RtcEventLogImpl::RtcEventLogImpl( - std::unique_ptr event_encoder) + std::unique_ptr event_encoder, + std::unique_ptr task_queue) : max_size_bytes_(std::numeric_limits::max()), written_bytes_(0), event_encoder_(std::move(event_encoder)), @@ -148,7 +152,9 @@ RtcEventLogImpl::RtcEventLogImpl( output_period_ms_(kImmediateOutput), last_output_ms_(rtc::TimeMillis()), output_scheduled_(false), - task_queue_("rtc_event_log") {} + task_queue_(std::move(task_queue)) { + RTC_DCHECK(task_queue_); +} RtcEventLogImpl::~RtcEventLogImpl() { RTC_DCHECK_CALLED_SEQUENTIALLY(&owner_sequence_checker_); @@ -178,7 +184,7 @@ bool RtcEventLogImpl::StartLogging(std::unique_ptr output, // Binding to |this| is safe because |this| outlives the |task_queue_|. auto start = [this, timestamp_us](std::unique_ptr output) { - RTC_DCHECK_RUN_ON(&task_queue_); + RTC_DCHECK_RUN_ON(task_queue_.get()); RTC_DCHECK(output->IsActive()); event_output_ = std::move(output); num_config_events_written_ = 0; @@ -186,7 +192,7 @@ bool RtcEventLogImpl::StartLogging(std::unique_ptr output, LogEventsFromMemoryToOutput(); }; - task_queue_.PostTask(rtc::MakeUnique>( + task_queue_->PostTask(rtc::MakeUnique>( std::move(output), start)); return true; @@ -200,8 +206,8 @@ void RtcEventLogImpl::StopLogging() { rtc::Event output_stopped(true, false); // Binding to |this| is safe because |this| outlives the |task_queue_|. - task_queue_.PostTask([this, &output_stopped]() { - RTC_DCHECK_RUN_ON(&task_queue_); + task_queue_->PostTask([this, &output_stopped]() { + RTC_DCHECK_RUN_ON(task_queue_.get()); if (event_output_) { RTC_DCHECK(event_output_->IsActive()); LogEventsFromMemoryToOutput(); @@ -220,13 +226,13 @@ void RtcEventLogImpl::Log(std::unique_ptr event) { // Binding to |this| is safe because |this| outlives the |task_queue_|. auto event_handler = [this](std::unique_ptr unencoded_event) { - RTC_DCHECK_RUN_ON(&task_queue_); + RTC_DCHECK_RUN_ON(task_queue_.get()); LogToMemory(std::move(unencoded_event)); if (event_output_) ScheduleOutput(); }; - task_queue_.PostTask(rtc::MakeUnique>( + task_queue_->PostTask(rtc::MakeUnique>( std::move(event), event_handler)); } @@ -250,7 +256,7 @@ void RtcEventLogImpl::ScheduleOutput() { output_scheduled_ = true; // Binding to |this| is safe because |this| outlives the |task_queue_|. auto output_task = [this]() { - RTC_DCHECK_RUN_ON(&task_queue_); + RTC_DCHECK_RUN_ON(task_queue_.get()); if (event_output_) { RTC_DCHECK(event_output_->IsActive()); LogEventsFromMemoryToOutput(); @@ -261,7 +267,7 @@ void RtcEventLogImpl::ScheduleOutput() { int64_t time_since_output_ms = now_ms - last_output_ms_; uint32_t delay = rtc::SafeClamp(output_period_ms_ - time_since_output_ms, 0, output_period_ms_); - task_queue_.PostDelayedTask(output_task, delay); + task_queue_->PostDelayedTask(output_task, delay); } }