1
#pragma once
2

            
3
#include <chrono>
4
#include <functional>
5
#include <memory>
6

            
7
#include "envoy/common/pure.h"
8
#include "envoy/common/time.h"
9
#include "envoy/event/schedulable_cb.h"
10

            
11
namespace Envoy {
12

            
13
class ScopeTrackedObject;
14

            
15
namespace Event {
16

            
17
class Dispatcher;
18

            
19
/**
20
 * Callback invoked when a timer event fires.
21
 */
22
using TimerCb = std::function<void()>;
23

            
24
/**
25
 * An abstract timer event. Free the timer to unregister any pending timeouts. Must be freed before
26
 * the dispatcher is torn down.
27
 */
28
class Timer {
29
public:
30
546821
  virtual ~Timer() = default;
31

            
32
  /**
33
   * Disable a pending timeout without destroying the underlying timer.
34
   */
35
  virtual void disableTimer() PURE;
36

            
37
  /**
38
   * Enable a pending timeout. If a timeout is already pending, it will be reset to the new timeout.
39
   *
40
   * @param ms supplies the duration of the alarm in milliseconds.
41
   * @param object supplies an optional scope for the duration of the alarm.
42
   */
43
  virtual void enableTimer(std::chrono::milliseconds ms,
44
                           const ScopeTrackedObject* object = nullptr) PURE;
45

            
46
  /**
47
   * Enable a pending high resolution timeout. If a timeout is already pending, it will be reset to
48
   * the new timeout.
49
   *
50
   * @param us supplies the duration of the alarm in microseconds.
51
   * @param object supplies an optional scope for the duration of the alarm.
52
   */
53
  virtual void enableHRTimer(std::chrono::microseconds us,
54
                             const ScopeTrackedObject* object = nullptr) PURE;
55
  /**
56
   * Return whether the timer is currently armed.
57
   */
58
  virtual bool enabled() PURE;
59
};
60

            
61
using TimerPtr = std::unique_ptr<Timer>;
62

            
63
class Scheduler {
64
public:
65
155656
  virtual ~Scheduler() = default;
66

            
67
  /**
68
   * Creates a timer.
69
   */
70
  virtual TimerPtr createTimer(const TimerCb& cb, Dispatcher& dispatcher) PURE;
71
};
72

            
73
using SchedulerPtr = std::unique_ptr<Scheduler>;
74

            
75
/**
76
 * Interface providing a mechanism to measure time and set timers that run callbacks
77
 * when the timer fires.
78
 */
79
class TimeSystem : public TimeSource {
80
public:
81
539736
  ~TimeSystem() override = default;
82

            
83
  using Duration = MonotonicTime::duration;
84
  using Nanoseconds = std::chrono::nanoseconds;
85
  using Microseconds = std::chrono::microseconds;
86
  using Milliseconds = std::chrono::milliseconds;
87
  using Seconds = std::chrono::seconds;
88

            
89
  /**
90
   * Creates a timer factory. This indirection enables thread-local timer-queue management,
91
   * so servers can have a separate timer-factory in each thread.
92
   */
93
  virtual SchedulerPtr createScheduler(Scheduler& base_scheduler,
94
                                       CallbackScheduler& cb_scheduler) PURE;
95
};
96

            
97
} // namespace Event
98
} // namespace Envoy