• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi/native_api.h"
17 #include <c/queue.h>
18 #include <c/sleep.h>
19 #include <c/task.h>
20 #include <ffrt.h>
21 #define FAIL (-1)
22 #define SUCCESS 0
23 #define PARAM_100 100
24 
OnePlusForTest(void * arg)25 inline void OnePlusForTest(void* arg)
26 {
27     *(uint64_t*)arg = ffrt_this_task_get_id();
28 }
29 
OnePlusForTestQos(void * arg)30 inline void OnePlusForTestQos(void* arg)
31 {
32     *(int*)arg = ffrt_this_task_update_qos(ffrt_qos_background);
33     *(int*)arg += PARAM_100;
34 }
35 template<class T>
36 struct function {
37     ffrt_function_header_t header;
38     T closure;
39 };
40 
41 template<class T>
exec_function_wrapper(void * t)42 void exec_function_wrapper(void* t)
43 {
44     auto f = reinterpret_cast<function<std::decay_t<T>>*>(t);
45     f->closure();
46 }
47 
48 template<class T>
destroy_function_wrapper(void * t)49 void destroy_function_wrapper(void* t)
50 {
51     auto f = reinterpret_cast<function<std::decay_t<T>>*>(t);
52     f->closure = nullptr;
53 }
54 template<class T>
create_function_wrapper(T && func,ffrt_function_kind_t kind=ffrt_function_kind_general)55 inline ffrt_function_header_t* create_function_wrapper(T&& func,
56     ffrt_function_kind_t kind = ffrt_function_kind_general)
57 {
58     using function_type = function<std::decay_t<T>>;
59 
60     auto p = ffrt_alloc_auto_managed_function_storage_base(kind);
61     auto f = new (p)function_type;
62     f->header.exec = exec_function_wrapper<T>;
63     f->header.destroy = destroy_function_wrapper<T>;
64     f->closure = std::forward<T>(func);
65     return reinterpret_cast<ffrt_function_header_t*>(f);
66 }
FfrtThisTaskGetId(napi_env env,napi_callback_info info)67 static napi_value FfrtThisTaskGetId(napi_env env, napi_callback_info info)
68 {
69     uint64_t taskId = SUCCESS;
70     std::function<void()>&& GetPidFunc = [&taskId]() { OnePlusForTest((void *)(&taskId)); };
71     ffrt_submit_base(create_function_wrapper(GetPidFunc), nullptr, nullptr, nullptr);
72     ffrt_wait();
73     napi_value result = nullptr;
74     napi_create_int32(env, taskId != SUCCESS, &result);
75     return result;
76 }
77 
FfrtThisTaskUpdateQos(napi_env env,napi_callback_info info)78 static napi_value FfrtThisTaskUpdateQos(napi_env env, napi_callback_info info)
79 {
80     int pos = FAIL;
81     std::function<void()>&& UpdateQosFunc = [&pos]() { OnePlusForTestQos((void *)(&pos)); };
82     ffrt_submit_base(create_function_wrapper(UpdateQosFunc), nullptr, nullptr, nullptr);
83     ffrt_wait();
84     napi_value result = nullptr;
85     napi_create_int32(env, pos, &result);
86     return result;
87 }
88 
89 EXTERN_C_START
Init(napi_env env,napi_value exports)90 static napi_value Init(napi_env env, napi_value exports)
91 {
92     napi_property_descriptor desc[] = {
93         {"ffrtThisTaskGetId", nullptr, FfrtThisTaskGetId, nullptr, nullptr, nullptr, napi_default, nullptr},
94         {"ffrtThisTaskUpdateQos", nullptr, FfrtThisTaskUpdateQos, nullptr, nullptr, nullptr, napi_default, nullptr}
95     };
96     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
97     return exports;
98 }
99 
100 EXTERN_C_END
101 
102 static napi_module demoModule = {
103     .nm_version = 1,
104     .nm_flags = 0,
105     .nm_filename = nullptr,
106     .nm_register_func = Init,
107     .nm_modname = "ffrtndk1test",
108     .nm_priv = ((void *)0),
109     .reserved = {0},
110 };
111 
RegisterModule(void)112 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
113