Apollo  6.0
Open source self driving car software
timing_wheel.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_TIMING_WHEEL_H_
18 #define CYBER_TIMER_TIMING_WHEEL_H_
19 
20 #include <future>
21 #include <list>
22 #include <memory>
23 #include <thread>
24 
25 #include "cyber/common/log.h"
26 #include "cyber/common/macros.h"
28 #include "cyber/time/rate.h"
30 
31 namespace apollo {
32 namespace cyber {
33 
34 struct TimerTask;
35 
36 static const uint64_t WORK_WHEEL_SIZE = 512;
37 static const uint64_t ASSISTANT_WHEEL_SIZE = 64;
38 static const uint64_t TIMER_RESOLUTION_MS = 2;
39 static const uint64_t TIMER_MAX_INTERVAL_MS =
40  WORK_WHEEL_SIZE * ASSISTANT_WHEEL_SIZE * TIMER_RESOLUTION_MS;
41 
42 class TimingWheel {
43  public:
45  if (running_) {
46  Shutdown();
47  }
48  }
49 
50  void Start();
51 
52  void Shutdown();
53 
54  void Tick();
55 
56  void AddTask(const std::shared_ptr<TimerTask>& task);
57 
58  void AddTask(const std::shared_ptr<TimerTask>& task,
59  const uint64_t current_work_wheel_index);
60 
61  void Cascade(const uint64_t assistant_wheel_index);
62 
63  void TickFunc();
64 
65  inline uint64_t TickCount() const { return tick_count_; }
66 
67  private:
68  inline uint64_t GetWorkWheelIndex(const uint64_t index) {
69  return index & (WORK_WHEEL_SIZE - 1);
70  }
71  inline uint64_t GetAssistantWheelIndex(const uint64_t index) {
72  return index & (ASSISTANT_WHEEL_SIZE - 1);
73  }
74 
75  bool running_ = false;
76  uint64_t tick_count_ = 0;
77  std::mutex running_mutex_;
78  TimerBucket work_wheel_[WORK_WHEEL_SIZE];
79  TimerBucket assistant_wheel_[ASSISTANT_WHEEL_SIZE];
80  uint64_t current_work_wheel_index_ = 0;
81  std::mutex current_work_wheel_index_mutex_;
82  uint64_t current_assistant_wheel_index_ = 0;
83  std::mutex current_assistant_wheel_index_mutex_;
84  std::thread tick_thread_;
85 
87 };
88 
89 } // namespace cyber
90 } // namespace apollo
91 
92 #endif // CYBER_TIMER_TIMING_WHEEL_H_
Definition: timing_wheel.h:42
Definition: timer_bucket.h:29
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
#define DECLARE_SINGLETON(classname)
Definition: macros.h:52
void Cascade(const uint64_t assistant_wheel_index)
uint64_t TickCount() const
Definition: timing_wheel.h:65
~TimingWheel()
Definition: timing_wheel.h:44
void AddTask(const std::shared_ptr< TimerTask > &task)