1 /*
2  * Copyright (c) 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 #include "c/executor_task.h"
17 #include "internal_inc/osal.h"
18 #include "dfx/log/ffrt_log_api.h"
19 #include "util/ffrt_facade.h"
20 
QosConvert(ffrt_qos_t qos,ffrt::QoS & mappedQos)21 static bool QosConvert(ffrt_qos_t qos, ffrt::QoS& mappedQos)
22 {
23     if (ffrt::GetFuncQosMap() == nullptr) {
24         FFRT_LOGE("FuncQosMap has not regist");
25         return false;
26     }
27     mappedQos = ffrt::GetFuncQosMap()(qos);
28     if (mappedQos == ffrt::qos_inherit) {
29         mappedQos = ffrt::ExecuteCtx::Cur()->qos();
30     }
31     return true;
32 }
33 
34 API_ATTRIBUTE((visibility("default")))
35 int ffrt_epoll_ctl(ffrt_qos_t qos, int op, int fd, uint32_t events, void* data, ffrt_poller_cb cb)
36 {
37     ffrt::QoS ffrtQos;
38     if (!QosConvert(qos, ffrtQos)) {
39         return -1;
40     }
41     if (op == EPOLL_CTL_DEL) {
42         return ffrt::FFRTFacade::GetPPInstance().GetPoller(ffrtQos).DelFdEvent(fd);
43     } else if (op == EPOLL_CTL_ADD || op == EPOLL_CTL_MOD) {
44         int ret = ffrt::FFRTFacade::GetPPInstance().GetPoller(ffrtQos).AddFdEvent(op, events, fd, data, cb);
45         if (ret == 0) {
46             ffrt::FFRTFacade::GetEUInstance().NotifyLocalTaskAdded(ffrtQos);
47         }
48         return ret;
49     } else {
50         FFRT_LOGE("ffrt_epoll_ctl input error: op=%d, fd=%d", op, fd);
51         return -1;
52     }
53 }
54 
55 API_ATTRIBUTE((visibility("default")))
56 int ffrt_epoll_wait(ffrt_qos_t qos, struct epoll_event* events, int max_events, int timeout)
57 {
58     ffrt::QoS ffrtQos;
59     if (!QosConvert(qos, ffrtQos)) {
60         return -1;
61     }
62     return ffrt::FFRTFacade::GetPPInstance().GetPoller(ffrtQos).WaitFdEvent(events, max_events, timeout);
63 }
64 
65 API_ATTRIBUTE((visibility("default")))
66 void ffrt_poller_wakeup(ffrt_qos_t qos)
67 {
68     ffrt::QoS pollerQos;
69     if (!QosConvert(qos, pollerQos)) {
70         return;
71     }
72 
73     ffrt::FFRTFacade::GetPPInstance().GetPoller(pollerQos).WakeUp();
74 }
75 
76 API_ATTRIBUTE((visibility("default")))
77 uint8_t ffrt_epoll_get_count(ffrt_qos_t qos)
78 {
79     ffrt::QoS pollerQos;
80     if (!QosConvert(qos, pollerQos)) {
81         return 0;
82     }
83 
84     return ffrt::FFRTFacade::GetPPInstance().GetPoller(pollerQos).GetPollCount();
85 }
86 
87 API_ATTRIBUTE((visibility("default")))
88 uint64_t ffrt_epoll_get_wait_time(void* taskHandle)
89 {
90     if (taskHandle == nullptr) {
91         FFRT_LOGE("invalid task handle");
92         return 0;
93     }
94 
95     auto task = reinterpret_cast<ffrt::CPUEUTask*>(taskHandle);
96     return ffrt::FFRTFacade::GetPPInstance().GetPoller(task->qos).GetTaskWaitTime(task);
97 }