Apollo  6.0
Open source self driving car software
task_manager.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_TASK_TASK_MANAGER_H_
18 #define CYBER_TASK_TASK_MANAGER_H_
19 
20 #include <atomic>
21 #include <future>
22 #include <memory>
23 #include <string>
24 #include <type_traits>
25 #include <utility>
26 #include <vector>
27 
30 
31 namespace apollo {
32 namespace cyber {
33 
34 class TaskManager {
35  public:
36  virtual ~TaskManager();
37 
38  void Shutdown();
39 
40  template <typename F, typename... Args>
41  auto Enqueue(F&& func, Args&&... args)
42  -> std::future<typename std::result_of<F(Args...)>::type> {
43  using return_type = typename std::result_of<F(Args...)>::type;
44  auto task = std::make_shared<std::packaged_task<return_type()>>(
45  std::bind(std::forward<F>(func), std::forward<Args>(args)...));
46  if (!stop_.load()) {
47  task_queue_->Enqueue([task]() { (*task)(); });
48  for (auto& task : tasks_) {
50  }
51  }
52  std::future<return_type> res(task->get_future());
53  return res;
54  }
55 
56  private:
57  uint32_t num_threads_ = 0;
58  uint32_t task_queue_size_ = 1000;
59  std::atomic<bool> stop_ = {false};
60  std::vector<uint64_t> tasks_;
61  std::shared_ptr<base::BoundedQueue<std::function<void()>>> task_queue_;
62  DECLARE_SINGLETON(TaskManager);
63 };
64 
65 } // namespace cyber
66 } // namespace apollo
67 
68 #endif // CYBER_TASK_TASK_MANAGER_H_
void(* func)(void *)
Definition: routine_context.h:41
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
auto Enqueue(F &&func, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
Definition: task_manager.h:41
Definition: task_manager.h:34