Apollo  6.0
Open source self driving car software
utility.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 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 #pragma once
17 
18 #include <atomic>
19 #include <condition_variable>
20 #include <functional>
21 #include <future>
22 #include <memory>
23 #include <mutex>
24 #include <queue>
25 #include <thread>
26 #include <utility>
27 #include <vector>
28 #define MAX_THREAD_NUM 4
29 
30 namespace apollo {
31 namespace drivers {
32 namespace robosense {
33 template <typename T>
34 class Queue {
35  public:
36  Queue() { is_task_finished = true; }
37  void push(const T &value) {
38  std::lock_guard<std::mutex> lock(m_mutex);
39  m_quque.push(value);
40  }
41 
42  void pop() {
43  if (!m_quque.empty()) {
44  std::lock_guard<std::mutex> lock(m_mutex);
45  m_quque.pop();
46  }
47  }
48 
49  void clear() {
50  std::queue<T> empty;
51  std::lock_guard<std::mutex> lock(m_mutex);
52  swap(empty, m_quque);
53  }
54 
55  public:
56  std::queue<T> m_quque;
57  std::atomic<bool> is_task_finished;
58 
59  private:
60  mutable std::mutex m_mutex;
61 };
62 
63 struct Thread {
64  Thread() { start = false; }
65  std::shared_ptr<std::thread> m_thread;
66  std::atomic<bool> start;
67 };
68 class ThreadPool {
69  private:
70  inline ThreadPool();
71 
72  public:
73  typedef std::shared_ptr<ThreadPool> Ptr;
74  ThreadPool(ThreadPool &) = delete;
75  ThreadPool &operator=(const ThreadPool &) = delete;
76  ~ThreadPool();
77 
78  public:
79  static Ptr getInstance();
80  int idlCount();
81  template <class F, class... Args>
82  inline auto commit(F &&f, Args &&... args)
83  -> std::future<decltype(f(args...))> {
84  if (stoped.load())
85  throw std::runtime_error("Commit on ThreadPool is stopped.");
86  using RetType = decltype(f(args...));
87  auto task = std::make_shared<std::packaged_task<RetType()>>(
88  std::bind(std::forward<F>(f), std::forward<Args>(args)...)); // wtf !
89  std::future<RetType> future = task->get_future();
90  {
91  std::lock_guard<std::mutex> lock{m_lock};
92  tasks.emplace([task]() { (*task)(); });
93  }
94  cv_task.notify_one();
95  return future;
96  }
97 
98  private:
99  using Task = std::function<void()>;
100  std::vector<std::thread> pool;
101  std::queue<Task> tasks;
102  std::mutex m_lock;
103  std::condition_variable cv_task;
104  std::atomic<bool> stoped;
105  std::atomic<int> idl_thr_num;
106  static Ptr instance_ptr;
107  static std::mutex instance_mutex;
108 };
109 
110 } // namespace robosense
111 } // namespace drivers
112 } // namespace apollo
Definition: utility.h:34
void push(const T &value)
Definition: utility.h:37
Queue()
Definition: utility.h:36
void clear()
Definition: utility.h:49
std::shared_ptr< ThreadPool > Ptr
Definition: utility.h:73
std::atomic< bool > start
Definition: utility.h:66
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void pop()
Definition: utility.h:42
std::shared_ptr< std::thread > m_thread
Definition: utility.h:65
Definition: utility.h:63
std::atomic< bool > is_task_finished
Definition: utility.h:57
auto commit(F &&f, Args &&... args) -> std::future< decltype(f(args...))>
Definition: utility.h:82
apollo::cyber::base::std value
Thread()
Definition: utility.h:64
std::queue< T > m_quque
Definition: utility.h:56