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_IO_WAITER_H 17 #define BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_IO_WAITER_H 18 19 #include <cerrno> 20 #include <cstdint> 21 #include <functional> 22 #include <mutex> 23 24 #include "nocopyable.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 // Interface of IO waiter 29 class IoWaiter { 30 public: 31 using FileDescriptorEventCallback = std::function<void(int32_t, uint32_t)>; 32 33 IoWaiter() = default; 34 virtual ~IoWaiter() = default; 35 DISALLOW_COPY_AND_MOVE(IoWaiter); 36 37 /** 38 * Wait until IO event coming or timed out. 39 * 40 * @param lock An unique lock which must be locked by the current thread. 41 * @param nanoseconds Nanoseconds for time out, negative value indicate waiting forever. 42 * @return True if succeeded. 43 */ 44 virtual bool WaitFor(std::unique_lock<std::mutex> &lock, int64_t nanoseconds) = 0; 45 46 /** 47 * Unblocks one of the waiting threads. 48 */ 49 virtual void NotifyOne() = 0; 50 51 /** 52 * Unblocks all of the waiting threads. 53 */ 54 virtual void NotifyAll() = 0; 55 56 /** 57 * Check whether this waiter support listening file descriptor. 58 * 59 * @return True if supported. 60 */ 61 virtual bool SupportListeningFileDescriptor() const = 0; 62 63 /** 64 * Add file descriptor. 65 * 66 * @param fileDescriptor File descriptor which need to listen. 67 * @param events Events from file descriptor, such as input, output. 68 * @return True if succeeded. 69 */ 70 virtual bool AddFileDescriptor(int32_t fileDescriptor, uint32_t events) = 0; 71 72 /** 73 * Remove file descriptor. 74 * 75 * @param fileDescriptor File descriptor which need to remove. 76 */ 77 virtual void RemoveFileDescriptor(int32_t fileDescriptor) = 0; 78 79 /** 80 * Set callback to handle events from file descriptors, such as readable, writable and so on. 81 * 82 * @param callback Callback function to handle events from file descriptors. 83 */ 84 virtual void SetFileDescriptorEventCallback(const FileDescriptorEventCallback &callback) = 0; 85 }; 86 } // namespace AppExecFwk 87 } // namespace OHOS 88 89 #endif // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_IO_WAITER_H 90