• 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 "task_group.h"
17 
18 #include "helper/error_helper.h"
19 #include "helper/napi_helper.h"
20 #include "helper/object_helper.h"
21 #include "napi/native_api.h"
22 #include "utils/log.h"
23 
24 namespace Commonlibrary::Concurrent::TaskPoolModule {
25 using namespace Commonlibrary::Concurrent::Common::Helper;
26 
TaskGroupConstructor(napi_env env,napi_callback_info cbinfo)27 napi_value TaskGroup::TaskGroupConstructor(napi_env env, napi_callback_info cbinfo)
28 {
29     napi_value thisVar;
30     napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisVar, nullptr);
31 
32     napi_value groupId;
33     uint32_t id = TaskGroupManager::GetInstance().GenerateGroupId();
34     napi_create_uint32(env, id, &groupId);
35     napi_property_descriptor properties[] = {
36         DECLARE_NAPI_PROPERTY(GROUP_ID_STR, groupId),
37         DECLARE_NAPI_FUNCTION_WITH_DATA("addTask", AddTask, thisVar),
38     };
39     napi_define_properties(env, thisVar, sizeof(properties) / sizeof(properties[0]), properties);
40     uint32_t* data = new uint32_t();
41     *data = id;
42     napi_wrap(env, thisVar, data, Destructor, nullptr, nullptr);
43 
44     return thisVar;
45 }
46 
Destructor(napi_env env,void * data,void * hint)47 void TaskGroup::Destructor(napi_env env, void* data, [[maybe_unused]] void* hint)
48 {
49     uint32_t* groupId = reinterpret_cast<uint32_t*>(data);
50     TaskGroupManager::GetInstance().ClearTasks(env, *groupId);
51     delete groupId;
52 }
53 
AddTask(napi_env env,napi_callback_info cbinfo)54 napi_value TaskGroup::AddTask(napi_env env, napi_callback_info cbinfo)
55 {
56     // Check the argc
57     size_t argc = NapiHelper::GetCallbackInfoArgc(env, cbinfo);
58     if (argc < 1) {
59         ErrorHelper::ThrowError(env, ErrorHelper::TYPE_ERROR, "taskGroup:: the number of params must be at least one");
60         return nullptr;
61     }
62 
63     // Check the first param is task or func
64     napi_value* args = new napi_value[argc];
65     ObjectScope<napi_value> scope(args, true);
66 
67     // Get groupId from this
68     napi_value thisVar;
69     napi_get_cb_info(env, cbinfo, &argc, args, &thisVar, nullptr);
70     napi_value groupIdVal = NapiHelper::GetNameProperty(env, thisVar, GROUP_ID_STR);
71     uint32_t groupId = NapiHelper::GetUint32Value(env, groupIdVal);
72 
73     napi_valuetype type;
74     napi_typeof(env, args[0], &type);
75     if (type == napi_object) {
76         napi_ref taskRef = NapiHelper::CreateReference(env, args[0], 1);
77         TaskGroupManager::GetInstance().AddTask(groupId, taskRef);
78         return nullptr;
79     } else if (type == napi_function) {
80         napi_value task = nullptr;
81         napi_create_object(env, &task);
82         Task::CreateTaskByFunc(env, task, args[0], args, argc);
83         napi_ref taskRef = NapiHelper::CreateReference(env, task, 1);
84         TaskGroupManager::GetInstance().AddTask(groupId, taskRef);
85         return nullptr;
86     }
87     ErrorHelper::ThrowError(env, ErrorHelper::TYPE_ERROR, "taskGroup:: first param must be object or function");
88     return nullptr;
89 }
90 } // namespace Commonlibrary::Concurrent::TaskPoolModule