• 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      * @return True if succeeded.
60      */
61     LOCAL_API virtual bool WaitFor(std::unique_lock<std::mutex> &lock, int64_t nanoseconds) = 0;
62 
63     /**
64      * Unblocks one of the waiting threads.
65      */
66     LOCAL_API virtual void NotifyOne() = 0;
67 
68     /**
69      * Unblocks all of the waiting threads.
70      */
71     LOCAL_API virtual void NotifyAll() = 0;
72 
73     /**
74      * Check whether this waiter support listening file descriptor.
75      *
76      * @return True if supported.
77      */
78     LOCAL_API virtual bool SupportListeningFileDescriptor() const = 0;
79 
80     /**
81      * Add file descriptor.
82      *
83      * @param fileDescriptor File descriptor which need to listen.
84      * @param events Events from file descriptor, such as input, output.
85      * @return True if succeeded.
86      */
87     LOCAL_API virtual bool AddFileDescriptor(int32_t fileDescriptor, uint32_t events, const std::string &taskName,
88         const std::shared_ptr<FileDescriptorListener>& listener, EventQueue::Priority priority) = 0;
89 
90     /**
91      * Remove file descriptor.
92      *
93      * @param fileDescriptor File descriptor which need to remove.
94      */
95     LOCAL_API virtual void RemoveFileDescriptor(int32_t fileDescriptor) = 0;
96 
97     /**
98      * Set callback to handle events from file descriptors, such as readable, writable and so on.
99      *
100      * @param callback Callback function to handle events from file descriptors.
101      */
102     LOCAL_API virtual void SetFileDescriptorEventCallback(const FileDescriptorEventCallback &callback) = 0;
103 
GetFileDescriptorMap(int32_t fileDescriptor)104     LOCAL_API virtual std::shared_ptr<FileDescriptorInfo> GetFileDescriptorMap(int32_t fileDescriptor)
105         { return nullptr; }
106 };
107 }  // namespace AppExecFwk
108 }  // namespace OHOS
109 
110 #endif  // #ifndef BASE_EVENTHANDLER_FRAMEWORKS_EVENTHANDLER_INCLUDE_IO_WAITER_H
111