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 public: GetFailedIndexGroupInfo27 int32_t GetFailedIndex() 28 { 29 return failedIndex; 30 } 31 SetFailedIndexGroupInfo32 void SetFailedIndex(int32_t index) 33 { 34 failedIndex = index; 35 } 36 HasExceptionGroupInfo37 bool HasException() 38 { 39 return failedIndex != -1; 40 } 41 42 uint32_t finishedTaskNum {}; 43 napi_ref resArr {nullptr}; 44 Priority priority {Priority::DEFAULT}; 45 napi_deferred deferred {nullptr}; 46 47 private: 48 int32_t failedIndex {-1}; // -1: the initial value means no exception 49 }; 50 51 class TaskGroup { 52 public: TaskGroup(napi_env env)53 explicit TaskGroup(napi_env env) : env_(env) {} 54 TaskGroup() = default; 55 ~TaskGroup() = default; 56 57 static napi_value TaskGroupConstructor(napi_env env, napi_callback_info cbinfo); 58 static napi_value AddTask(napi_env env, napi_callback_info cbinfo); 59 static void HostEnvCleanupHook(void* data); 60 static void StartRejectResult(const uv_async_t* req); 61 62 uint32_t GetTaskIndex(uint32_t taskId); 63 void NotifyGroupTask(napi_env env); 64 void CancelPendingGroup(napi_env env); 65 void CancelGroupTask(napi_env env, uint32_t taskId); 66 void RejectResult(napi_env env, napi_value res); 67 void RejectResult(napi_env env); 68 void InitHandle(napi_env env); 69 void TriggerRejectResult(); 70 bool IsSameEnv(napi_env env); 71 72 private: 73 TaskGroup(const TaskGroup &) = delete; 74 TaskGroup& operator=(const TaskGroup &) = delete; 75 TaskGroup(TaskGroup &&) = delete; 76 TaskGroup& operator=(TaskGroup &&) = delete; 77 78 static void TaskGroupDestructor(napi_env env, void* data, void* hint); 79 80 friend class NativeEngineTest; 81 public: 82 napi_env env_ = nullptr; 83 uint64_t groupId_ {}; 84 GroupInfo* currentGroupInfo_ {}; 85 std::list<GroupInfo*> pendingGroupInfos_ {}; 86 std::list<napi_ref> taskRefs_ {}; 87 std::list<uint32_t> taskIds_ {}; 88 uint32_t taskNum_ {}; 89 std::atomic<ExecuteState> groupState_ {ExecuteState::NOT_FOUND}; 90 napi_ref groupRef_ {}; 91 std::recursive_mutex taskGroupMutex_ {}; 92 uv_async_t* onRejectResultSignal_ = nullptr; 93 std::atomic<bool> isValid_ {true}; 94 }; 95 } // namespace Commonlibrary::Concurrent::TaskPoolModule 96 #endif // JS_CONCURRENT_MODULE_TASKPOOL_TASK_GROUP_H