• 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_WORKER_H_
17 #define JS_CONCURRENT_MODULE_TASKPOOL_WORKER_H_
18 
19 #include <list>
20 #include <memory>
21 
22 #include "helper/error_helper.h"
23 #include "helper/napi_helper.h"
24 #include "helper/object_helper.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "native_engine/native_engine.h"
28 #include "task.h"
29 #include "task_runner.h"
30 #include "utils/log.h"
31 
32 namespace Commonlibrary::Concurrent::TaskPoolModule {
33 using namespace Commonlibrary::Concurrent::Common::Helper;
34 
35 class Worker {
36 public:
37     ~Worker();
38 
39     static Worker* WorkerConstructor(napi_env env);
40 
41     void NotifyExecuteTask();
42 
43     void NotifyIdle();
44 
45 private:
46     explicit Worker(napi_env env);
47 
48 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
49     static void HandleDebuggerTask(const uv_async_t* req);
50     void DebuggerOnPostTask(std::function<void()>&& task);
51 #endif
52 
GetWorkerLoop()53     uv_loop_t* GetWorkerLoop() const
54     {
55         if (workerEnv_ != nullptr) {
56             return NapiHelper::GetLibUV(workerEnv_);
57         }
58         return nullptr;
59     }
60 
RunLoop()61     void RunLoop() const
62     {
63         uv_loop_t* loop = GetWorkerLoop();
64         if (loop != nullptr) {
65             uv_run(loop, UV_RUN_DEFAULT);
66         } else {
67             HILOG_ERROR("taskpool:: Worker loop is nullptr when start worker loop");
68             return;
69         }
70     }
71 
72     void StartExecuteInThread();
73     static void ExecuteInThread(const void* data);
74     bool PrepareForWorkerInstance();
75     void ReleaseWorkerThreadContent();
76     static void PerformTask(const uv_async_t* req);
77     static void TaskResultCallback(NativeEngine* engine, NativeValue* value, NativeValue* data);
78     static void NotifyTaskResult(napi_env env, TaskInfo* taskInfo, napi_value result);
79 
80     napi_env hostEnv_ {nullptr};
81     napi_env workerEnv_ {nullptr};
82     uv_async_t *performTaskSignal_ {nullptr};
83 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
84     uv_async_t *debuggerOnPostTaskSignal_ {nullptr};
85     std::function<void()> debuggerTask_;
86 #endif
87     std::unique_ptr<TaskRunner> runner_ {};
88 };
89 } // namespace Commonlibrary::Concurrent::TaskPoolModule
90 #endif // JS_CONCURRENT_MODULE_TASKPOOL_WORKER_H_
91