• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #ifndef PANDA_RUNTIME_MEM_GC_WORKERS_GC_WORKER_H
17 #define PANDA_RUNTIME_MEM_GC_WORKERS_GC_WORKER_H
18 
19 #include "libpandabase/taskmanager/task.h"
20 #include "runtime/mem/gc/gc_queue.h"
21 
22 namespace ark {
23 // Forward declaration for GCWorker
24 class Thread;
25 }  // namespace ark
26 
27 namespace ark::mem {
28 // Forward declaration for GCWorker
29 class GC;
30 
31 /**
32  * @class GCWorker represents GC-worker which runs GC tasks on a non-managed thread
33  * @brief The class implements interaction between GC and GC-worker. GC-worker can be separate internal GC-thread or
34  * a thread from TaskManager
35  */
36 class GCWorker {
37 public:
38     /**
39      * @brief Create local gc tasks queue and structure for gc panda thread.
40      * Don't start gc worker
41      * @see ark::Thread
42      * @see CreateAndStartWorker
43      * @see FinalizeAndDestroyWorker
44      *
45      * @param gc poiner to used GC
46      */
47     explicit GCWorker(GC *gc);
48     NO_COPY_SEMANTIC(GCWorker);
49     NO_MOVE_SEMANTIC(GCWorker);
50     ~GCWorker();
51 
52     /// @brief Create worker and start execution
53     void CreateAndStartWorker();
54     /// @brief Join (wait for) all worker tasks and destroy worker
55     void FinalizeAndDestroyWorker();
56 
57     /**
58      * @brief Add new gc task to GC worker. Try to add the task to local queue and then run it on a worker
59      * @param task gc task for running on a worker
60      * @return true if task was added to local task queue, false - otherwise
61      */
62     bool AddTask(PandaUniquePtr<GCTask> task);
63 
64     /// @brief Add all postponed tasks to gc queue
65     void OnPostponeGCEnd();
66 
67 private:
68     PandaUniquePtr<GCTask> GetTask();
69     void RunGC(PandaUniquePtr<GCTask> task);
70 
71     /* Internal thread specific functions */
72 
73     static void GCThreadLoop(GCWorker *gcWorker);
74 
75     /* TaskManager specific functions */
76 
77     void CreateAndAddTaskToTaskManager();
78     void GCTaskRunner();
79 
80     GC *gc_ {nullptr};
81     GCQueueInterface *gcTaskQueue_ {nullptr};
82     Thread *gcThread_ {nullptr};
83     uint32_t collectNumberMod_ {1U};
84     /* Internal thread specific variables */
85     std::thread *gcInternalThread_ {nullptr};
86     /* TaskManager specific variables */
87     taskmanager::RunnerCallback gcRunner_ {nullptr};
88     std::atomic_bool needToFinish_ {false};
89     os::memory::Mutex gcTaskRunMutex_;
90     os::memory::Mutex postponedTasksMutex_;
91     PandaQueue<PandaUniquePtr<GCTask>> postponedTasks_ GUARDED_BY(postponedTasksMutex_);
92 };
93 
94 }  // namespace ark::mem
95 
96 #endif  // PANDA_RUNTIME_MEM_GC_WORKERS_GC_WORKER_H
97