Apollo 11.0
自动驾驶开放平台
processor.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 <sched.h>
20#include <sys/resource.h>
21#include <sys/syscall.h>
22
23#include <chrono>
24
26#include "cyber/common/log.h"
28#include "cyber/time/time.h"
29
30namespace apollo {
31namespace cyber {
32namespace scheduler {
33
35
36Processor::Processor() { running_.store(true); }
37
39
41 tid_.store(static_cast<int>(syscall(SYS_gettid)));
42 AINFO << "processor_tid: " << tid_;
43 snap_shot_->processor_id.store(tid_);
44
45 while (cyber_likely(running_.load())) {
46 if (cyber_likely(context_ != nullptr)) {
47 auto croutine = context_->NextRoutine();
48 if (croutine) {
49 snap_shot_->execute_start_time.store(cyber::Time::Now().ToNanosecond());
50 snap_shot_->routine_name = croutine->name();
51 croutine->Resume();
52 croutine->Release();
53 } else {
54 snap_shot_->execute_start_time.store(0);
55 context_->Wait();
56 }
57 } else {
58 std::unique_lock<std::mutex> lk(mtx_ctx_);
59 cv_ctx_.wait_for(lk, std::chrono::milliseconds(10));
60 }
61 }
62}
63
65 if (!running_.exchange(false)) {
66 return;
67 }
68
69 if (context_) {
70 context_->Shutdown();
71 }
72
73 cv_ctx_.notify_one();
74 if (thread_.joinable()) {
75 thread_.join();
76 }
77}
78
79void Processor::BindContext(const std::shared_ptr<ProcessorContext>& context) {
80 context_ = context;
81 std::call_once(thread_flag_,
82 [this]() { thread_ = std::thread(&Processor::Run, this); });
83}
84
85std::atomic<pid_t>& Processor::Tid() {
86 while (tid_.load() == -1) {
87 cpu_relax();
88 }
89 return tid_;
90}
91
92} // namespace scheduler
93} // namespace cyber
94} // namespace apollo
static Time Now()
get the current time.
Definition time.cc:57
void BindContext(const std::shared_ptr< ProcessorContext > &context)
Definition processor.cc:79
std::atomic< pid_t > & Tid()
Definition processor.cc:85
void cpu_relax()
Definition macros.h:51
#define cyber_likely(x)
Definition macros.h:27
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37