blob: e31c7aa145aee0fbae530b2242158c876fc7f829 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/third_party/quiche/src/quic/core/quic_alarm.h"
6
7namespace quic {
8
9QuicAlarm::QuicAlarm(QuicArenaScopedPtr<Delegate> delegate)
10 : delegate_(std::move(delegate)), deadline_(QuicTime::Zero()) {}
11
12QuicAlarm::~QuicAlarm() {}
13
14void QuicAlarm::Set(QuicTime new_deadline) {
15 DCHECK(!IsSet());
16 DCHECK(new_deadline.IsInitialized());
17 deadline_ = new_deadline;
18 SetImpl();
19}
20
21void QuicAlarm::Cancel() {
22 if (!IsSet()) {
23 // Don't try to cancel an alarm that hasn't been set.
24 return;
25 }
26 deadline_ = QuicTime::Zero();
27 CancelImpl();
28}
29
30void QuicAlarm::Update(QuicTime new_deadline, QuicTime::Delta granularity) {
31 if (!new_deadline.IsInitialized()) {
32 Cancel();
33 return;
34 }
35 if (std::abs((new_deadline - deadline_).ToMicroseconds()) <
36 granularity.ToMicroseconds()) {
37 return;
38 }
39 const bool was_set = IsSet();
40 deadline_ = new_deadline;
41 if (was_set) {
42 UpdateImpl();
43 } else {
44 SetImpl();
45 }
46}
47
48bool QuicAlarm::IsSet() const {
49 return deadline_.IsInitialized();
50}
51
52void QuicAlarm::Fire() {
53 if (!IsSet()) {
54 return;
55 }
56
57 deadline_ = QuicTime::Zero();
58 delegate_->OnAlarm();
59}
60
61void QuicAlarm::UpdateImpl() {
62 // CancelImpl and SetImpl take the new deadline by way of the deadline_
63 // member, so save and restore deadline_ before canceling.
64 const QuicTime new_deadline = deadline_;
65
66 deadline_ = QuicTime::Zero();
67 CancelImpl();
68
69 deadline_ = new_deadline;
70 SetImpl();
71}
72
73} // namespace quic