1 /* 2 * Copyright (c) 2021 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 ECMASCRIPT_MEM_CONCURRENT_MARKER_H 17 #define ECMASCRIPT_MEM_CONCURRENT_MARKER_H 18 19 #include <array> 20 #include <atomic> 21 22 #include "ecmascript/mem/object_xray.h" 23 #include "ecmascript/mem/parallel_work_helper.h" 24 #include "ecmascript/mem/space.h" 25 #include "ecmascript/platform/task.h" 26 #include "os/mutex.h" 27 28 namespace panda::ecmascript { 29 class Heap; 30 31 class ConcurrentMarker { 32 public: 33 ConcurrentMarker(Heap *heap); 34 ~ConcurrentMarker() = default; 35 36 void ConcurrentMarking(); 37 void FinishPhase(); 38 void ReMarking(); 39 40 void HandleMarkFinished(); // call in vm thread. 41 void WaitConcurrentMarkingFinished(); // call in main thread 42 void Reset(bool isRevertCSet = true); 43 GetDuration()44 double GetDuration() const 45 { 46 return duration_; 47 } 48 GetHeapObjectSize()49 double GetHeapObjectSize() const 50 { 51 return heapObjectSize_; 52 } 53 54 private: 55 NO_COPY_SEMANTIC(ConcurrentMarker); 56 NO_MOVE_SEMANTIC(ConcurrentMarker); 57 58 class MarkerTask : public Task { 59 public: MarkerTask(Heap * heap)60 MarkerTask(Heap *heap) : heap_(heap) {} 61 ~MarkerTask() override = default; 62 bool Run(uint32_t threadId) override; 63 64 private: 65 NO_COPY_SEMANTIC(MarkerTask); 66 NO_MOVE_SEMANTIC(MarkerTask); 67 68 Heap *heap_ {nullptr}; 69 }; 70 SetDuration(double duration)71 void SetDuration(double duration) 72 { 73 duration_ = duration; 74 } 75 76 void InitializeMarking(); 77 void MarkingFinished(float spendTime); 78 79 Heap *heap_ {nullptr}; 80 EcmaVM *vm_ {nullptr}; 81 JSThread *thread_ {nullptr}; 82 83 // obtain from heap 84 WorkerHelper *workList_ {nullptr}; 85 size_t heapObjectSize_ {0}; 86 double duration_ {0.0}; 87 88 bool notifyMarkingFinished_ {false}; // notify js-thread that marking is finished and need sweep 89 bool vmThreadWaitMarkingFinished_ {false}; // jsMainThread waiting for concurrentGC FINISHED 90 os::memory::Mutex waitMarkingFinishedMutex_; 91 os::memory::ConditionVariable waitMarkingFinishedCV_; 92 }; 93 } // namespace panda::ecmascript 94 #endif // ECMASCRIPT_MEM_CONCURRENT_MARKER_H 95