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_INTERFACES_INNER_API_FFRT_DESCRIPTOR_LISTENER_H
17 #define BASE_EVENTHANDLER_INTERFACES_INNER_API_FFRT_DESCRIPTOR_LISTENER_H
18 
19 #include "file_descriptor_listener.h"
20 
21 #include <functional>
22 #include <sys/epoll.h>
23 #include <sys/eventfd.h>
24 
25 #define LOCAL_API __attribute__((visibility ("hidden")))
26 namespace OHOS {
27 namespace AppExecFwk {
28 class FfrtDescriptorListener : public FileDescriptorListener {
29 public:
FfrtDescriptorListener()30     FfrtDescriptorListener() {}
FfrtDescriptorListener(uint32_t events,void * data,std::function<void (void *,uint32_t)> cbVal)31     FfrtDescriptorListener(uint32_t events, void *data, std::function<void(void *, uint32_t)> cbVal)
32         : events_(events), data_(data), cb_(cbVal) {}
33 
~FfrtDescriptorListener()34     ~FfrtDescriptorListener() {}
35 
36     LOCAL_API void OnReadable(int32_t fileDescriptor) override;
37 
38     LOCAL_API void OnWritable(int32_t fileDescriptor) override;
39 
40     LOCAL_API void OnShutdown(int32_t fileDescriptor) override;
41 
42     LOCAL_API void OnException(int32_t fileDescriptor) override;
43 
44     static uint32_t ConvertEvents(uint32_t events);
45 private:
InvokePollCb(uint32_t events)46     void InvokePollCb(uint32_t events)
47     {
48         if (cb_ != nullptr) {
49             cb_(data_, events);
50         }
51     }
52 private:
53     uint32_t events_ = 0;
54     void* data_ = nullptr;
55     std::function<void(void*, uint32_t)> cb_ = nullptr;
56 };
57 }
58 }
59 #endif  // #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_QUEUE_FFRT_H
60