1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EPOLL_IO_WAITER_H 17 #define BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EPOLL_IO_WAITER_H 18 19 #include <atomic> 20 #include <mutex> 21 22 #include "io_waiter.h" 23 #include "nocopyable.h" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 // Use epoll to listen file descriptor. 28 class EpollIoWaiter final : public IoWaiter { 29 public: 30 EpollIoWaiter() = default; 31 ~EpollIoWaiter() final; 32 DISALLOW_COPY_AND_MOVE(EpollIoWaiter); 33 34 /** 35 * Initialize epoll. 36 * 37 * @return True if succeeded. 38 */ 39 bool Init(); 40 41 bool WaitFor(std::unique_lock<std::mutex> &lock, int64_t nanoseconds) final; 42 43 void NotifyOne() final; 44 void NotifyAll() final; 45 46 bool SupportListeningFileDescriptor() const final; 47 48 bool AddFileDescriptor(int32_t fileDescriptor, uint32_t events) final; 49 void RemoveFileDescriptor(int32_t fileDescriptor) final; 50 51 void SetFileDescriptorEventCallback(const FileDescriptorEventCallback &callback) final; 52 53 private: 54 void DrainAwakenPipe() const; 55 56 // File descriptor for epoll. 57 int32_t epollFd_{-1}; 58 // File descriptor used to wake up epoll. 59 int32_t awakenFd_{-1}; 60 61 FileDescriptorEventCallback callback_; 62 std::atomic<uint32_t> waitingCount_{0}; 63 }; 64 } // namespace AppExecFwk 65 } // namespace OHOS 66 67 #endif // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_EPOLL_IO_WAITER_H 68