Apollo  6.0
Open source self driving car software
scheduler.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_SCHEDULER_SCHEDULER_H_
18 #define CYBER_SCHEDULER_SCHEDULER_H_
19 
20 #include <unistd.h>
21 
22 #include <atomic>
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 #include <string>
27 #include <thread>
28 #include <unordered_map>
29 #include <vector>
30 
31 #include "cyber/proto/choreography_conf.pb.h"
32 
35 #include "cyber/common/log.h"
36 #include "cyber/common/macros.h"
37 #include "cyber/common/types.h"
42 
43 namespace apollo {
44 namespace cyber {
45 namespace scheduler {
46 
53 using apollo::cyber::proto::InnerThread;
54 
55 class Processor;
56 class ProcessorContext;
57 
58 class Scheduler {
59  public:
60  virtual ~Scheduler() {}
61  static Scheduler* Instance();
62 
63  bool CreateTask(const RoutineFactory& factory, const std::string& name);
64  bool CreateTask(std::function<void()>&& func, const std::string& name,
65  std::shared_ptr<DataVisitorBase> visitor = nullptr);
66  bool NotifyTask(uint64_t crid);
67 
68  void Shutdown();
69  uint32_t TaskPoolSize() { return task_pool_size_; }
70 
71  virtual bool RemoveTask(const std::string& name) = 0;
72 
74  void SetInnerThreadAttr(const std::string& name, std::thread* thr);
75 
76  virtual bool DispatchTask(const std::shared_ptr<CRoutine>&) = 0;
77  virtual bool NotifyProcessor(uint64_t crid) = 0;
78  virtual bool RemoveCRoutine(uint64_t crid) = 0;
79 
80  void CheckSchedStatus();
81 
83  const std::unordered_map<std::string, InnerThread>& confs) {
84  inner_thr_confs_ = confs;
85  }
86 
87  protected:
88  Scheduler() : stop_(false) {}
89 
92  std::mutex cr_wl_mtx_;
93 
94  std::unordered_map<uint64_t, std::shared_ptr<CRoutine>> id_cr_;
95  std::vector<std::shared_ptr<ProcessorContext>> pctxs_;
96  std::vector<std::shared_ptr<Processor>> processors_;
97 
98  std::unordered_map<std::string, InnerThread> inner_thr_confs_;
99 
101  uint32_t proc_num_ = 0;
102  uint32_t task_pool_size_ = 0;
103  std::atomic<bool> stop_;
104 };
105 
106 } // namespace scheduler
107 } // namespace cyber
108 } // namespace apollo
109 
110 #endif // CYBER_SCHEDULER_SCHEDULER_H_
void(* func)(void *)
Definition: routine_context.h:41
Definition: croutine.h:40
void SetInnerThreadAttr(const std::string &name, std::thread *thr)
AtomicHashMap< uint64_t, MutexWrapper * > id_map_mutex_
Definition: scheduler.h:91
uint32_t TaskPoolSize()
Definition: scheduler.h:69
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: scheduler.h:58
Definition: atomic_rw_lock.h:36
std::vector< std::shared_ptr< ProcessorContext > > pctxs_
Definition: scheduler.h:95
uint32_t proc_num_
Definition: scheduler.h:101
Definition: routine_factory.h:33
virtual bool RemoveTask(const std::string &name)=0
AtomicRWLock id_cr_lock_
Definition: scheduler.h:90
A implementation of lock-free fixed size hash map.
Definition: atomic_hash_map.h:40
Definition: rw_lock_guard.h:35
std::unordered_map< uint64_t, std::shared_ptr< CRoutine > > id_cr_
Definition: scheduler.h:94
std::mutex cr_wl_mtx_
Definition: scheduler.h:92
virtual ~Scheduler()
Definition: scheduler.h:60
Definition: data_visitor_base.h:33
virtual bool RemoveCRoutine(uint64_t crid)=0
bool CreateTask(const RoutineFactory &factory, const std::string &name)
Scheduler()
Definition: scheduler.h:88
uint32_t task_pool_size_
Definition: scheduler.h:102
void SetInnerThreadConfs(const std::unordered_map< std::string, InnerThread > &confs)
Definition: scheduler.h:82
std::vector< std::shared_ptr< Processor > > processors_
Definition: scheduler.h:96
std::atomic< bool > stop_
Definition: scheduler.h:103
virtual bool DispatchTask(const std::shared_ptr< CRoutine > &)=0
std::string process_level_cpuset_
Definition: scheduler.h:100
std::unordered_map< std::string, InnerThread > inner_thr_confs_
Definition: scheduler.h:98
virtual bool NotifyProcessor(uint64_t crid)=0