Apollo 10.0
自动驾驶开放平台
timer.h
浏览该文件的文档.
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
25namespace apollo {
26namespace cyber {
27
40 TimerOption(uint32_t period, std::function<void()> callback, bool oneshot)
42
48
54 uint32_t period = 0;
55
57 std::function<void()> callback;
58
63 bool oneshot;
64};
65
71class 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
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_
Used to perform oneshot or periodic timing tasks
Definition timer.h:71
void Stop()
Stop the timer
Definition timer.cc:142
void SetTimerOption(TimerOption opt)
Set the Timer Option object
Definition timer.cc:49
void Start()
Start the timer
Definition timer.cc:129
class register implement
Definition arena_queue.h:37
The options of timer
Definition timer.h:32
uint32_t period
The period of the timer, unit is ms max: 512 * 64 min: 1
Definition timer.h:54
TimerOption()
Default constructor for initializer list
Definition timer.h:47
bool oneshot
True: perform the callback only after the first timing cycle False: perform the callback every timed ...
Definition timer.h:63
TimerOption(uint32_t period, std::function< void()> callback, bool oneshot)
Construct a new Timer Option object
Definition timer.h:40
std::function< void()> callback
The task that the timer needs to perform
Definition timer.h:57