• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_ASYNC_RUNNER_H
17 #define JS_CONCURRENT_MODULE_TASKPOOL_ASYNC_RUNNER_H
18 
19 #include <deque>
20 #include <unordered_map>
21 #include "task.h"
22 
23 namespace Commonlibrary::Concurrent::TaskPoolModule {
24 
25 class AsyncRunner {
26 public:
27     AsyncRunner() = default;
28     ~AsyncRunner() = default;
29 
30     static napi_value AsyncRunnerConstructor(napi_env env, napi_callback_info cbinfo);
31     static napi_value Execute(napi_env env, napi_callback_info cbinfo);
32     static AsyncRunner* CreateGlobalRunner(const std::string& name, uint32_t runningCapacity, uint32_t waitingCapacity);
33     bool RemoveWaitingTask(Task* task, bool isReject = true);
34     void TriggerRejectErrorTimer(Task* task, int32_t errCode, bool isWaiting = false);
35     void TriggerWaitingTask();
36     uint64_t DecreaseAsyncCount();
37     void IncreaseAsyncCount();
38     bool CheckGlobalRunnerParams(napi_env env, uint32_t runningCapacity, uint32_t waitingCapacity);
39     void DecreaseRunningCount();
40 
41 private:
42     AsyncRunner(const AsyncRunner &) = delete;
43     AsyncRunner& operator=(const AsyncRunner &) = delete;
44     AsyncRunner(AsyncRunner &&) = delete;
45     AsyncRunner& operator=(AsyncRunner &&) = delete;
46 
47     static bool AsyncRunnerConstructorInner(napi_env env, napi_value& thisVar, AsyncRunner* asyncRunner);
48     static void ExecuteTaskImmediately(AsyncRunner* asyncRunner, Task* task);
49     static void AsyncRunnerDestructor(napi_env env, void* data, void* hint);
50     static AsyncRunner* CheckAndCreateAsyncRunner(napi_env env, napi_value &thisVar, napi_value name,
51                                                   napi_value runningCapacity, napi_value waitingCapacity);
52     static bool CheckExecuteArgs(napi_env env, napi_value napiTask, napi_value napiPriority);
53     static bool AddTasksToAsyncRunner(AsyncRunner* asyncRunner, Task* task);
54 
55     friend class NativeEngineTest;
56 public:
57     uint64_t asyncRunnerId_ {};
58     uint32_t runningCapacity_ {};
59     uint32_t waitingCapacity_ {};
60     std::atomic<uint32_t> runningCount_ {}; // running task count
61 
62     // for global AsyncRunner
63     std::string name_ {};
64     std::atomic<bool> isGlobalRunner_ {false};
65     std::atomic<uint64_t> refCount_ {1};
66     std::deque<Task*> waitingTasks_ {};
67     std::shared_mutex asyncRunnerMutex_;
68     std::shared_mutex waitingTasksMutex_;
69 };
70 } // namespace Commonlibrary::Concurrent::TaskPoolModule
71 #endif // JS_CONCURRENT_MODULE_TASKPOOL_ASYNC_RUNNER_H