OLD | NEW |
| (Empty) |
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/quic/quic_alarm.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "net/quic/quic_flags.h" | |
9 | |
10 namespace net { | |
11 | |
12 QuicAlarm::QuicAlarm(QuicArenaScopedPtr<Delegate> delegate) | |
13 : delegate_(std::move(delegate)), deadline_(QuicTime::Zero()) {} | |
14 | |
15 QuicAlarm::~QuicAlarm() {} | |
16 | |
17 void QuicAlarm::Set(QuicTime new_deadline) { | |
18 DCHECK(!IsSet()); | |
19 DCHECK(new_deadline.IsInitialized()); | |
20 deadline_ = new_deadline; | |
21 SetImpl(); | |
22 } | |
23 | |
24 void QuicAlarm::Cancel() { | |
25 if (!IsSet()) { | |
26 // Don't try to cancel an alarm that hasn't been set. | |
27 return; | |
28 } | |
29 deadline_ = QuicTime::Zero(); | |
30 CancelImpl(); | |
31 } | |
32 | |
33 void QuicAlarm::Update(QuicTime new_deadline, QuicTime::Delta granularity) { | |
34 if (!new_deadline.IsInitialized()) { | |
35 Cancel(); | |
36 return; | |
37 } | |
38 if (std::abs((new_deadline - deadline_).ToMicroseconds()) < | |
39 granularity.ToMicroseconds()) { | |
40 return; | |
41 } | |
42 if (FLAGS_quic_change_alarms_efficiently) { | |
43 const bool was_set = IsSet(); | |
44 deadline_ = new_deadline; | |
45 if (was_set) { | |
46 UpdateImpl(); | |
47 } else { | |
48 SetImpl(); | |
49 } | |
50 } else { | |
51 Cancel(); | |
52 Set(new_deadline); | |
53 } | |
54 } | |
55 | |
56 bool QuicAlarm::IsSet() const { | |
57 return deadline_.IsInitialized(); | |
58 } | |
59 | |
60 void QuicAlarm::Fire() { | |
61 if (!IsSet()) { | |
62 return; | |
63 } | |
64 | |
65 deadline_ = QuicTime::Zero(); | |
66 delegate_->OnAlarm(); | |
67 } | |
68 | |
69 void QuicAlarm::UpdateImpl() { | |
70 // CancelImpl and SetImpl take the new deadline by way of the deadline_ | |
71 // member, so save and restore deadline_ before canceling. | |
72 const QuicTime new_deadline = deadline_; | |
73 | |
74 deadline_ = QuicTime::Zero(); | |
75 CancelImpl(); | |
76 | |
77 deadline_ = new_deadline; | |
78 SetImpl(); | |
79 } | |
80 | |
81 } // namespace net | |
OLD | NEW |