• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 "runtime/mem/gc/g1/update_remset_thread.h"
17 
18 #include "libpandabase/utils/logger.h"
19 #include "runtime/include/language_config.h"
20 #include "runtime/mem/gc/g1/g1-gc.h"
21 #include "runtime/mem/gc/g1/update_remset_worker.h"
22 
23 namespace panda::mem {
24 
25 template <class LanguageConfig>
UpdateRemsetThread(G1GC<LanguageConfig> * gc,GCG1BarrierSet::ThreadLocalCardQueues * queue,os::memory::Mutex * queueLock,size_t regionSize,bool updateConcurrent,size_t minConcurrentCardsToProcess)26 UpdateRemsetThread<LanguageConfig>::UpdateRemsetThread(G1GC<LanguageConfig> *gc,
27                                                        GCG1BarrierSet::ThreadLocalCardQueues *queue,
28                                                        os::memory::Mutex *queueLock, size_t regionSize,
29                                                        bool updateConcurrent, size_t minConcurrentCardsToProcess)
30     : UpdateRemsetWorker<LanguageConfig>(gc, queue, queueLock, regionSize, updateConcurrent,
31                                          minConcurrentCardsToProcess)
32 {
33 }
34 
35 template <class LanguageConfig>
36 UpdateRemsetThread<LanguageConfig>::~UpdateRemsetThread() = default;
37 
38 template <class LanguageConfig>
CreateWorkerImpl()39 void UpdateRemsetThread<LanguageConfig>::CreateWorkerImpl()
40 {
41     LOG(DEBUG, GC) << "Start creating UpdateRemsetThread";
42     os::memory::LockHolder lk(this->updateRemsetLock_);
43     auto internalAllocator = this->GetGC()->GetInternalAllocator();
44     ASSERT(updateThread_ == nullptr);
45     updateThread_ = internalAllocator->template New<std::thread>(&UpdateRemsetThread::ThreadLoop, this);
46     int res = os::thread::SetThreadName(updateThread_->native_handle(), "UpdateRemset");
47 
48     LOG_IF(res != 0, ERROR, RUNTIME) << "Failed to set a name for the UpdateRemset thread";
49 }
50 
51 template <class LanguageConfig>
DestroyWorkerImpl()52 void UpdateRemsetThread<LanguageConfig>::DestroyWorkerImpl()
53 {
54     LOG(DEBUG, GC) << "Starting destroy UpdateRemsetThread";
55     {
56         os::memory::LockHolder holder(this->updateRemsetLock_);
57         threadCondVar_.SignalAll();  // wake up updateremset worker & pause method
58     }
59     ASSERT(updateThread_ != nullptr);
60     updateThread_->join();
61     auto allocator = this->GetGC()->GetInternalAllocator();
62     ASSERT(allocator != nullptr);
63     allocator->Delete(updateThread_);
64     updateThread_ = nullptr;
65     LOG(DEBUG, GC) << "UpdateRemsetThread was destroyed";
66 }
67 
68 template <class LanguageConfig>
ThreadLoop()69 void UpdateRemsetThread<LanguageConfig>::ThreadLoop()
70 {
71     LOG(DEBUG, GC) << "Entering UpdateRemsetThread ThreadLoop";
72 
73     this->updateRemsetLock_.Lock();
74     while (true) {
75         // Do one atomic load before checks
76         auto iterationFlag = this->GetFlag();
77         if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_STOP_WORKER) {
78             LOG(DEBUG, GC) << "exit UpdateRemsetThread loop, because thread was stopped";
79             break;
80         }
81         if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_PAUSED_BY_GC_THREAD) {
82             // UpdateRemsetThread is paused by GC, wait until GC notifies to continue work
83             threadCondVar_.Wait(&this->updateRemsetLock_);
84             continue;
85         }
86         if (iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_INVALIDATE_REGIONS) {
87             // wait until GC-Thread invalidates regions
88             threadCondVar_.Wait(&this->updateRemsetLock_);
89             continue;
90         }
91         ASSERT(!this->pausedByGcThread_);
92         ASSERT(iterationFlag == UpdateRemsetWorker<LanguageConfig>::UpdateRemsetWorkerFlags::IS_PROCESS_CARD);
93         auto processedCards = this->ProcessAllCards();
94         if (processedCards < this->GetMinConcurrentCardsToProcess()) {
95             Sleep();
96         }
97     }
98     this->updateRemsetLock_.Unlock();
99     LOG(DEBUG, GC) << "Exiting UpdateRemsetThread ThreadLoop";
100 }
101 
102 template <class LanguageConfig>
ContinueProcessCards()103 void UpdateRemsetThread<LanguageConfig>::ContinueProcessCards()
104 {
105     // Signal to continue UpdateRemsetThread
106     threadCondVar_.Signal();
107 }
108 
109 TEMPLATE_CLASS_LANGUAGE_CONFIG(UpdateRemsetThread);
110 }  // namespace panda::mem
111