Apollo 10.0
自动驾驶开放平台
scheduler_factory.cc
浏览该文件的文档.
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_FACTORY_H_
18#define CYBER_SCHEDULER_SCHEDULER_FACTORY_H_
19
21
22#include <atomic>
23#include <string>
24#include <unordered_map>
25
27#include "cyber/common/file.h"
29#include "cyber/common/util.h"
33
34namespace apollo {
35namespace cyber {
36namespace scheduler {
37
43
44namespace {
45std::atomic<Scheduler*> instance = {nullptr};
46std::mutex mutex;
47} // namespace
48
50 Scheduler* obj = instance.load(std::memory_order_acquire);
51 if (obj == nullptr) {
52 std::lock_guard<std::mutex> lock(mutex);
53 obj = instance.load(std::memory_order_relaxed);
54 if (obj == nullptr) {
55 std::string policy("classic");
56 std::string conf("conf/");
57 conf.append(GlobalData::Instance()->ProcessGroup()).append(".conf");
58 auto cfg_file = GetAbsolutePath(WorkRoot(), conf);
60 if (PathExists(cfg_file) && GetProtoFromFile(cfg_file, &cfg)) {
61 policy = cfg.scheduler_conf().policy();
62 } else {
63 AWARN << "Scheduler conf named " << cfg_file
64 << " not found, use default.";
65 }
66 if (!policy.compare("classic")) {
67 obj = new SchedulerClassic();
68 } else if (!policy.compare("choreography")) {
69 obj = new SchedulerChoreography();
70 } else {
71 AWARN << "Invalid scheduler policy: " << policy;
72 obj = new SchedulerClassic();
73 }
74 instance.store(obj, std::memory_order_release);
75 }
76 }
77 return obj;
78}
79
80void CleanUp() {
81 Scheduler* obj = instance.load(std::memory_order_acquire);
82 if (obj != nullptr) {
83 obj->Shutdown();
84 }
85}
86
87} // namespace scheduler
88} // namespace cyber
89} // namespace apollo
90
91#endif // CYBER_SCHEDULER_SCHEDULER_FACTORY_H_
#define AWARN
Definition log.h:43
bool PathExists(const std::string &path)
Check if the path exists.
Definition file.cc:195
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
Definition file.cc:179
const std::string WorkRoot()
Definition environment.h:40
class register implement
Definition arena_queue.h:37
optional SchedulerConf scheduler_conf