Apollo 10.0
自动驾驶开放平台
croutine.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
18
19#include <algorithm>
20#include <utility>
21
24#include "cyber/common/log.h"
26
27namespace apollo {
28namespace cyber {
29namespace croutine {
30
31thread_local CRoutine *CRoutine::current_routine_ = nullptr;
32thread_local char *CRoutine::main_stack_ = nullptr;
33
34namespace {
35std::shared_ptr<base::CCObjectPool<RoutineContext>> context_pool = nullptr;
36std::once_flag pool_init_flag;
37
38void CRoutineEntry(void *arg) {
39 CRoutine *r = static_cast<CRoutine *>(arg);
40 r->Run();
42}
43} // namespace
44
45CRoutine::CRoutine(const std::function<void()> &func) : func_(func) {
46 std::call_once(pool_init_flag, [&]() {
47 uint32_t routine_num = common::GlobalData::Instance()->ComponentNums();
48 auto &global_conf = common::GlobalData::Instance()->Config();
49 if (global_conf.has_scheduler_conf() &&
50 global_conf.scheduler_conf().has_routine_num()) {
51 routine_num =
52 std::max(routine_num, global_conf.scheduler_conf().routine_num());
53 }
54 context_pool.reset(new base::CCObjectPool<RoutineContext>(routine_num));
55 });
56
57 context_ = context_pool->GetObject();
58 if (context_ == nullptr) {
59 AWARN << "Maximum routine context number exceeded! Please check "
60 "[routine_num] in config file.";
61 context_.reset(new RoutineContext());
62 }
63
64 MakeContext(CRoutineEntry, this, context_.get());
65 state_ = RoutineState::READY;
66 updated_.test_and_set(std::memory_order_release);
67}
68
69CRoutine::~CRoutine() { context_ = nullptr; }
70
72 if (cyber_unlikely(force_stop_)) {
74 return state_;
75 }
76
77 if (cyber_unlikely(state_ != RoutineState::READY)) {
78 AERROR << "Invalid Routine State!";
79 return state_;
80 }
81
82 current_routine_ = this;
84 current_routine_ = nullptr;
85 return state_;
86}
87
88void CRoutine::Stop() { force_stop_ = true; }
89
90} // namespace croutine
91} // namespace cyber
92} // namespace apollo
CRoutine(const RoutineFunc &func)
Definition croutine.cc:45
#define cyber_unlikely(x)
Definition macros.h:30
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
void SwapContext(char **src_sp, char **dest_sp)
void MakeContext(const func &f1, const void *arg, RoutineContext *ctx)
class register implement
Definition arena_queue.h:37