2019-01-18 10:30:54 +01:00
|
|
|
/*
|
|
|
|
|
* 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"
|
2019-03-06 18:41:39 +01:00
|
|
|
|
2019-09-17 17:06:18 +02:00
|
|
|
#include "absl/memory/memory.h"
|
2019-01-18 10:30:54 +01:00
|
|
|
#include "rtc_base/logging.h"
|
2021-06-11 18:39:17 +02:00
|
|
|
#include "rtc_base/task_utils/pending_task_safety_flag.h"
|
2019-03-06 18:41:39 +01:00
|
|
|
#include "rtc_base/task_utils/to_queued_task.h"
|
2019-01-24 19:07:40 -08:00
|
|
|
#include "rtc_base/time_utils.h"
|
2019-01-18 10:30:54 +01:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace webrtc_repeating_task_impl {
|
2020-05-18 14:53:42 +02:00
|
|
|
|
2021-06-11 18:39:17 +02:00
|
|
|
RepeatingTaskBase::RepeatingTaskBase(
|
|
|
|
|
TaskQueueBase* task_queue,
|
2022-01-24 17:12:35 +01:00
|
|
|
TaskQueueBase::DelayPrecision precision,
|
2021-06-11 18:39:17 +02:00
|
|
|
TimeDelta first_delay,
|
|
|
|
|
Clock* clock,
|
|
|
|
|
rtc::scoped_refptr<PendingTaskSafetyFlag> alive_flag)
|
2019-01-18 10:30:54 +01:00
|
|
|
: task_queue_(task_queue),
|
2022-01-24 17:12:35 +01:00
|
|
|
precision_(precision),
|
2020-05-18 14:53:42 +02:00
|
|
|
clock_(clock),
|
2021-06-11 18:39:17 +02:00
|
|
|
next_run_time_(clock_->CurrentTime() + first_delay),
|
|
|
|
|
alive_flag_(std::move(alive_flag)) {}
|
2019-01-18 10:30:54 +01:00
|
|
|
|
|
|
|
|
RepeatingTaskBase::~RepeatingTaskBase() = default;
|
|
|
|
|
|
|
|
|
|
bool RepeatingTaskBase::Run() {
|
2020-05-15 10:12:36 +02:00
|
|
|
RTC_DCHECK_RUN_ON(task_queue_);
|
2019-01-18 10:30:54 +01:00
|
|
|
// Return true to tell the TaskQueue to destruct this object.
|
2021-06-11 18:39:17 +02:00
|
|
|
if (!alive_flag_->alive())
|
2019-01-18 10:30:54 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
TimeDelta delay = RunClosure();
|
2022-01-17 15:20:24 +01:00
|
|
|
RTC_DCHECK_GE(delay, TimeDelta::Zero());
|
2019-01-18 10:30:54 +01:00
|
|
|
|
2022-01-17 15:20:24 +01:00
|
|
|
// A delay of +infinity means that the task should not be run again.
|
|
|
|
|
// Alternatively, the closure might have stopped this task. In either which
|
|
|
|
|
// case we return true to destruct this object.
|
|
|
|
|
if (delay.IsPlusInfinity() || !alive_flag_->alive())
|
2019-01-18 10:30:54 +01:00
|
|
|
return true;
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2020-05-18 14:53:42 +02:00
|
|
|
TimeDelta lost_time = clock_->CurrentTime() - next_run_time_;
|
2019-01-18 10:30:54 +01:00
|
|
|
next_run_time_ += delay;
|
|
|
|
|
delay -= lost_time;
|
2019-02-04 16:39:28 +01:00
|
|
|
delay = std::max(delay, TimeDelta::Zero());
|
|
|
|
|
|
2022-01-24 17:12:35 +01:00
|
|
|
task_queue_->PostDelayedTaskWithPrecision(precision_, absl::WrapUnique(this),
|
|
|
|
|
delay.ms());
|
2019-01-18 10:30:54 +01:00
|
|
|
|
|
|
|
|
// Return false to tell the TaskQueue to not destruct this object since we
|
|
|
|
|
// have taken ownership with absl::WrapUnique.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc_repeating_task_impl
|
|
|
|
|
|
|
|
|
|
void RepeatingTaskHandle::Stop() {
|
|
|
|
|
if (repeating_task_) {
|
2021-06-11 18:39:17 +02:00
|
|
|
repeating_task_->SetNotAlive();
|
2019-01-18 10:30:54 +01:00
|
|
|
repeating_task_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RepeatingTaskHandle::Running() const {
|
|
|
|
|
return repeating_task_ != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 11:11:32 +02:00
|
|
|
namespace webrtc_repeating_task_impl {
|
|
|
|
|
// These methods are empty, but can be externally equipped with actions using
|
|
|
|
|
// dtrace.
|
|
|
|
|
void RepeatingTaskHandleDTraceProbeStart() {}
|
|
|
|
|
void RepeatingTaskHandleDTraceProbeDelayedStart() {}
|
|
|
|
|
void RepeatingTaskImplDTraceProbeRun() {}
|
|
|
|
|
} // namespace webrtc_repeating_task_impl
|
2019-01-18 10:30:54 +01:00
|
|
|
} // namespace webrtc
|