1 /* 2 * Copyright (c) 2021-2023 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_DEAMON_IO_WAITER_H 17 #define BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_DEAMON_IO_WAITER_H 18 19 #include <atomic> 20 #include <map> 21 #include <mutex> 22 23 #include <sys/epoll.h> 24 #include "io_waiter.h" 25 #include "nocopyable.h" 26 #include "event_queue.h" 27 #include "file_descriptor_listener.h" 28 29 #define LOCAL_API __attribute__((visibility ("hidden"))) 30 namespace OHOS { 31 namespace AppExecFwk { 32 class EventHandler; 33 34 // Use epoll to listen file descriptor. 35 class DeamonIoWaiter { 36 public: 37 DeamonIoWaiter() = default; 38 ~DeamonIoWaiter(); 39 static DeamonIoWaiter& GetInstance(); 40 DISALLOW_COPY_AND_MOVE(DeamonIoWaiter); 41 42 /** 43 * Initialize epoll. 44 * 45 * @return True if succeeded. 46 */ 47 bool Init(); 48 49 void NotifyOne(); 50 void NotifyAll(); 51 void StopEpollIoWaiter(); 52 void StartEpollIoWaiter(); 53 bool SupportListeningFileDescriptor() const; 54 55 LOCAL_API bool AddFileDescriptor(int32_t fileDescriptor, uint32_t events, const std::string &taskName, 56 const std::shared_ptr<FileDescriptorListener>& listener, EventQueue::Priority priority); 57 LOCAL_API void RemoveFileDescriptor(int32_t fileDescriptor); 58 59 LOCAL_API void InsertFileDescriptorMap(int32_t fileDescriptor, const std::string& taskName, 60 EventQueue::Priority priority, const std::shared_ptr<FileDescriptorListener>& listener); 61 LOCAL_API void EraseFileDescriptorMap(int32_t fileDescriptor); 62 LOCAL_API std::shared_ptr<FileDescriptorInfo> GetFileDescriptorMap(int32_t fileDescriptor); 63 LOCAL_API void HandleFileDescriptorEvent(int32_t fileDescriptor, uint32_t events); 64 private: 65 LOCAL_API void EpollWaitFor(); 66 LOCAL_API void DrainAwakenPipe() const; 67 LOCAL_API void HandleEpollEvents(struct epoll_event *epollEvents, int32_t eventsCount); 68 69 // File descriptor for epoll. 70 int32_t epollFd_{-1}; 71 // File descriptor used to wake up epoll. 72 int32_t awakenFd_{-1}; 73 std::mutex fileDescriptorMapLock; 74 std::atomic<int32_t> waitingCount_{0}; 75 std::atomic<bool> running_ = false; 76 std::atomic<bool> isFinished_ = false; 77 std::unique_ptr<std::thread> epollThread_; 78 std::map<int32_t, std::shared_ptr<FileDescriptorInfo>> fileDescriptorMap_; 79 }; 80 } // namespace AppExecFwk 81 } // namespace OHOS 82 83 #endif // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_DEAMON_IO_WAITER_H 84