• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 #include "event_queue.h"
24 #include "file_descriptor_listener.h"
25 
26 #include "nocopyable.h"
27 
28 #define LOCAL_API __attribute__((visibility ("hidden")))
29 namespace OHOS {
30 namespace AppExecFwk {
31 
32 class FileDescriptorInfo {
33 public:
34     DISALLOW_COPY_AND_MOVE(FileDescriptorInfo);
FileDescriptorInfo()35     FileDescriptorInfo() {}
FileDescriptorInfo(std::string taskName,EventQueue::Priority priority,std::shared_ptr<FileDescriptorListener> listener)36     FileDescriptorInfo(std::string taskName, EventQueue::Priority priority,
37         std::shared_ptr<FileDescriptorListener>listener): taskName_(taskName), priority_(priority),
38         listener_(listener) {}
39     std::string taskName_;
40     EventQueue::Priority priority_;
41     std::shared_ptr<FileDescriptorListener> listener_;
42 };
43 
44 // Interface of IO waiter
45 class IoWaiter {
46 public:
47     using FileDescriptorEventCallback = std::function<void(int32_t, uint32_t, const std::string&,
48         EventQueue::Priority priority)>;
49 
50     IoWaiter() = default;
51     virtual ~IoWaiter() = default;
52     DISALLOW_COPY_AND_MOVE(IoWaiter);
53 
54     /**
55      * Wait until IO event coming or timed out.
56      *
57      * @param lock An unique lock which must be locked by the current thread.
58      * @param nanoseconds Nanoseconds for time out, negative value indicate waiting forever.
59      * @param vsyncOnly wait for vsync fd event only.
60      * @return True if succeeded.
61      */
62     LOCAL_API virtual bool WaitFor(std::unique_lock<std::mutex> &lock, int64_t nanoseconds, bool vsyncOnly = false) = 0;
63 
64     /**
65      * Unblocks one of the waiting threads.
66      */
67     LOCAL_API virtual void NotifyOne() = 0;
68 
69     /**
70      * Unblocks all of the waiting threads.
71      */
72     LOCAL_API virtual void NotifyAll() = 0;
73 
74     /**
75      * Check whether this waiter support listening file descriptor.
76      *
77      * @return True if supported.
78      */
79     LOCAL_API virtual bool SupportListeningFileDescriptor() const = 0;
80 
81     /**
82      * Add file descriptor.
83      *
84      * @param fileDescriptor File descriptor which need to listen.
85      * @param events Events from file descriptor, such as input, output.
86      * @return True if succeeded.
87      */
88     LOCAL_API virtual bool AddFileDescriptor(int32_t fileDescriptor, uint32_t events, const std::string &taskName,
89         const std::shared_ptr<FileDescriptorListener>& listener, EventQueue::Priority priority) = 0;
90 
91     /**
92      * Remove file descriptor.
93      *
94      * @param fileDescriptor File descriptor which need to remove.
95      */
96     LOCAL_API virtual void RemoveFileDescriptor(int32_t fileDescriptor) = 0;
97 
98     /**
99      * Set callback to handle events from file descriptors, such as readable, writable and so on.
100      *
101      * @param callback Callback function to handle events from file descriptors.
102      */
103     LOCAL_API virtual void SetFileDescriptorEventCallback(const FileDescriptorEventCallback &callback) = 0;
104 
GetFileDescriptorMap(int32_t fileDescriptor)105     LOCAL_API virtual std::shared_ptr<FileDescriptorInfo> GetFileDescriptorMap(int32_t fileDescriptor)
106         { return nullptr; }
107 };
108 }  // namespace AppExecFwk
109 }  // namespace OHOS
110 
111 #endif  // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_IO_WAITER_H
112