Apollo  6.0
Open source self driving car software
classic_context.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_POLICY_CLASSIC_CONTEXT_H_
18 #define CYBER_SCHEDULER_POLICY_CLASSIC_CONTEXT_H_
19 
20 #include <array>
21 #include <functional>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
33 
34 namespace apollo {
35 namespace cyber {
36 namespace scheduler {
37 
38 static constexpr uint32_t MAX_PRIO = 20;
39 
40 #define DEFAULT_GROUP_NAME "default_grp"
41 
42 using CROUTINE_QUEUE = std::vector<std::shared_ptr<CRoutine>>;
43 using MULTI_PRIO_QUEUE = std::array<CROUTINE_QUEUE, MAX_PRIO>;
44 using CR_GROUP = std::unordered_map<std::string, MULTI_PRIO_QUEUE>;
45 using LOCK_QUEUE = std::array<base::AtomicRWLock, MAX_PRIO>;
46 using RQ_LOCK_GROUP = std::unordered_map<std::string, LOCK_QUEUE>;
47 
48 using GRP_WQ_MUTEX = std::unordered_map<std::string, MutexWrapper>;
49 using GRP_WQ_CV = std::unordered_map<std::string, CvWrapper>;
50 using NOTIFY_GRP = std::unordered_map<std::string, int>;
51 
53  public:
55  explicit ClassicContext(const std::string &group_name);
56 
57  std::shared_ptr<CRoutine> NextRoutine() override;
58  void Wait() override;
59  void Shutdown() override;
60 
61  static void Notify(const std::string &group_name);
62  static bool RemoveCRoutine(const std::shared_ptr<CRoutine> &cr);
63 
66  alignas(CACHELINE_SIZE) static GRP_WQ_CV cv_wq_;
69 
70  private:
71  void InitGroup(const std::string &group_name);
72 
73  std::chrono::steady_clock::time_point wake_time_;
74  bool need_sleep_ = false;
75 
76  MULTI_PRIO_QUEUE *multi_pri_rq_ = nullptr;
77  LOCK_QUEUE *lq_ = nullptr;
78  MutexWrapper *mtx_wrapper_ = nullptr;
79  CvWrapper *cw_ = nullptr;
80 
81  std::string current_grp;
82 };
83 
84 } // namespace scheduler
85 } // namespace cyber
86 } // namespace apollo
87 
88 #endif // CYBER_SCHEDULER_POLICY_CLASSIC_CONTEXT_H_
Definition: processor_context.h:33
std::unordered_map< std::string, int > NOTIFY_GRP
Definition: classic_context.h:50
std::unordered_map< std::string, MULTI_PRIO_QUEUE > CR_GROUP
Definition: classic_context.h:44
static CR_GROUP cr_group_
Definition: classic_context.h:64
static RQ_LOCK_GROUP rq_locks_
Definition: classic_context.h:65
std::array< CROUTINE_QUEUE, MAX_PRIO > MULTI_PRIO_QUEUE
Definition: classic_context.h:43
Definition: mutex_wrapper.h:26
std::shared_ptr< CRoutine > NextRoutine() override
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
std::array< base::AtomicRWLock, MAX_PRIO > LOCK_QUEUE
Definition: classic_context.h:45
static GRP_WQ_CV cv_wq_
Definition: classic_context.h:66
Definition: cv_wrapper.h:26
static bool RemoveCRoutine(const std::shared_ptr< CRoutine > &cr)
std::unordered_map< std::string, CvWrapper > GRP_WQ_CV
Definition: classic_context.h:49
std::unordered_map< std::string, LOCK_QUEUE > RQ_LOCK_GROUP
Definition: classic_context.h:46
static NOTIFY_GRP notify_grp_
Definition: classic_context.h:68
#define CACHELINE_SIZE
Definition: macros.h:31
static GRP_WQ_MUTEX mtx_wq_
Definition: classic_context.h:67
Definition: classic_context.h:52
static void Notify(const std::string &group_name)
std::vector< std::shared_ptr< CRoutine > > CROUTINE_QUEUE
Definition: classic_context.h:42
std::unordered_map< std::string, MutexWrapper > GRP_WQ_MUTEX
Definition: classic_context.h:48