Apollo  6.0
Open source self driving car software
timer.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_TIMER_TIMER_H_
18 #define CYBER_TIMER_TIMER_H_
19 
20 #include <atomic>
21 #include <memory>
22 
24 
25 namespace apollo {
26 namespace cyber {
27 
32 struct TimerOption {
40  TimerOption(uint32_t period, std::function<void()> callback, bool oneshot)
41  : period(period), callback(callback), oneshot(oneshot) {}
42 
47  TimerOption() : period(), callback(), oneshot() {}
48 
54  uint32_t period = 0;
55 
57  std::function<void()> callback;
58 
63  bool oneshot;
64 };
65 
71 class Timer {
72  public:
73  Timer();
74 
80  explicit Timer(TimerOption opt);
81 
90  Timer(uint32_t period, std::function<void()> callback, bool oneshot);
91  ~Timer();
92 
98  void SetTimerOption(TimerOption opt);
99 
104  void Start();
105 
110  void Stop();
111 
112  private:
113  bool InitTimerTask();
114  uint64_t timer_id_;
115  TimerOption timer_opt_;
116  TimingWheel* timing_wheel_ = nullptr;
117  std::shared_ptr<TimerTask> task_;
118  std::atomic<bool> started_ = {false};
119 };
120 
121 } // namespace cyber
122 } // namespace apollo
123 
124 #endif // CYBER_TIMER_TIMER_H_
Definition: timing_wheel.h:42
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
TimerOption()
Default constructor for initializer list.
Definition: timer.h:47
std::function< void()> callback
Definition: timer.h:57
bool oneshot
Definition: timer.h:63
Used to perform oneshot or periodic timing tasks.
Definition: timer.h:71
uint32_t period
The period of the timer, unit is ms max: 512 * 64 min: 1.
Definition: timer.h:54
The options of timer.
Definition: timer.h:32
TimerOption(uint32_t period, std::function< void()> callback, bool oneshot)
Construct a new Timer Option object.
Definition: timer.h:40