14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_SWEEPER_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_SWEEPER_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/mem/concurrent_sweeper.h" 204514f5e3Sopenharmony_ci 214514f5e3Sopenharmony_cinamespace panda::ecmascript { 224514f5e3Sopenharmony_ciclass SharedHeap; 234514f5e3Sopenharmony_ci 244514f5e3Sopenharmony_ciclass SharedConcurrentSweeper { 254514f5e3Sopenharmony_cipublic: 264514f5e3Sopenharmony_ci SharedConcurrentSweeper(SharedHeap *heap, EnableConcurrentSweepType type); 274514f5e3Sopenharmony_ci ~SharedConcurrentSweeper() = default; 284514f5e3Sopenharmony_ci 294514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(SharedConcurrentSweeper); 304514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(SharedConcurrentSweeper); 314514f5e3Sopenharmony_ci 324514f5e3Sopenharmony_ci void PostTask(bool isFullGC); 334514f5e3Sopenharmony_ci void Sweep(bool isFullGC); 344514f5e3Sopenharmony_ci 354514f5e3Sopenharmony_ci void WaitAllTaskFinished(); 364514f5e3Sopenharmony_ci // Help to finish sweeping task. It can be called through js thread 374514f5e3Sopenharmony_ci void EnsureAllTaskFinished(); 384514f5e3Sopenharmony_ci // Ensure task finish. It can be called through js thread 394514f5e3Sopenharmony_ci void EnsureTaskFinished(MemSpaceType type); 404514f5e3Sopenharmony_ci 414514f5e3Sopenharmony_ci void TryFillSweptRegion(); 424514f5e3Sopenharmony_ci 434514f5e3Sopenharmony_ci void EnableConcurrentSweep(EnableConcurrentSweepType type); 444514f5e3Sopenharmony_ci 454514f5e3Sopenharmony_ci bool IsSweeping() 464514f5e3Sopenharmony_ci { 474514f5e3Sopenharmony_ci return isSweeping_; 484514f5e3Sopenharmony_ci } 494514f5e3Sopenharmony_ci 504514f5e3Sopenharmony_ci bool ConcurrentSweepEnabled() 514514f5e3Sopenharmony_ci { 524514f5e3Sopenharmony_ci return !IsDisabled(); 534514f5e3Sopenharmony_ci } 544514f5e3Sopenharmony_ci 554514f5e3Sopenharmony_ci void ConfigConcurrentSweep(bool enabled) 564514f5e3Sopenharmony_ci { 574514f5e3Sopenharmony_ci enableType_ = enabled ? EnableConcurrentSweepType::ENABLE : 584514f5e3Sopenharmony_ci EnableConcurrentSweepType::CONFIG_DISABLE; 594514f5e3Sopenharmony_ci } 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ci bool IsDisabled() const 624514f5e3Sopenharmony_ci { 634514f5e3Sopenharmony_ci return enableType_ == EnableConcurrentSweepType::DISABLE || 644514f5e3Sopenharmony_ci enableType_ == EnableConcurrentSweepType::CONFIG_DISABLE; 654514f5e3Sopenharmony_ci } 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ci bool IsRequestDisabled() const 684514f5e3Sopenharmony_ci { 694514f5e3Sopenharmony_ci return enableType_ == EnableConcurrentSweepType::REQUEST_DISABLE; 704514f5e3Sopenharmony_ci } 714514f5e3Sopenharmony_ci 724514f5e3Sopenharmony_ci bool IsConfigDisabled() const 734514f5e3Sopenharmony_ci { 744514f5e3Sopenharmony_ci return enableType_ == EnableConcurrentSweepType::CONFIG_DISABLE; 754514f5e3Sopenharmony_ci } 764514f5e3Sopenharmony_ciprivate: 774514f5e3Sopenharmony_ci class SweeperTask : public Task { 784514f5e3Sopenharmony_ci public: 794514f5e3Sopenharmony_ci SweeperTask(int32_t id, SharedConcurrentSweeper *sweeper, MemSpaceType type) 804514f5e3Sopenharmony_ci : Task(id), sweeper_(sweeper), type_(type) {}; 814514f5e3Sopenharmony_ci ~SweeperTask() override = default; 824514f5e3Sopenharmony_ci bool Run(uint32_t threadIndex) override; 834514f5e3Sopenharmony_ci 844514f5e3Sopenharmony_ci NO_COPY_SEMANTIC(SweeperTask); 854514f5e3Sopenharmony_ci NO_MOVE_SEMANTIC(SweeperTask); 864514f5e3Sopenharmony_ci 874514f5e3Sopenharmony_ci private: 884514f5e3Sopenharmony_ci SharedConcurrentSweeper *sweeper_; 894514f5e3Sopenharmony_ci MemSpaceType type_; 904514f5e3Sopenharmony_ci }; 914514f5e3Sopenharmony_ci 924514f5e3Sopenharmony_ci void AsyncSweepSpace(MemSpaceType type, bool isMain); 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci void WaitingTaskFinish(MemSpaceType type); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci std::array<Mutex, SHARED_SWEEPING_SPACE_NUM> mutexs_; 974514f5e3Sopenharmony_ci std::array<ConditionVariable, SHARED_SWEEPING_SPACE_NUM> cvs_; 984514f5e3Sopenharmony_ci std::array<std::atomic_int, SHARED_SWEEPING_SPACE_NUM> remainingTaskNum_ = {0, 0}; 994514f5e3Sopenharmony_ci 1004514f5e3Sopenharmony_ci SharedHeap *sHeap_; 1014514f5e3Sopenharmony_ci EnableConcurrentSweepType enableType_ {EnableConcurrentSweepType::CONFIG_DISABLE}; 1024514f5e3Sopenharmony_ci bool isSweeping_ {false}; 1034514f5e3Sopenharmony_ci bool isFullGC_ {false}; 1044514f5e3Sopenharmony_ci}; 1054514f5e3Sopenharmony_ci} // namespace panda::ecmascript 1064514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MEM_SHARED_HEAP_SHARED_CONCURRENT_SWEEPER_H 107