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/timer.h"
17 #include "sync/timer_manager.h"
18 #include "internal_inc/osal.h"
19 #include "dfx/log/ffrt_log_api.h"
20 #include "util/ffrt_facade.h"
21
QosConvert(ffrt_qos_t qos,ffrt::QoS & mappedQos)22 static bool QosConvert(ffrt_qos_t qos, ffrt::QoS& mappedQos)
23 {
24 if (ffrt::GetFuncQosMap() == nullptr) {
25 FFRT_LOGE("FuncQosMap has not regist");
26 return false;
27 }
28 mappedQos = ffrt::GetFuncQosMap()(qos);
29 if (mappedQos == ffrt::qos_inherit) {
30 mappedQos = ffrt::ExecuteCtx::Cur()->qos();
31 }
32 return true;
33 }
34
35 API_ATTRIBUTE((visibility("default")))
ffrt_timer_start(ffrt_qos_t qos,uint64_t timeout,void * data,ffrt_timer_cb cb,bool repeat)36 ffrt_timer_t ffrt_timer_start(ffrt_qos_t qos, uint64_t timeout, void* data, ffrt_timer_cb cb, bool repeat)
37 {
38 ffrt::QoS convertQos;
39 if (!QosConvert(qos, convertQos)) {
40 return -1;
41 }
42
43 if (cb == nullptr) {
44 FFRT_LOGE("[Timer] cb cannot be null");
45 return -1;
46 }
47 return ffrt::FFRTFacade::GetTMInstance().RegisterTimer(convertQos, timeout, data, cb, repeat);
48 }
49
50 API_ATTRIBUTE((visibility("default")))
ffrt_timer_stop(ffrt_qos_t qos,ffrt_timer_t handle)51 int ffrt_timer_stop(ffrt_qos_t qos, ffrt_timer_t handle)
52 {
53 ffrt::QoS convertQos;
54 if (!QosConvert(qos, convertQos)) {
55 return -1;
56 }
57
58 return ffrt::FFRTFacade::GetTMInstance().UnregisterTimer(handle);
59 }
60
61 API_ATTRIBUTE((visibility("default")))
ffrt_timer_query(ffrt_qos_t qos,ffrt_timer_t handle)62 ffrt_timer_query_t ffrt_timer_query(ffrt_qos_t qos, ffrt_timer_t handle)
63 {
64 ffrt::QoS convertQos;
65 if (!QosConvert(qos, convertQos)) {
66 return ffrt_timer_notfound;
67 }
68
69 return ffrt::FFRTFacade::GetTMInstance().GetTimerStatus(handle);
70 }
71