QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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 | |
| 7 | namespace quic { |
| 8 | |
| 9 | QuicAlarm::QuicAlarm(QuicArenaScopedPtr<Delegate> delegate) |
| 10 | : delegate_(std::move(delegate)), deadline_(QuicTime::Zero()) {} |
| 11 | |
| 12 | QuicAlarm::~QuicAlarm() {} |
| 13 | |
| 14 | void QuicAlarm::Set(QuicTime new_deadline) { |
| 15 | DCHECK(!IsSet()); |
| 16 | DCHECK(new_deadline.IsInitialized()); |
| 17 | deadline_ = new_deadline; |
| 18 | SetImpl(); |
| 19 | } |
| 20 | |
| 21 | void 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 | |
| 30 | void 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 | |
| 48 | bool QuicAlarm::IsSet() const { |
| 49 | return deadline_.IsInitialized(); |
| 50 | } |
| 51 | |
| 52 | void QuicAlarm::Fire() { |
| 53 | if (!IsSet()) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | deadline_ = QuicTime::Zero(); |
| 58 | delegate_->OnAlarm(); |
| 59 | } |
| 60 | |
| 61 | void 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 |