Apollo 10.0
自动驾驶开放平台
wait_strategy.h
浏览该文件的文档.
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
26namespace apollo {
27namespace cyber {
28namespace base {
29
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_
SleepWaitStrategy(uint64_t sleep_time_us)
void SetSleepTimeMicroSeconds(uint64_t sleep_time_us)
class register implement
Definition arena_queue.h:37
Definition future.h:29