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 | #ifndef QUICHE_QUIC_CORE_QUIC_ALARM_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_ALARM_H_ |
| 7 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/quic/core/quic_arena_scoped_ptr.h" |
| 9 | #include "net/third_party/quiche/src/quic/core/quic_time.h" |
| 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
| 11 | |
| 12 | namespace quic { |
| 13 | |
| 14 | // Abstract class which represents an alarm which will go off at a |
| 15 | // scheduled time, and execute the |OnAlarm| method of the delegate. |
| 16 | // An alarm may be cancelled, in which case it may or may not be |
| 17 | // removed from the underlying scheduling system, but in either case |
| 18 | // the task will not be executed. |
| 19 | class QUIC_EXPORT_PRIVATE QuicAlarm { |
| 20 | public: |
| 21 | class QUIC_EXPORT_PRIVATE Delegate { |
| 22 | public: |
| 23 | virtual ~Delegate() {} |
| 24 | |
| 25 | // Invoked when the alarm fires. |
| 26 | virtual void OnAlarm() = 0; |
| 27 | }; |
| 28 | |
| 29 | explicit QuicAlarm(QuicArenaScopedPtr<Delegate> delegate); |
| 30 | QuicAlarm(const QuicAlarm&) = delete; |
| 31 | QuicAlarm& operator=(const QuicAlarm&) = delete; |
| 32 | virtual ~QuicAlarm(); |
| 33 | |
| 34 | // Sets the alarm to fire at |deadline|. Must not be called while |
| 35 | // the alarm is set. To reschedule an alarm, call Cancel() first, |
| 36 | // then Set(). |
| 37 | void Set(QuicTime new_deadline); |
| 38 | |
| 39 | // Cancels the alarm. May be called repeatedly. Does not |
| 40 | // guarantee that the underlying scheduling system will remove |
| 41 | // the alarm's associated task, but guarantees that the |
| 42 | // delegates OnAlarm method will not be called. |
| 43 | void Cancel(); |
| 44 | |
| 45 | // Cancels and sets the alarm if the |deadline| is farther from the current |
| 46 | // deadline than |granularity|, and otherwise does nothing. If |deadline| is |
| 47 | // not initialized, the alarm is cancelled. |
| 48 | void Update(QuicTime new_deadline, QuicTime::Delta granularity); |
| 49 | |
| 50 | // Returns true if |deadline_| has been set to a non-zero time. |
| 51 | bool IsSet() const; |
| 52 | |
| 53 | QuicTime deadline() const { return deadline_; } |
| 54 | |
| 55 | protected: |
| 56 | // Subclasses implement this method to perform the platform-specific |
| 57 | // scheduling of the alarm. Is called from Set() or Fire(), after the |
| 58 | // deadline has been updated. |
| 59 | virtual void SetImpl() = 0; |
| 60 | |
| 61 | // Subclasses implement this method to perform the platform-specific |
| 62 | // cancelation of the alarm. |
| 63 | virtual void CancelImpl() = 0; |
| 64 | |
| 65 | // Subclasses implement this method to perform the platform-specific update of |
| 66 | // the alarm if there exists a more optimal implementation than calling |
| 67 | // CancelImpl() and SetImpl(). |
| 68 | virtual void UpdateImpl(); |
| 69 | |
| 70 | // Called by subclasses when the alarm fires. Invokes the |
| 71 | // delegates |OnAlarm| if a delegate is set, and if the deadline |
| 72 | // has been exceeded. Implementations which do not remove the |
| 73 | // alarm from the underlying scheduler on Cancel() may need to handle |
| 74 | // the situation where the task executes before the deadline has been |
| 75 | // reached, in which case they need to reschedule the task and must not |
| 76 | // call invoke this method. |
| 77 | void Fire(); |
| 78 | |
| 79 | private: |
| 80 | QuicArenaScopedPtr<Delegate> delegate_; |
| 81 | QuicTime deadline_; |
| 82 | }; |
| 83 | |
| 84 | } // namespace quic |
| 85 | |
| 86 | #endif // QUICHE_QUIC_CORE_QUIC_ALARM_H_ |