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 <deque> 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 explicit SequenceRunner(Priority priority, const std::string& name = "", bool isGlobal = false) priority_(priority)30 : priority_(priority), seqName_(name), isGlobalRunner_(isGlobal) {} 31 static napi_value SeqRunnerConstructor(napi_env env, napi_callback_info cbinfo); 32 static napi_value Execute(napi_env env, napi_callback_info cbinfo); 33 bool RemoveWaitingTask(Task* task); 34 void AddTask(Task* task); 35 void IncreaseSeqCount(); 36 uint64_t DecreaseSeqCount(); 37 void TriggerTask(napi_env env); 38 39 private: 40 SequenceRunner(const SequenceRunner &) = delete; 41 SequenceRunner& operator=(const SequenceRunner &) = delete; 42 SequenceRunner(SequenceRunner &&) = delete; 43 SequenceRunner& operator=(SequenceRunner &&) = delete; 44 45 static void ExecuteTaskImmediately(uint32_t taskId, Priority priority); 46 static void SequenceRunnerDestructor(napi_env env, void* data, void* hint); 47 static bool SeqRunnerConstructorInner(napi_env env, napi_value& thisVar, SequenceRunner* seqRunner); 48 49 friend class NativeEngineTest; 50 public: 51 uint64_t seqRunnerId_ {}; 52 std::atomic<uint64_t> currentTaskId_ {}; 53 Priority priority_ {Priority::DEFAULT}; 54 std::deque<Task*> seqRunnerTasks_ {}; 55 std::shared_mutex seqRunnerMutex_; 56 57 // for global SequenceRunner 58 std::string seqName_ {}; 59 bool isGlobalRunner_ {false}; 60 std::atomic<uint64_t> refCount_ {1}; 61 }; 62 } // namespace Commonlibrary::Concurrent::TaskPoolModule 63 #endif // JS_CONCURRENT_MODULE_TASKPOOL_RUNNER_H