1 /** 2 * Copyright (c) 2024-2025 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 PANDA_RUNTIME_COROUTINES_COROUTINE_WORKER_H 17 #define PANDA_RUNTIME_COROUTINES_COROUTINE_WORKER_H 18 19 #include "include/external_callback_poster.h" 20 #include "runtime/include/runtime.h" 21 #include "runtime/coroutines/local_storage.h" 22 23 namespace ark { 24 25 /** 26 * THE ORDER HAS MEANING 27 * ASCEDING ORDER - HIGHER PRIORITY 28 * DO NOT CHANGE INITIALIZATION VALUES 29 */ 30 enum class CoroutinePriority { 31 LOW_PRIORITY, 32 MEDIUM_PRIORITY, 33 DEFAULT_PRIORITY = MEDIUM_PRIORITY, 34 HIGH_PRIORITY, 35 CRITICAL_PRIORITY, 36 PRIORITY_COUNT 37 }; 38 39 /// Represents a coroutine worker, which can host multiple coroutines and schedule them. 40 class CoroutineWorker { 41 public: 42 enum class DataIdx { INTEROP_CTX_PTR, EXTERNAL_IFACES, LAST_ID }; 43 using LocalStorage = StaticLocalStorage<DataIdx>; 44 45 NO_COPY_SEMANTIC(CoroutineWorker); 46 NO_MOVE_SEMANTIC(CoroutineWorker); 47 CoroutineWorker(Runtime * runtime,PandaVM * vm)48 CoroutineWorker(Runtime *runtime, PandaVM *vm) : runtime_(runtime), vm_(vm) {} ~CoroutineWorker()49 virtual ~CoroutineWorker() 50 { 51 os::memory::LockHolder l(posterLock_); 52 if (callbackPoster_ != nullptr) { 53 callbackPoster_->SetDestroyInPlace(); 54 } 55 } 56 GetRuntime()57 Runtime *GetRuntime() 58 { 59 return runtime_; 60 } 61 GetPandaVM()62 PandaVM *GetPandaVM() const 63 { 64 return vm_; 65 } 66 GetLocalStorage()67 LocalStorage &GetLocalStorage() 68 { 69 return localStorage_; 70 } 71 SetCallbackPoster(PandaUniquePtr<CallbackPoster> poster)72 void SetCallbackPoster(PandaUniquePtr<CallbackPoster> poster) 73 { 74 ASSERT(!callbackPoster_); 75 callbackPoster_ = std::move(poster); 76 } 77 IsExternalSchedulingEnabled()78 bool IsExternalSchedulingEnabled() const 79 { 80 return callbackPoster_ != nullptr; 81 } 82 83 template <typename PosterCallback> PostExternalCallback(PosterCallback cb)84 void PostExternalCallback(PosterCallback cb) 85 { 86 os::memory::LockHolder l(posterLock_); 87 if (callbackPoster_ != nullptr) { 88 callbackPoster_->Post(std::move(cb)); 89 } 90 } 91 92 void OnCoroBecameActive(Coroutine *co); 93 94 private: 95 Runtime *runtime_ = nullptr; 96 PandaVM *vm_ = nullptr; 97 LocalStorage localStorage_; 98 // event loop poster 99 os::memory::Mutex posterLock_; 100 PandaUniquePtr<CallbackPoster> callbackPoster_; 101 }; 102 103 } // namespace ark 104 105 #endif /* PANDA_RUNTIME_COROUTINES_COROUTINE_WORKER_H */ 106