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 #ifndef JS_CONCURRENT_MODULE_TASKPOOL_TASK_GROUP_H
17 #define JS_CONCURRENT_MODULE_TASKPOOL_TASK_GROUP_H
18 
19 #include <list>
20 
21 #include "task.h"
22 #include "task_manager.h"
23 
24 namespace Commonlibrary::Concurrent::TaskPoolModule {
25 struct GroupInfo {
26     uint32_t finishedTask {};
27     napi_ref resArr = nullptr;
28     Priority priority {Priority::DEFAULT};
29     napi_deferred deferred = nullptr;
30 };
31 
32 class TaskGroup {
33 public:
34     TaskGroup() = default;
35     ~TaskGroup() = default;
36 
37     static napi_value TaskGroupConstructor(napi_env env, napi_callback_info cbinfo);
38     static napi_value AddTask(napi_env env, napi_callback_info cbinfo);
39 
40     uint32_t GetTaskIndex(uint32_t taskId);
41     void NotifyGroupTask(napi_env env);
42     void CancelPendingGroup(napi_env env);
43 
44 private:
45     TaskGroup(const TaskGroup &) = delete;
46     TaskGroup& operator=(const TaskGroup &) = delete;
47     TaskGroup(TaskGroup &&) = delete;
48     TaskGroup& operator=(TaskGroup &&) = delete;
49 
50     static void TaskGroupDestructor(napi_env env, void* data, void* hint);
51 
52     friend class NativeEngineTest;
53 public:
54     uint64_t groupId_ {};
55     GroupInfo* currentGroupInfo_ {};
56     std::list<GroupInfo*> pendingGroupInfos_ {};
57     std::list<napi_ref> taskRefs_ {};
58     std::list<uint64_t> taskIds_ {};
59     uint32_t taskNum_ {};
60     std::atomic<ExecuteState> groupState_ {ExecuteState::NOT_FOUND};
61     napi_ref groupRef_ {};
62     RECURSIVE_MUTEX taskGroupMutex_ {};
63 };
64 } // namespace Commonlibrary::Concurrent::TaskPoolModule
65 #endif // JS_CONCURRENT_MODULE_TASKPOOL_TASK_GROUP_H