27using croutine::CRoutine;
31 : fd_(fd), is_read_(false), is_blocking_(false), routine_(nullptr) {}
34 if (!Check(timeout_ms)) {
38 if (is_blocking_.exchange(
true)) {
39 AINFO <<
"poll handler is blocking.";
43 Fill(timeout_ms, is_read);
44 if (!Poller::Instance()->Register(request_)) {
45 is_blocking_.store(
false);
49 routine_->
Yield(RoutineState::IO_WAIT);
52 uint32_t target_events = is_read ? EPOLLIN : EPOLLOUT;
53 if (response_.
events & target_events) {
56 is_blocking_.store(
false);
62 is_blocking_.store(
false);
63 return Poller::Instance()->Unregister(request_);
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.";
74 AERROR <<
"invalid fd[" << fd_ <<
"]";
79 if (routine_ ==
nullptr) {
80 AERROR <<
"routine nullptr, please use IO in routine context.";
87void PollHandler::Fill(
int timeout_ms,
bool is_read) {
88 is_read_.store(is_read);
91 request_.
events = EPOLLET | EPOLLONESHOT;
93 request_.
events |= EPOLLIN;
95 request_.
events |= EPOLLOUT;
99 std::bind(&PollHandler::ResponseCallback,
this, std::placeholders::_1);
102void PollHandler::ResponseCallback(
const PollResponse& rsp) {
103 if (!is_blocking_.load() || routine_ ==
nullptr) {
109 if (routine_->
state() == RoutineState::IO_WAIT) {
static CRoutine * GetCurrentRoutine()
RoutineState state() const
bool Block(int timeout_ms, bool is_read)
bool NotifyTask(uint64_t crid)
std::function< void(const PollResponse &)> callback