mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-13 22:00:47 +01:00

This CL adds a single class to manage the use case of having a task that repeats itself by a fixed or variable interval. It replaces the repeating task previously locally defined for rtp transport controller send as well as the cancelable periodic task. Furthermore, it is introduced where one off repeating tasks were created before. It provides the currently used functionality of the cancelable periodic task, but not some of the unused features, such as allowing cancellation of tasks before they are started and cancellation of a task after the owning task queue has been destroyed. Bug: webrtc:9883 Change-Id: Ifa7edee836c2a64fce16a7d0f682eb09c879eaca Reviewed-on: https://webrtc-review.googlesource.com/c/116182 Commit-Queue: Sebastian Jansson <srte@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#26313}
119 lines
3.5 KiB
C++
119 lines
3.5 KiB
C++
/*
|
|
* 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 "rtc_base/task_utils/repeating_task.h"
|
|
#include "rtc_base/logging.h"
|
|
#include "rtc_base/timeutils.h"
|
|
|
|
namespace webrtc {
|
|
namespace webrtc_repeating_task_impl {
|
|
RepeatingTaskBase::RepeatingTaskBase(rtc::TaskQueue* task_queue,
|
|
TimeDelta first_delay)
|
|
: task_queue_(task_queue),
|
|
next_run_time_(Timestamp::us(rtc::TimeMicros()) + first_delay) {}
|
|
|
|
RepeatingTaskBase::~RepeatingTaskBase() = default;
|
|
|
|
bool RepeatingTaskBase::Run() {
|
|
RTC_DCHECK_RUN_ON(task_queue_);
|
|
// Return true to tell the TaskQueue to destruct this object.
|
|
if (next_run_time_.IsPlusInfinity())
|
|
return true;
|
|
|
|
TimeDelta delay = RunClosure();
|
|
RTC_DCHECK(delay.IsFinite());
|
|
|
|
// The closure might have stopped this task, in which case we return true to
|
|
// destruct this object.
|
|
if (next_run_time_.IsPlusInfinity())
|
|
return true;
|
|
|
|
TimeDelta lost_time = Timestamp::us(rtc::TimeMicros()) - next_run_time_;
|
|
next_run_time_ += delay;
|
|
delay -= lost_time;
|
|
|
|
if (delay <= TimeDelta::Zero()) {
|
|
task_queue_->PostTask(absl::WrapUnique(this));
|
|
} else {
|
|
task_queue_->PostDelayedTask(absl::WrapUnique(this), delay.ms());
|
|
}
|
|
// Return false to tell the TaskQueue to not destruct this object since we
|
|
// have taken ownership with absl::WrapUnique.
|
|
return false;
|
|
}
|
|
|
|
void RepeatingTaskBase::Stop() {
|
|
RTC_DCHECK(next_run_time_.IsFinite());
|
|
next_run_time_ = Timestamp::PlusInfinity();
|
|
}
|
|
|
|
void RepeatingTaskBase::PostStop() {
|
|
if (task_queue_->IsCurrent()) {
|
|
RTC_DLOG(LS_INFO) << "Using PostStop() from the task queue running the "
|
|
"repeated task. Consider calling Stop() instead.";
|
|
}
|
|
task_queue_->PostTask([this] {
|
|
RTC_DCHECK_RUN_ON(task_queue_);
|
|
Stop();
|
|
});
|
|
}
|
|
|
|
} // namespace webrtc_repeating_task_impl
|
|
RepeatingTaskHandle::RepeatingTaskHandle() {
|
|
sequence_checker_.Detach();
|
|
}
|
|
RepeatingTaskHandle::~RepeatingTaskHandle() {
|
|
sequence_checker_.Detach();
|
|
}
|
|
|
|
RepeatingTaskHandle::RepeatingTaskHandle(RepeatingTaskHandle&& other)
|
|
: repeating_task_(other.repeating_task_) {
|
|
RTC_DCHECK_RUN_ON(&sequence_checker_);
|
|
other.repeating_task_ = nullptr;
|
|
}
|
|
|
|
RepeatingTaskHandle& RepeatingTaskHandle::operator=(
|
|
RepeatingTaskHandle&& other) {
|
|
RTC_DCHECK_RUN_ON(&other.sequence_checker_);
|
|
{
|
|
RTC_DCHECK_RUN_ON(&sequence_checker_);
|
|
repeating_task_ = other.repeating_task_;
|
|
}
|
|
other.repeating_task_ = nullptr;
|
|
return *this;
|
|
}
|
|
|
|
RepeatingTaskHandle::RepeatingTaskHandle(
|
|
webrtc_repeating_task_impl::RepeatingTaskBase* repeating_task)
|
|
: repeating_task_(repeating_task) {}
|
|
|
|
void RepeatingTaskHandle::Stop() {
|
|
RTC_DCHECK_RUN_ON(&sequence_checker_);
|
|
if (repeating_task_) {
|
|
RTC_DCHECK_RUN_ON(repeating_task_->task_queue_);
|
|
repeating_task_->Stop();
|
|
repeating_task_ = nullptr;
|
|
}
|
|
}
|
|
|
|
void RepeatingTaskHandle::PostStop() {
|
|
RTC_DCHECK_RUN_ON(&sequence_checker_);
|
|
if (repeating_task_) {
|
|
repeating_task_->PostStop();
|
|
repeating_task_ = nullptr;
|
|
}
|
|
}
|
|
|
|
bool RepeatingTaskHandle::Running() const {
|
|
RTC_DCHECK_RUN_ON(&sequence_checker_);
|
|
return repeating_task_ != nullptr;
|
|
}
|
|
|
|
} // namespace webrtc
|