13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci
163f4cbf05Sopenharmony_ci#include <atomic>
173f4cbf05Sopenharmony_ci#include <mutex>
183f4cbf05Sopenharmony_ci#include <ostream>
193f4cbf05Sopenharmony_ci#include <queue>
203f4cbf05Sopenharmony_ci#include <climits>
213f4cbf05Sopenharmony_ci#include <iostream>
223f4cbf05Sopenharmony_ci#include "utils_log.h"
233f4cbf05Sopenharmony_ci#include "common_event_sys_errors.h"
243f4cbf05Sopenharmony_ci#include "io_event_epoll.h"
253f4cbf05Sopenharmony_ci#include "io_event_reactor.h"
263f4cbf05Sopenharmony_ci
273f4cbf05Sopenharmony_cinamespace OHOS {
283f4cbf05Sopenharmony_cinamespace Utils {
293f4cbf05Sopenharmony_ci
303f4cbf05Sopenharmony_ciIOEventReactor::IOEventReactor()
313f4cbf05Sopenharmony_ci    :loopReady_(false), enabled_(false), count_(0), ioHandlers_(INIT_FD_NUMS), backend_(new IOEventEpoll()) {}
323f4cbf05Sopenharmony_ci
333f4cbf05Sopenharmony_ciIOEventReactor::~IOEventReactor()
343f4cbf05Sopenharmony_ci{
353f4cbf05Sopenharmony_ci    CleanUp();
363f4cbf05Sopenharmony_ci}
373f4cbf05Sopenharmony_ci
383f4cbf05Sopenharmony_ciErrCode IOEventReactor::SetUp()
393f4cbf05Sopenharmony_ci{
403f4cbf05Sopenharmony_ci    if (backend_ == nullptr) {
413f4cbf05Sopenharmony_ci        backend_ = std::make_unique<IOEventEpoll>();
423f4cbf05Sopenharmony_ci    }
433f4cbf05Sopenharmony_ci
443f4cbf05Sopenharmony_ci    ErrCode res = backend_->SetUp();
453f4cbf05Sopenharmony_ci    if (res != EVENT_SYS_ERR_OK) {
463f4cbf05Sopenharmony_ci        UTILS_LOGE("%{public}s: Backend start failed.", __FUNCTION__);
473f4cbf05Sopenharmony_ci        return res;
483f4cbf05Sopenharmony_ci    }
493f4cbf05Sopenharmony_ci
503f4cbf05Sopenharmony_ci    loopReady_ = true;
513f4cbf05Sopenharmony_ci    return res;
523f4cbf05Sopenharmony_ci}
533f4cbf05Sopenharmony_ci
543f4cbf05Sopenharmony_civoid IOEventReactor::InsertNodeFront(int fd, IOEventHandler* target)
553f4cbf05Sopenharmony_ci{
563f4cbf05Sopenharmony_ci    IOEventHandler* h = ioHandlers_[fd].head.get();
573f4cbf05Sopenharmony_ci    target->next_ = h->next_;
583f4cbf05Sopenharmony_ci    target->prev_ = h;
593f4cbf05Sopenharmony_ci    if (h->next_ != nullptr) {
603f4cbf05Sopenharmony_ci        h->next_->prev_ = target;
613f4cbf05Sopenharmony_ci    }
623f4cbf05Sopenharmony_ci    h->next_ = target;
633f4cbf05Sopenharmony_ci}
643f4cbf05Sopenharmony_ci
653f4cbf05Sopenharmony_civoid IOEventReactor::RemoveNode(IOEventHandler* target)
663f4cbf05Sopenharmony_ci{
673f4cbf05Sopenharmony_ci    target->prev_->next_ = target->next_;
683f4cbf05Sopenharmony_ci
693f4cbf05Sopenharmony_ci    if (target->next_ != nullptr) {
703f4cbf05Sopenharmony_ci        target->next_->prev_ = target->prev_;
713f4cbf05Sopenharmony_ci    }
723f4cbf05Sopenharmony_ci
733f4cbf05Sopenharmony_ci    target->prev_ = nullptr;
743f4cbf05Sopenharmony_ci    target->next_ = nullptr;
753f4cbf05Sopenharmony_ci}
763f4cbf05Sopenharmony_ci
773f4cbf05Sopenharmony_ciErrCode IOEventReactor::AddHandler(IOEventHandler* target)
783f4cbf05Sopenharmony_ci{
793f4cbf05Sopenharmony_ci    if (target == nullptr) {
803f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_NOT_FOUND;
813f4cbf05Sopenharmony_ci    }
823f4cbf05Sopenharmony_ci
833f4cbf05Sopenharmony_ci    if (target->fd_ == -1) {
843f4cbf05Sopenharmony_ci        UTILS_LOGE("%{public}s: Failed, Bad fd.", __FUNCTION__);
853f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_BADF;
863f4cbf05Sopenharmony_ci    }
873f4cbf05Sopenharmony_ci    if (target->prev_!=nullptr) {
883f4cbf05Sopenharmony_ci        UTILS_LOGW("%{public}s: Warning, already started.", __FUNCTION__);
893f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_ALREADY_STARTED;
903f4cbf05Sopenharmony_ci    }
913f4cbf05Sopenharmony_ci
923f4cbf05Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
933f4cbf05Sopenharmony_ci    int fd = target->fd_;
943f4cbf05Sopenharmony_ci    if (static_cast<size_t>(fd) > ioHandlers_.size() - 1u) {
953f4cbf05Sopenharmony_ci        UTILS_LOGD("%{public}s: Resize when fd: %{public}d", __FUNCTION__, fd);
963f4cbf05Sopenharmony_ci        ioHandlers_.resize(fd * EXPANSION_COEFF);
973f4cbf05Sopenharmony_ci    }
983f4cbf05Sopenharmony_ci
993f4cbf05Sopenharmony_ci    InsertNodeFront(fd, target);
1003f4cbf05Sopenharmony_ci
1013f4cbf05Sopenharmony_ci    if ((ioHandlers_[fd].events & target->events_) != target->events_) {
1023f4cbf05Sopenharmony_ci        if (backend_ == nullptr || !UpdateToDemultiplexer(target->fd_)) {
1033f4cbf05Sopenharmony_ci            UTILS_LOGE("%{public}s: Update fd: %{public}d to backend failed.", __FUNCTION__, target->fd_);
1043f4cbf05Sopenharmony_ci            return EVENT_SYS_ERR_FAILED;
1053f4cbf05Sopenharmony_ci        }
1063f4cbf05Sopenharmony_ci    }
1073f4cbf05Sopenharmony_ci
1083f4cbf05Sopenharmony_ci    target->enabled_ = true;
1093f4cbf05Sopenharmony_ci    count_++;
1103f4cbf05Sopenharmony_ci    return EVENT_SYS_ERR_OK;
1113f4cbf05Sopenharmony_ci}
1123f4cbf05Sopenharmony_ci
1133f4cbf05Sopenharmony_ciErrCode IOEventReactor::UpdateHandler(IOEventHandler* target)
1143f4cbf05Sopenharmony_ci{
1153f4cbf05Sopenharmony_ci    if (target == nullptr) {
1163f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_NOT_FOUND;
1173f4cbf05Sopenharmony_ci    }
1183f4cbf05Sopenharmony_ci
1193f4cbf05Sopenharmony_ci    if (target->fd_ == -1) {
1203f4cbf05Sopenharmony_ci        UTILS_LOGE("%{public}s: Failed, Bad fd.", __FUNCTION__);
1213f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_BADF;
1223f4cbf05Sopenharmony_ci    }
1233f4cbf05Sopenharmony_ci
1243f4cbf05Sopenharmony_ci    if (target->prev_!=nullptr) {
1253f4cbf05Sopenharmony_ci        if (!HasHandler(target)) {
1263f4cbf05Sopenharmony_ci            UTILS_LOGE("%{public}s: Failed, handler not found.", __FUNCTION__);
1273f4cbf05Sopenharmony_ci            return EVENT_SYS_ERR_NOT_FOUND;
1283f4cbf05Sopenharmony_ci        }
1293f4cbf05Sopenharmony_ci        if (backend_ == nullptr || !UpdateToDemultiplexer(target->fd_)) {
1303f4cbf05Sopenharmony_ci            UTILS_LOGE("%{public}s: Update fd: %{public}d to backend failed.", __FUNCTION__, target->fd_);
1313f4cbf05Sopenharmony_ci            return EVENT_SYS_ERR_FAILED;
1323f4cbf05Sopenharmony_ci        }
1333f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_OK;
1343f4cbf05Sopenharmony_ci    }
1353f4cbf05Sopenharmony_ci
1363f4cbf05Sopenharmony_ci    return AddHandler(target);
1373f4cbf05Sopenharmony_ci}
1383f4cbf05Sopenharmony_ci
1393f4cbf05Sopenharmony_ciErrCode IOEventReactor::RemoveHandler(IOEventHandler* target)
1403f4cbf05Sopenharmony_ci{
1413f4cbf05Sopenharmony_ci    if (target == nullptr) {
1423f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_NOT_FOUND;
1433f4cbf05Sopenharmony_ci    }
1443f4cbf05Sopenharmony_ci
1453f4cbf05Sopenharmony_ci    if (target->fd_ == -1) {
1463f4cbf05Sopenharmony_ci        UTILS_LOGE("%{public}s: Failed, Bad fd.", __FUNCTION__);
1473f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_BADF;
1483f4cbf05Sopenharmony_ci    }
1493f4cbf05Sopenharmony_ci
1503f4cbf05Sopenharmony_ci    target->enabled_ = false;
1513f4cbf05Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
1523f4cbf05Sopenharmony_ci
1533f4cbf05Sopenharmony_ci    if (!HasHandler(target)) {
1543f4cbf05Sopenharmony_ci        UTILS_LOGE("%{public}s Failed. Handler not found.", __FUNCTION__);
1553f4cbf05Sopenharmony_ci        target->enabled_=true;
1563f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_NOT_FOUND;
1573f4cbf05Sopenharmony_ci    }
1583f4cbf05Sopenharmony_ci
1593f4cbf05Sopenharmony_ci    RemoveNode(target);
1603f4cbf05Sopenharmony_ci
1613f4cbf05Sopenharmony_ci    if (backend_ == nullptr || !UpdateToDemultiplexer(target->fd_)) {
1623f4cbf05Sopenharmony_ci        UTILS_LOGE("%{public}s: Update fd: %{public}d to backend failed.", __FUNCTION__, target->fd_);
1633f4cbf05Sopenharmony_ci        target->enabled_=true;
1643f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_FAILED;
1653f4cbf05Sopenharmony_ci    }
1663f4cbf05Sopenharmony_ci
1673f4cbf05Sopenharmony_ci    count_--;
1683f4cbf05Sopenharmony_ci    return EVENT_SYS_ERR_OK;
1693f4cbf05Sopenharmony_ci}
1703f4cbf05Sopenharmony_ci
1713f4cbf05Sopenharmony_cibool IOEventReactor::HasHandler(IOEventHandler* target)
1723f4cbf05Sopenharmony_ci{
1733f4cbf05Sopenharmony_ci    for (IOEventHandler* cur = ioHandlers_[target->fd_].head.get(); cur != nullptr; cur = cur->next_) {
1743f4cbf05Sopenharmony_ci        if (cur == target) {
1753f4cbf05Sopenharmony_ci            return true;
1763f4cbf05Sopenharmony_ci        }
1773f4cbf05Sopenharmony_ci    }
1783f4cbf05Sopenharmony_ci
1793f4cbf05Sopenharmony_ci    return false;
1803f4cbf05Sopenharmony_ci}
1813f4cbf05Sopenharmony_ci
1823f4cbf05Sopenharmony_ciErrCode IOEventReactor::FindHandler(IOEventHandler* target)
1833f4cbf05Sopenharmony_ci{
1843f4cbf05Sopenharmony_ci    if (target == nullptr) {
1853f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_NOT_FOUND;
1863f4cbf05Sopenharmony_ci    }
1873f4cbf05Sopenharmony_ci
1883f4cbf05Sopenharmony_ci    if (target->fd_ == -1) {
1893f4cbf05Sopenharmony_ci        UTILS_LOGD("%{public}s: Failed, Bad fd.", __FUNCTION__);
1903f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_BADF;
1913f4cbf05Sopenharmony_ci    }
1923f4cbf05Sopenharmony_ci
1933f4cbf05Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
1943f4cbf05Sopenharmony_ci
1953f4cbf05Sopenharmony_ci    if (!HasHandler(target)) {
1963f4cbf05Sopenharmony_ci        UTILS_LOGD("%{public}s: Handler not found.", __FUNCTION__);
1973f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_NOT_FOUND;
1983f4cbf05Sopenharmony_ci    }
1993f4cbf05Sopenharmony_ci
2003f4cbf05Sopenharmony_ci    return EVENT_SYS_ERR_OK;
2013f4cbf05Sopenharmony_ci}
2023f4cbf05Sopenharmony_ci
2033f4cbf05Sopenharmony_cibool IOEventReactor::UpdateToDemultiplexer(int fd)
2043f4cbf05Sopenharmony_ci{
2053f4cbf05Sopenharmony_ci    uint32_t emask = 0u;
2063f4cbf05Sopenharmony_ci    for (IOEventHandler* cur = ioHandlers_[fd].head.get(); cur != nullptr; cur = cur->next_) {
2073f4cbf05Sopenharmony_ci        emask |= cur->events_;
2083f4cbf05Sopenharmony_ci    }
2093f4cbf05Sopenharmony_ci
2103f4cbf05Sopenharmony_ci    if (emask == ioHandlers_[fd].events) {
2113f4cbf05Sopenharmony_ci        UTILS_LOGW("%{public}s: Warning, Interested events not changed.", __FUNCTION__);
2123f4cbf05Sopenharmony_ci        return true;
2133f4cbf05Sopenharmony_ci    }
2143f4cbf05Sopenharmony_ci
2153f4cbf05Sopenharmony_ci    ErrCode res = backend_->ModifyEvents(fd, emask);
2163f4cbf05Sopenharmony_ci    if (res != EVENT_SYS_ERR_OK) {
2173f4cbf05Sopenharmony_ci        UTILS_LOGE("%{public}s: Modify events on backend failed. fd: %{public}d, \
2183f4cbf05Sopenharmony_ci                   new event: %{public}d, error code: %{public}d", __FUNCTION__, fd, emask, res);
2193f4cbf05Sopenharmony_ci        return false;
2203f4cbf05Sopenharmony_ci    }
2213f4cbf05Sopenharmony_ci
2223f4cbf05Sopenharmony_ci    ioHandlers_[fd].events = emask;
2233f4cbf05Sopenharmony_ci    return true;
2243f4cbf05Sopenharmony_ci}
2253f4cbf05Sopenharmony_ci
2263f4cbf05Sopenharmony_civoid IOEventReactor::Execute(const std::vector<EventCallback>& tasks)
2273f4cbf05Sopenharmony_ci{
2283f4cbf05Sopenharmony_ci    for (const EventCallback& cb : tasks) {
2293f4cbf05Sopenharmony_ci        cb();
2303f4cbf05Sopenharmony_ci    }
2313f4cbf05Sopenharmony_ci}
2323f4cbf05Sopenharmony_ci
2333f4cbf05Sopenharmony_ciErrCode IOEventReactor::HandleEvents(int fd, EventId event)
2343f4cbf05Sopenharmony_ci{
2353f4cbf05Sopenharmony_ci    std::vector<EventCallback> taskQue;
2363f4cbf05Sopenharmony_ci    {
2373f4cbf05Sopenharmony_ci        std::lock_guard<std::mutex> lock(mutex_);
2383f4cbf05Sopenharmony_ci        if (!(ioHandlers_[fd].events & event)) {
2393f4cbf05Sopenharmony_ci            UTILS_LOGD("%{public}s: Non-interested event: %{public}d with fd: %{public}d, interested events: \
2403f4cbf05Sopenharmony_ci                       %{public}d", __FUNCTION__, event, fd, ioHandlers_[fd].events);
2413f4cbf05Sopenharmony_ci            return EVENT_SYS_ERR_BADEVENT;
2423f4cbf05Sopenharmony_ci        }
2433f4cbf05Sopenharmony_ci
2443f4cbf05Sopenharmony_ci        for (IOEventHandler* cur = ioHandlers_[fd].head.get()->next_; cur != nullptr; cur = cur->next_) {
2453f4cbf05Sopenharmony_ci            if (cur->events_ != Events::EVENT_NONE && cur->enabled_ && (cur->events_ & event) && cur->cb_) {
2463f4cbf05Sopenharmony_ci                taskQue.push_back(cur->cb_);
2473f4cbf05Sopenharmony_ci                UTILS_LOGD("%{public}s: Handling event success: %{public}d with fd: %{public}d; \
2483f4cbf05Sopenharmony_ci                           handler interested events: %{public}d, active-status: %{public}d", \
2493f4cbf05Sopenharmony_ci                           __FUNCTION__, event, fd, cur->events_, cur->enabled_);
2503f4cbf05Sopenharmony_ci            } else {
2513f4cbf05Sopenharmony_ci                UTILS_LOGD("%{public}s: Handling event ignore: %{public}d with fd: %{public}d; \
2523f4cbf05Sopenharmony_ci                           handler interested events: %{public}d, active-status: %{public}d", \
2533f4cbf05Sopenharmony_ci                           __FUNCTION__, event, fd, cur->events_, cur->enabled_);
2543f4cbf05Sopenharmony_ci            }
2553f4cbf05Sopenharmony_ci        }
2563f4cbf05Sopenharmony_ci    }
2573f4cbf05Sopenharmony_ci
2583f4cbf05Sopenharmony_ci    Execute(taskQue);
2593f4cbf05Sopenharmony_ci    return EVENT_SYS_ERR_OK;
2603f4cbf05Sopenharmony_ci}
2613f4cbf05Sopenharmony_ci
2623f4cbf05Sopenharmony_civoid IOEventReactor::HandleAll(const std::vector<std::pair<int, EventId>>& events)
2633f4cbf05Sopenharmony_ci{
2643f4cbf05Sopenharmony_ci    for (size_t idx = 0u; idx < events.size(); idx++) {
2653f4cbf05Sopenharmony_ci        int fd = events[idx].first;
2663f4cbf05Sopenharmony_ci        EventId event = events[idx].second;
2673f4cbf05Sopenharmony_ci
2683f4cbf05Sopenharmony_ci        UTILS_LOGD("%{public}s: Processing. Handling event: %{public}d, with fd: %{public}d.", \
2693f4cbf05Sopenharmony_ci                   __FUNCTION__, event, fd);
2703f4cbf05Sopenharmony_ci
2713f4cbf05Sopenharmony_ci        if (HandleEvents(fd, event) == EVENT_SYS_ERR_BADEVENT) {
2723f4cbf05Sopenharmony_ci            UTILS_LOGD("%{public}s: Received non-interested events-%{public}d.", __FUNCTION__, event);
2733f4cbf05Sopenharmony_ci        }
2743f4cbf05Sopenharmony_ci    }
2753f4cbf05Sopenharmony_ci}
2763f4cbf05Sopenharmony_ci
2773f4cbf05Sopenharmony_civoid IOEventReactor::Run(int timeout)
2783f4cbf05Sopenharmony_ci{
2793f4cbf05Sopenharmony_ci    std::vector<std::pair<int, EventId>> gotEvents;
2803f4cbf05Sopenharmony_ci    while (loopReady_) {
2813f4cbf05Sopenharmony_ci        if (!enabled_) {
2823f4cbf05Sopenharmony_ci            continue;
2833f4cbf05Sopenharmony_ci        }
2843f4cbf05Sopenharmony_ci        ErrCode res;
2853f4cbf05Sopenharmony_ci        if (timeout == -1) {
2863f4cbf05Sopenharmony_ci            std::lock_guard<std::mutex> lock(mutex_);
2873f4cbf05Sopenharmony_ci            if (count_ ==0) {
2883f4cbf05Sopenharmony_ci                continue;
2893f4cbf05Sopenharmony_ci            }
2903f4cbf05Sopenharmony_ci            res = backend_->Polling(timeout, gotEvents);
2913f4cbf05Sopenharmony_ci        } else {
2923f4cbf05Sopenharmony_ci            res = backend_->Polling(timeout, gotEvents);
2933f4cbf05Sopenharmony_ci        }
2943f4cbf05Sopenharmony_ci
2953f4cbf05Sopenharmony_ci        switch (res) {
2963f4cbf05Sopenharmony_ci            case EVENT_SYS_ERR_OK:
2973f4cbf05Sopenharmony_ci                HandleAll(gotEvents);
2983f4cbf05Sopenharmony_ci                gotEvents.clear();
2993f4cbf05Sopenharmony_ci                break;
3003f4cbf05Sopenharmony_ci            case EVENT_SYS_ERR_NOEVENT:
3013f4cbf05Sopenharmony_ci                UTILS_LOGD("%{public}s: No events captured.", __FUNCTION__);
3023f4cbf05Sopenharmony_ci                break;
3033f4cbf05Sopenharmony_ci            case EVENT_SYS_ERR_FAILED:
3043f4cbf05Sopenharmony_ci                UTILS_LOGE("%{public}s: Backends failed.", __FUNCTION__);
3053f4cbf05Sopenharmony_ci                break;
3063f4cbf05Sopenharmony_ci            default:
3073f4cbf05Sopenharmony_ci                break;
3083f4cbf05Sopenharmony_ci        }
3093f4cbf05Sopenharmony_ci    }
3103f4cbf05Sopenharmony_ci}
3113f4cbf05Sopenharmony_ci
3123f4cbf05Sopenharmony_cibool IOEventReactor::DoClean(int fd)
3133f4cbf05Sopenharmony_ci{
3143f4cbf05Sopenharmony_ci    if (ioHandlers_[fd].head->next_ == nullptr) {
3153f4cbf05Sopenharmony_ci        return true;
3163f4cbf05Sopenharmony_ci    }
3173f4cbf05Sopenharmony_ci
3183f4cbf05Sopenharmony_ci    for (IOEventHandler* cur = ioHandlers_[fd].head->next_; cur != nullptr; cur = cur->next_) {
3193f4cbf05Sopenharmony_ci        cur->prev_->next_ = nullptr;
3203f4cbf05Sopenharmony_ci        cur->prev_ = nullptr;
3213f4cbf05Sopenharmony_ci        cur->enabled_ = false;
3223f4cbf05Sopenharmony_ci    }
3233f4cbf05Sopenharmony_ci
3243f4cbf05Sopenharmony_ci    if (!UpdateToDemultiplexer(fd)) {
3253f4cbf05Sopenharmony_ci        UTILS_LOGD("%{public}s: Clear handler list success, while updating backend failed.", __FUNCTION__);
3263f4cbf05Sopenharmony_ci        return false;
3273f4cbf05Sopenharmony_ci    }
3283f4cbf05Sopenharmony_ci
3293f4cbf05Sopenharmony_ci    return true;
3303f4cbf05Sopenharmony_ci}
3313f4cbf05Sopenharmony_ci
3323f4cbf05Sopenharmony_ciErrCode IOEventReactor::CleanUp()
3333f4cbf05Sopenharmony_ci{
3343f4cbf05Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
3353f4cbf05Sopenharmony_ci    ErrCode res = EVENT_SYS_ERR_OK;
3363f4cbf05Sopenharmony_ci    for (size_t fd = 0u; fd < ioHandlers_.size() && fd <= INT_MAX; fd++) {
3373f4cbf05Sopenharmony_ci        if (!DoClean(fd)) {
3383f4cbf05Sopenharmony_ci            UTILS_LOGD("%{public}s Failed.", __FUNCTION__);
3393f4cbf05Sopenharmony_ci            res = EVENT_SYS_ERR_FAILED;
3403f4cbf05Sopenharmony_ci        }
3413f4cbf05Sopenharmony_ci    }
3423f4cbf05Sopenharmony_ci
3433f4cbf05Sopenharmony_ci    return res;
3443f4cbf05Sopenharmony_ci}
3453f4cbf05Sopenharmony_ci
3463f4cbf05Sopenharmony_ciErrCode IOEventReactor::Clean(int fd)
3473f4cbf05Sopenharmony_ci{
3483f4cbf05Sopenharmony_ci    if (fd == -1) {
3493f4cbf05Sopenharmony_ci        UTILS_LOGD("%{public}s: Failed, bad fd.", __FUNCTION__);
3503f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_BADF;
3513f4cbf05Sopenharmony_ci    }
3523f4cbf05Sopenharmony_ci
3533f4cbf05Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
3543f4cbf05Sopenharmony_ci    if (!DoClean(fd)) {
3553f4cbf05Sopenharmony_ci        UTILS_LOGD("%{public}s: Failed.", __FUNCTION__);
3563f4cbf05Sopenharmony_ci        return EVENT_SYS_ERR_FAILED;
3573f4cbf05Sopenharmony_ci    }
3583f4cbf05Sopenharmony_ci
3593f4cbf05Sopenharmony_ci    return EVENT_SYS_ERR_OK;
3603f4cbf05Sopenharmony_ci}
3613f4cbf05Sopenharmony_ci
3623f4cbf05Sopenharmony_ci} // namespace Utils
3633f4cbf05Sopenharmony_ci} // namespace OHOS
364