1 /*
2 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10 #include "net/dcsctp/timer/task_queue_timeout.h"
11
12 #include "api/task_queue/pending_task_safety_flag.h"
13 #include "api/units/time_delta.h"
14 #include "rtc_base/logging.h"
15
16 namespace dcsctp {
17
TaskQueueTimeout(TaskQueueTimeoutFactory & parent,webrtc::TaskQueueBase::DelayPrecision precision)18 TaskQueueTimeoutFactory::TaskQueueTimeout::TaskQueueTimeout(
19 TaskQueueTimeoutFactory& parent,
20 webrtc::TaskQueueBase::DelayPrecision precision)
21 : parent_(parent),
22 precision_(precision),
23 pending_task_safety_flag_(webrtc::PendingTaskSafetyFlag::Create()) {}
24
~TaskQueueTimeout()25 TaskQueueTimeoutFactory::TaskQueueTimeout::~TaskQueueTimeout() {
26 RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
27 pending_task_safety_flag_->SetNotAlive();
28 }
29
Start(DurationMs duration_ms,TimeoutID timeout_id)30 void TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms,
31 TimeoutID timeout_id) {
32 RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
33 RTC_DCHECK(timeout_expiration_ == TimeMs::InfiniteFuture());
34 timeout_expiration_ = parent_.get_time_() + duration_ms;
35 timeout_id_ = timeout_id;
36
37 if (timeout_expiration_ >= posted_task_expiration_) {
38 // There is already a running task, and it's scheduled to expire sooner than
39 // the new expiration time. Don't do anything; The `timeout_expiration_` has
40 // already been updated and if the delayed task _does_ expire and the timer
41 // hasn't been stopped, that will be noticed in the timeout handler, and the
42 // task will be re-scheduled. Most timers are stopped before they expire.
43 return;
44 }
45
46 if (posted_task_expiration_ != TimeMs::InfiniteFuture()) {
47 RTC_DLOG(LS_VERBOSE) << "New timeout duration is less than scheduled - "
48 "ghosting old delayed task.";
49 // There is already a scheduled delayed task, but its expiration time is
50 // further away than the new expiration, so it can't be used. It will be
51 // "killed" by replacing the safety flag. This is not expected to happen
52 // especially often; Mainly when a timer did exponential backoff and
53 // later recovered.
54 pending_task_safety_flag_->SetNotAlive();
55 pending_task_safety_flag_ = webrtc::PendingTaskSafetyFlag::Create();
56 }
57
58 posted_task_expiration_ = timeout_expiration_;
59 parent_.task_queue_.PostDelayedTaskWithPrecision(
60 precision_,
61 webrtc::SafeTask(
62 pending_task_safety_flag_,
63 [timeout_id, this]() {
64 RTC_DLOG(LS_VERBOSE) << "Timout expired: " << timeout_id.value();
65 RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
66 RTC_DCHECK(posted_task_expiration_ != TimeMs::InfiniteFuture());
67 posted_task_expiration_ = TimeMs::InfiniteFuture();
68
69 if (timeout_expiration_ == TimeMs::InfiniteFuture()) {
70 // The timeout was stopped before it expired. Very common.
71 } else {
72 // Note that the timeout might have been restarted, which updated
73 // `timeout_expiration_` but left the scheduled task running. So
74 // if it's not quite time to trigger the timeout yet, schedule a
75 // new delayed task with what's remaining and retry at that point
76 // in time.
77 DurationMs remaining = timeout_expiration_ - parent_.get_time_();
78 timeout_expiration_ = TimeMs::InfiniteFuture();
79 if (*remaining > 0) {
80 Start(remaining, timeout_id_);
81 } else {
82 // It has actually triggered.
83 RTC_DLOG(LS_VERBOSE)
84 << "Timout triggered: " << timeout_id.value();
85 parent_.on_expired_(timeout_id_);
86 }
87 }
88 }),
89 webrtc::TimeDelta::Millis(duration_ms.value()));
90 }
91
Stop()92 void TaskQueueTimeoutFactory::TaskQueueTimeout::Stop() {
93 // As the TaskQueue doesn't support deleting a posted task, just mark the
94 // timeout as not running.
95 RTC_DCHECK_RUN_ON(&parent_.thread_checker_);
96 timeout_expiration_ = TimeMs::InfiniteFuture();
97 }
98
99 } // namespace dcsctp
100