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 #include "ecmascript/checkpoint/thread_state_transition.h"
17 #include "ecmascript/cross_vm/unified_gc/unified_gc_marker-inl.h"
18 #include "ecmascript/mem/concurrent_marker.h"
19 #include "ecmascript/mem/verification.h"
20
21 namespace panda::ecmascript {
22
Initialize()23 void UnifiedGCMarker::Initialize()
24 {
25 if (initialized_.load(std::memory_order_acquire)) {
26 return;
27 }
28 LockHolder holder(initializeMutex_);
29 if (!initialized_.load(std::memory_order_relaxed)) {
30 heap_->UnifiedGCPrepare();
31 heap_->GetAppSpawnSpace()->EnumerateRegions([](Region *current) {
32 current->ClearMarkGCBitset();
33 });
34 workManager_->Initialize(TriggerGCType::UNIFIED_GC, ParallelGCTaskPhase::UNIFIED_HANDLE_GLOBAL_POOL_TASK);
35 initialized_.store(true, std::memory_order_release);
36 }
37 }
38
InitialMark(uint32_t threadId)39 void UnifiedGCMarker::InitialMark(uint32_t threadId)
40 {
41 UnifiedGCMarkRootsScope scope(heap_->GetJSThread());
42 UnifiedGCMarkRootVisitor visitor(workManager_->GetWorkNodeHolder(threadId), this);
43 MarkRoots(visitor);
44 }
45
Finish()46 void UnifiedGCMarker::Finish()
47 {
48 initialized_.store(false, std::memory_order_relaxed);
49 workManager_->Finish();
50 heap_->Resume(TriggerGCType::UNIFIED_GC);
51 }
52
MarkFromObject(TaggedObject * object)53 void UnifiedGCMarker::MarkFromObject(TaggedObject *object)
54 {
55 Initialize();
56 Region *objectRegion = Region::ObjectAddressToRange(object);
57
58 if (objectRegion->InSharedHeap()) {
59 return;
60 }
61
62 if (objectRegion->AtomicMark(object)) {
63 workManager_->PushObjectToGlobal(object);
64 }
65 }
66
ProcessMarkStack(uint32_t threadId)67 void UnifiedGCMarker::ProcessMarkStack(uint32_t threadId)
68 {
69 WorkNodeHolder *workNodeHolder = workManager_->GetWorkNodeHolder(threadId);
70 UnifiedGCMarkObjectVisitor visitor(workNodeHolder, this);
71
72 TaggedObject *obj = nullptr;
73 while (workNodeHolder->Pop(&obj)) {
74 JSHClass *jsHclass = obj->SynchronizedGetClass();
75 visitor.VisitHClass(jsHclass);
76 ObjectXRay::VisitObjectBody<VisitType::OLD_GC_VISIT>(obj, jsHclass, visitor);
77 }
78 }
79 } // namespace panda::ecmascript