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_SYSEVENT_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")))
ffrt_epoll_ctl(ffrt_qos_t qos,int op,int fd,uint32_t events,void * data,ffrt_poller_cb cb)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().DelFdEvent(fd);
43 } else if (op == EPOLL_CTL_ADD || op == EPOLL_CTL_MOD) {
44 return ffrt::FFRTFacade::GetPPInstance().AddFdEvent(op, events, fd, data, cb);
45 } else {
46 FFRT_SYSEVENT_LOGE("ffrt_epoll_ctl input error: op=%d, fd=%d", op, fd);
47 return -1;
48 }
49 }
50
51 API_ATTRIBUTE((visibility("default")))
ffrt_epoll_wait(ffrt_qos_t qos,struct epoll_event * events,int max_events,int timeout)52 int ffrt_epoll_wait(ffrt_qos_t qos, struct epoll_event* events, int max_events, int timeout)
53 {
54 ffrt::QoS ffrtQos;
55 if (!QosConvert(qos, ffrtQos)) {
56 return -1;
57 }
58 return ffrt::FFRTFacade::GetPPInstance().WaitFdEvent(events, max_events, timeout);
59 }
60
61 API_ATTRIBUTE((visibility("default")))
ffrt_poller_wakeup(ffrt_qos_t qos)62 void ffrt_poller_wakeup(ffrt_qos_t qos)
63 {
64 ffrt::QoS pollerQos;
65 if (!QosConvert(qos, pollerQos)) {
66 return;
67 }
68
69 ffrt::FFRTFacade::GetPPInstance().WakeUp();
70 }
71
72 API_ATTRIBUTE((visibility("default")))
ffrt_epoll_get_count(ffrt_qos_t qos)73 uint8_t ffrt_epoll_get_count(ffrt_qos_t qos)
74 {
75 ffrt::QoS pollerQos;
76 if (!QosConvert(qos, pollerQos)) {
77 return 0;
78 }
79
80 return ffrt::FFRTFacade::GetPPInstance().GetPollCount() % UINT8_MAX;
81 }
82
83 API_ATTRIBUTE((visibility("default")))
ffrt_epoll_get_wait_time(void * taskHandle)84 uint64_t ffrt_epoll_get_wait_time(void* taskHandle)
85 {
86 if (taskHandle == nullptr) {
87 FFRT_LOGE("invalid task handle");
88 return 0;
89 }
90
91 auto task = reinterpret_cast<ffrt::CoTask*>(taskHandle);
92 return ffrt::FFRTFacade::GetPPInstance().GetTaskWaitTime(task);
93 }