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/gc_workers_thread_pool.h"
17
18 #include <utility>
19 #include "runtime/mem/gc/gc.h"
20 #include "runtime/mem/region_space.h"
21
22 namespace panda::mem {
23
Init()24 bool GCWorkersProcessor::Init()
25 {
26 return gc_threads_pools_->GetGC()->InitWorker(&worker_data_);
27 }
28
Destroy()29 bool GCWorkersProcessor::Destroy()
30 {
31 gc_threads_pools_->GetGC()->DestroyWorker(worker_data_);
32 return true;
33 }
34
Process(GCWorkersTask task)35 bool GCWorkersProcessor::Process(GCWorkersTask task)
36 {
37 gc_threads_pools_->GetGC()->WorkerTaskProcessing(&task, worker_data_);
38 gc_threads_pools_->IncreaseSolvedTasks();
39 return true;
40 }
41
GCWorkersThreadPool(mem::InternalAllocatorPtr internal_allocator,GC * gc,size_t threads_count)42 GCWorkersThreadPool::GCWorkersThreadPool(mem::InternalAllocatorPtr internal_allocator, GC *gc, size_t threads_count)
43 : gc_(gc), internal_allocator_(internal_allocator)
44 {
45 ASSERT(gc->GetPandaVm() != nullptr);
46 queue_ = internal_allocator_->New<GCWorkersQueueSimple>(internal_allocator_, QUEUE_SIZE_MAX_SIZE);
47 worker_iface_ = internal_allocator_->New<GCWorkersCreationInterface>(gc->GetPandaVm());
48 thread_pool_ = internal_allocator_->New<ThreadPool<GCWorkersTask, GCWorkersProcessor, GCWorkersThreadPool *>>(
49 internal_allocator_, queue_, this, threads_count, "GC_WORKER",
50 static_cast<WorkerCreationInterface *>(worker_iface_));
51 }
52
~GCWorkersThreadPool()53 GCWorkersThreadPool::~GCWorkersThreadPool()
54 {
55 internal_allocator_->Delete(thread_pool_);
56 internal_allocator_->Delete(worker_iface_);
57 queue_->Finalize();
58 internal_allocator_->Delete(queue_);
59 }
60
WaitUntilTasksEnd()61 void GCWorkersThreadPool::WaitUntilTasksEnd()
62 {
63 thread_pool_->Help();
64 for (;;) {
65 os::memory::LockHolder lock(cond_var_lock_);
66 if (GetSolvedTasks() == GetSendedTasks()) {
67 break;
68 }
69 cond_var_.TimedWait(&cond_var_lock_, ALL_TASK_FINISH_WAIT_TIMEOUT);
70 }
71 ResetTasks();
72 }
73
74 } // namespace panda::mem
75