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_RUNNER_H 17 #define JS_CONCURRENT_MODULE_TASKPOOL_RUNNER_H 18 19 #include <queue> 20 #include <unordered_map> 21 22 #include "task.h" 23 24 namespace Commonlibrary::Concurrent::TaskPoolModule { 25 class SequenceRunner { 26 public: 27 SequenceRunner() = default; 28 ~SequenceRunner() = default; 29 30 static napi_value SeqRunnerConstructor(napi_env env, napi_callback_info cbinfo); 31 static napi_value Execute(napi_env env, napi_callback_info cbinfo); 32 33 private: 34 SequenceRunner(const SequenceRunner &) = delete; 35 SequenceRunner& operator=(const SequenceRunner &) = delete; 36 SequenceRunner(SequenceRunner &&) = delete; 37 SequenceRunner& operator=(SequenceRunner &&) = delete; 38 39 static void ExecuteTaskImmediately(uint64_t taskId, Priority priority); 40 static void SequenceRunnerDestructor(napi_env env, void* data, void* hint); 41 static bool SeqRunnerConstructorInner(napi_env env, napi_value &thisVar, SequenceRunner *seqRunner); 42 43 friend class NativeEngineTest; 44 public: 45 uint64_t seqRunnerId_ {}; 46 std::atomic<uint64_t> currentTaskId_ {}; 47 Priority priority_ {Priority::DEFAULT}; 48 std::queue<Task*> seqRunnerTasks_ {}; 49 std::shared_mutex seqRunnerMutex_; 50 51 // for global SequenceRunner 52 std::string seqName_ {}; 53 napi_ref seqRunnerRef_ = nullptr; 54 bool isGlobalRunner_ {false}; 55 uint64_t count_ = 0; 56 std::unordered_map<napi_env, napi_ref> globalSeqRunnerRef_ {}; 57 }; 58 } 59 60 #endif // JS_CONCURRENT_MODULE_TASKPOOL_RUNNER_H