Apollo 10.0
自动驾驶开放平台
poll_handler.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 "cyber/common/log.h"
20#include "cyber/io/poller.h"
22
23namespace apollo {
24namespace cyber {
25namespace io {
26
27using croutine::CRoutine;
29
31 : fd_(fd), is_read_(false), is_blocking_(false), routine_(nullptr) {}
32
33bool PollHandler::Block(int timeout_ms, bool is_read) {
34 if (!Check(timeout_ms)) {
35 return false;
36 }
37
38 if (is_blocking_.exchange(true)) {
39 AINFO << "poll handler is blocking.";
40 return false;
41 }
42
43 Fill(timeout_ms, is_read);
44 if (!Poller::Instance()->Register(request_)) {
45 is_blocking_.store(false);
46 return false;
47 }
48
49 routine_->Yield(RoutineState::IO_WAIT);
50
51 bool result = false;
52 uint32_t target_events = is_read ? EPOLLIN : EPOLLOUT;
53 if (response_.events & target_events) {
54 result = true;
55 }
56 is_blocking_.store(false);
57
58 return result;
59}
60
62 is_blocking_.store(false);
63 return Poller::Instance()->Unregister(request_);
64}
65
66bool PollHandler::Check(int timeout_ms) {
67 if (timeout_ms == 0) {
68 AINFO << "timeout[" << timeout_ms
69 << "] must be larger than zero or less than zero.";
70 return false;
71 }
72
73 if (fd_ < 0) {
74 AERROR << "invalid fd[" << fd_ << "]";
75 return false;
76 }
77
78 routine_ = CRoutine::GetCurrentRoutine();
79 if (routine_ == nullptr) {
80 AERROR << "routine nullptr, please use IO in routine context.";
81 return false;
82 }
83
84 return true;
85}
86
87void PollHandler::Fill(int timeout_ms, bool is_read) {
88 is_read_.store(is_read);
89
90 request_.fd = fd_;
91 request_.events = EPOLLET | EPOLLONESHOT;
92 if (is_read) {
93 request_.events |= EPOLLIN;
94 } else {
95 request_.events |= EPOLLOUT;
96 }
97 request_.timeout_ms = timeout_ms;
98 request_.callback =
99 std::bind(&PollHandler::ResponseCallback, this, std::placeholders::_1);
100}
101
102void PollHandler::ResponseCallback(const PollResponse& rsp) {
103 if (!is_blocking_.load() || routine_ == nullptr) {
104 return;
105 }
106
107 response_ = rsp;
108
109 if (routine_->state() == RoutineState::IO_WAIT) {
110 scheduler::Instance()->NotifyTask(routine_->id());
111 }
112}
113
114} // namespace io
115} // namespace cyber
116} // namespace apollo
static CRoutine * GetCurrentRoutine()
Definition croutine.h:135
RoutineState state() const
Definition croutine.h:147
bool Block(int timeout_ms, bool is_read)
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
std::function< void(const PollResponse &)> callback
Definition poll_data.h:39