Make task_queue_ injectable by wrapping it into a std::unique_ptr and adding an optional arg to the constructor of RtcEventLogImpl.

Bug: webrtc:9004
Change-Id: I46336ba4f6464d806f0fb8549f98faea69a5f748
Reviewed-on: https://webrtc-review.googlesource.com/61420
Commit-Queue: Dino Radaković <dinor@webrtc.org>
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Reviewed-by: Elad Alon <eladalon@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22456}
This commit is contained in:
Dino Radaković
2018-03-15 15:19:16 +01:00
committed by Commit Bot
parent 3ab1d262bc
commit 56f9f0eed1

View File

@ -81,7 +81,10 @@ std::unique_ptr<RtcEventLogEncoder> CreateEncoder(
class RtcEventLogImpl final : public RtcEventLog {
public:
explicit RtcEventLogImpl(std::unique_ptr<RtcEventLogEncoder> event_encoder);
explicit RtcEventLogImpl(
std::unique_ptr<RtcEventLogEncoder> event_encoder,
std::unique_ptr<rtc::TaskQueue> task_queue =
rtc::MakeUnique<rtc::TaskQueue>("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<RtcEvent> event) override;
private:
void LogToMemory(std::unique_ptr<RtcEvent> event) RTC_RUN_ON(&task_queue_);
void LogEventsFromMemoryToOutput() RTC_RUN_ON(&task_queue_);
void LogToMemory(std::unique_ptr<RtcEvent> 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<std::unique_ptr<RtcEvent>> config_history_
RTC_GUARDED_BY(task_queue_);
RTC_GUARDED_BY(*task_queue_);
// History containing the most recent (non-configuration) events (~10s).
std::deque<std::unique_ptr<RtcEvent>> history_ RTC_GUARDED_BY(task_queue_);
std::deque<std::unique_ptr<RtcEvent>> 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<RtcEventLogEncoder> event_encoder_
RTC_GUARDED_BY(task_queue_);
std::unique_ptr<RtcEventLogOutput> event_output_ RTC_GUARDED_BY(task_queue_);
RTC_GUARDED_BY(*task_queue_);
std::unique_ptr<RtcEventLogOutput> 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<rtc::TaskQueue> task_queue_;
RTC_DISALLOW_COPY_AND_ASSIGN(RtcEventLogImpl);
};
RtcEventLogImpl::RtcEventLogImpl(
std::unique_ptr<RtcEventLogEncoder> event_encoder)
std::unique_ptr<RtcEventLogEncoder> event_encoder,
std::unique_ptr<rtc::TaskQueue> task_queue)
: max_size_bytes_(std::numeric_limits<decltype(max_size_bytes_)>::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<RtcEventLogOutput> output,
// Binding to |this| is safe because |this| outlives the |task_queue_|.
auto start = [this, timestamp_us](std::unique_ptr<RtcEventLogOutput> 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<RtcEventLogOutput> output,
LogEventsFromMemoryToOutput();
};
task_queue_.PostTask(rtc::MakeUnique<ResourceOwningTask<RtcEventLogOutput>>(
task_queue_->PostTask(rtc::MakeUnique<ResourceOwningTask<RtcEventLogOutput>>(
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<RtcEvent> event) {
// Binding to |this| is safe because |this| outlives the |task_queue_|.
auto event_handler = [this](std::unique_ptr<RtcEvent> 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<ResourceOwningTask<RtcEvent>>(
task_queue_->PostTask(rtc::MakeUnique<ResourceOwningTask<RtcEvent>>(
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);
}
}