Apollo  6.0
Open source self driving car software
wait_strategy.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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_BASE_WAIT_STRATEGY_H_
18 #define CYBER_BASE_WAIT_STRATEGY_H_
19 
20 #include <chrono>
21 #include <condition_variable>
22 #include <cstdlib>
23 #include <mutex>
24 #include <thread>
25 
26 namespace apollo {
27 namespace cyber {
28 namespace base {
29 
30 class WaitStrategy {
31  public:
32  virtual void NotifyOne() {}
33  virtual void BreakAllWait() {}
34  virtual bool EmptyWait() = 0;
35  virtual ~WaitStrategy() {}
36 };
37 
39  public:
41  void NotifyOne() override { cv_.notify_one(); }
42 
43  bool EmptyWait() override {
44  std::unique_lock<std::mutex> lock(mutex_);
45  cv_.wait(lock);
46  return true;
47  }
48 
49  void BreakAllWait() override { cv_.notify_all(); }
50 
51  private:
52  std::mutex mutex_;
53  std::condition_variable cv_;
54 };
55 
57  public:
59  explicit SleepWaitStrategy(uint64_t sleep_time_us)
60  : sleep_time_us_(sleep_time_us) {}
61 
62  bool EmptyWait() override {
63  std::this_thread::sleep_for(std::chrono::microseconds(sleep_time_us_));
64  return true;
65  }
66 
67  void SetSleepTimeMicroSeconds(uint64_t sleep_time_us) {
68  sleep_time_us_ = sleep_time_us;
69  }
70 
71  private:
72  uint64_t sleep_time_us_ = 10000;
73 };
74 
76  public:
78  bool EmptyWait() override {
79  std::this_thread::yield();
80  return true;
81  }
82 };
83 
85  public:
87  bool EmptyWait() override { return true; }
88 };
89 
91  public:
93  explicit TimeoutBlockWaitStrategy(uint64_t timeout)
94  : time_out_(std::chrono::milliseconds(timeout)) {}
95 
96  void NotifyOne() override { cv_.notify_one(); }
97 
98  bool EmptyWait() override {
99  std::unique_lock<std::mutex> lock(mutex_);
100  if (cv_.wait_for(lock, time_out_) == std::cv_status::timeout) {
101  return false;
102  }
103  return true;
104  }
105 
106  void BreakAllWait() override { cv_.notify_all(); }
107 
108  void SetTimeout(uint64_t timeout) {
109  time_out_ = std::chrono::milliseconds(timeout);
110  }
111 
112  private:
113  std::mutex mutex_;
114  std::condition_variable cv_;
115  std::chrono::milliseconds time_out_;
116 };
117 
118 } // namespace base
119 } // namespace cyber
120 } // namespace apollo
121 
122 #endif // CYBER_BASE_WAIT_STRATEGY_H_
TimeoutBlockWaitStrategy(uint64_t timeout)
Definition: wait_strategy.h:93
Definition: wait_strategy.h:75
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void SetTimeout(uint64_t timeout)
Definition: wait_strategy.h:108
Definition: wait_strategy.h:30
BlockWaitStrategy()
Definition: wait_strategy.h:40
Definition: future.h:29
void BreakAllWait() override
Definition: wait_strategy.h:106
Definition: wait_strategy.h:90
bool EmptyWait() override
Definition: wait_strategy.h:43
virtual void BreakAllWait()
Definition: wait_strategy.h:33
bool EmptyWait() override
Definition: wait_strategy.h:87
YieldWaitStrategy()
Definition: wait_strategy.h:77
virtual void NotifyOne()
Definition: wait_strategy.h:32
void NotifyOne() override
Definition: wait_strategy.h:96
Definition: wait_strategy.h:56
bool EmptyWait() override
Definition: wait_strategy.h:62
void SetSleepTimeMicroSeconds(uint64_t sleep_time_us)
Definition: wait_strategy.h:67
BusySpinWaitStrategy()
Definition: wait_strategy.h:86
SleepWaitStrategy(uint64_t sleep_time_us)
Definition: wait_strategy.h:59
bool EmptyWait() override
Definition: wait_strategy.h:98
SleepWaitStrategy()
Definition: wait_strategy.h:58
TimeoutBlockWaitStrategy()
Definition: wait_strategy.h:92
void BreakAllWait() override
Definition: wait_strategy.h:49
bool EmptyWait() override
Definition: wait_strategy.h:78
void NotifyOne() override
Definition: wait_strategy.h:41
virtual ~WaitStrategy()
Definition: wait_strategy.h:35
Definition: wait_strategy.h:84
Definition: wait_strategy.h:38