• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef JS_CONCURRENT_MODULE_TASKPOOL_TASKPOOL_H
17 #define JS_CONCURRENT_MODULE_TASKPOOL_TASKPOOL_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "native_engine/native_engine.h"
22 #include "task.h"
23 #include "task_group.h"
24 
25 namespace Commonlibrary::Concurrent::TaskPoolModule {
26 using namespace Commonlibrary::Concurrent::Common;
27 
28 struct TaskMessage {
29     napi_deferred deferred = nullptr;
30     Priority priority {Priority::DEFAULT};
31     uint32_t taskId {};
32 };
33 
34 class TaskPool {
35 public:
36     static napi_value InitTaskPool(napi_env env, napi_value exports);
37     static void HandleTaskResult(Task* task);
38 
39 private:
40     TaskPool() = delete;
41     ~TaskPool() = delete;
42     TaskPool(const TaskPool &) = delete;
43     TaskPool& operator=(const TaskPool &) = delete;
44     TaskPool(TaskPool &&) = delete;
45     TaskPool& operator=(TaskPool &&) = delete;
46 
47     static napi_value Execute(napi_env env, napi_callback_info cbinfo);
48     static napi_value ExecuteDelayed(napi_env env, napi_callback_info cbinfo);
49     static void DelayTask(uv_timer_t* handle);
50     static napi_value Cancel(napi_env env, napi_callback_info cbinfo);
51     static napi_value GetTaskPoolInfo(napi_env env, [[maybe_unused]] napi_callback_info cbinfo);
52     static napi_value TerminateTask(napi_env env, napi_callback_info cbinfo);
53     static napi_value IsConcurrent(napi_env env, napi_callback_info cbinfo);
54     static napi_value ExecutePeriodically(napi_env env, napi_callback_info cbinfo);
55     static void PeriodicTaskCallback(uv_timer_t* handle);
56 
57     static void HandleTaskResultInner(Task* task);
58     static void UpdateGroupInfoByResult(napi_env env, Task* task, napi_value res, bool success);
59     static void ExecuteTask(napi_env env, Task* task, Priority priority = Priority::DEFAULT);
60     static napi_value ExecuteGroup(napi_env env, napi_value taskGroup, Priority priority);
61 
62     static void TriggerTask(Task* task, bool isCancel);
63     static void TriggerTimer(napi_env env, Task* task, int32_t period);
64     static bool CheckDelayedParams(napi_env env, napi_callback_info cbinfo, uint32_t& priority, int32_t& delayTime,
65                                    Task*& task);
66     static bool CheckPeriodicallyParams(napi_env env, napi_callback_info cbinfo, int32_t& period, uint32_t& priority,
67                                         Task*& task);
68     static void ExecuteOnReceiveDataCallback(CallbackInfo* callbackInfo, TaskResultInfo* resultInfo);
69     static void RecordTaskResultLog(Task* task, napi_status status, napi_value& napiTaskResult, bool& isCancel);
70     friend class TaskManager;
71     friend class NativeEngineTest;
72 };
73 
74 class CallbackScope {
75 public:
CallbackScope(napi_env env,TaskResultInfo * resultInfo,napi_status & status)76     CallbackScope(napi_env env, TaskResultInfo* resultInfo, napi_status& status)
77     {
78         env_ = env;
79         workerEnv_ = resultInfo->workerEnv;
80         taskId_ = resultInfo->taskId;
81         status = napi_open_handle_scope(env_, &scope_);
82     }
83 
~CallbackScope()84     ~CallbackScope()
85     {
86         TaskManager::GetInstance().DecreaseSendDataRefCount(env_, taskId_);
87         if (workerEnv_ != nullptr) {
88             auto workerEngine = reinterpret_cast<NativeEngine*>(workerEnv_);
89             workerEngine->DecreaseListeningCounter();
90         }
91 
92         if (scope_ != nullptr) {
93             napi_close_handle_scope(env_, scope_);
94         }
95     }
96 private:
97     napi_env env_;
98     napi_env workerEnv_;
99     uint32_t taskId_;
100     napi_handle_scope scope_ = nullptr;
101 };
102 } // namespace Commonlibrary::Concurrent::TaskPoolModule
103 #endif // JS_CONCURRENT_MODULE_TASKPOOL_TASKPOOL_H