1 /** 2 * Copyright (c) 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_PLUGINS_ETS_RUNTIME_FINALREG_FINALIZATION_REGISTRY_H 17 #define PANDA_PLUGINS_ETS_RUNTIME_FINALREG_FINALIZATION_REGISTRY_H 18 19 #include "runtime/mem/gc/gc.h" 20 #include "plugins/ets/runtime/ets_coroutine.h" 21 #include "plugins/ets/runtime/types/ets_object.h" 22 23 namespace ark::ets { 24 25 class PandaEtsVM; 26 27 /// @brief The final class that manages all created FinalizationRegistry objects 28 class FinalizationRegistryManager final { 29 public: FinalizationRegistryManager(PandaEtsVM * vm)30 explicit FinalizationRegistryManager(PandaEtsVM *vm) : vm_(vm) {} 31 NO_COPY_SEMANTIC(FinalizationRegistryManager); 32 NO_MOVE_SEMANTIC(FinalizationRegistryManager); 33 ~FinalizationRegistryManager() = default; 34 35 /** 36 * @brief Add a new created FinalizationRegistry instanse to array of FinalizationRegistry 37 * @param instance Object that represents WeakRef<FinalizationRegistry> 38 */ 39 void RegisterInstance(EtsObject *instance); 40 41 /** 42 * @brief Checks the number of called coroutines with cleanup and calls the next one 43 * if the number does not exceed MAX_FINREG_CLEANUP_COROS. 44 * @param coro Pointer to current coroutine 45 */ 46 void StartCleanupCoroIfNeeded(EtsCoroutine *coro); 47 48 /// @brief Decreases the counter of called cleanup coroutines 49 void CleanupCoroFinished(); 50 51 private: 52 void SortInstancies(); 53 54 void EnsureCapacity(EtsCoroutine *coro); 55 56 /// @brief Increase number of cleanup coroutines and check if not exceeds limit 57 bool UpdateFinRegCoroCountAndCheckIfCleanupNeeded(); 58 59 // Limit of cleanup coroutines count 60 static constexpr uint32_t MAX_FINREG_CLEANUP_COROS = 3; 61 // Initial size of the created array 62 static constexpr uint32_t INIT_SIZE = 10; 63 64 PandaEtsVM *vm_ {nullptr}; 65 size_t finRegLastIndex_ {0}; 66 std::atomic<uint32_t> finRegCleanupCoroCount_ {0}; 67 mem::Reference *finRegInstancies_ {nullptr}; 68 }; 69 } // namespace ark::ets 70 71 #endif // PANDA_PLUGINS_ETS_RUNTIME_FINALREG_FINALIZATION_REGISTRY_H