• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2024 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_WORKERS_TASK_POOL_H
17 #define PANDA_RUNTIME_MEM_GC_WORKERS_GC_WORKERS_TASK_POOL_H
18 
19 #include "runtime/mem/gc/gc.h"
20 #include "runtime/mem/gc/workers/gc_workers_tasks.h"
21 
22 namespace ark::mem {
23 
24 /// @brief Base abstract class for GC workers task pool
25 class GCWorkersTaskPool {
26 public:
GCWorkersTaskPool(GC * gc)27     explicit GCWorkersTaskPool(GC *gc) : gc_(gc) {}
28     NO_COPY_SEMANTIC(GCWorkersTaskPool);
29     NO_MOVE_SEMANTIC(GCWorkersTaskPool);
30     virtual ~GCWorkersTaskPool() = default;
31 
32     /**
33      * @brief Add new GCWorkerTask to gc workers task pool
34      * @param task new gc worker task for pool
35      * @see GCWorkerTask
36      *
37      * @return true if task successfully added to pool, false - otherwise
38      */
39     bool AddTask(GCWorkersTask &&task);
40 
41     /**
42      * @brief Add new GCWorkerTask to gc workers task pool by gc task type
43      * @param type type of new gc worker task for pool
44      * @see GCWorkerTaskTypes
45      *
46      * @return true if task successfully added to pool, false - otherwise
47      */
AddTask(GCWorkersTaskTypes type)48     ALWAYS_INLINE bool AddTask(GCWorkersTaskTypes type)
49     {
50         return AddTask(GCWorkersTask(type));
51     }
52 
53     /// @return pointer to used GC
GetGC()54     ALWAYS_INLINE GC *GetGC() const
55     {
56         return gc_;
57     }
58 
59     /// @brief Wait until all GC sended tasks will be solved
60     void WaitUntilTasksEnd();
61 
62 protected:
63     /**
64      * @brief If gc task pool is not empty then try to get gc workers task from gc task pool
65      * and run it in the current thread.
66      * It helps to gc workers
67      */
68     virtual void RunInCurrentThread() = 0;
69 
70     /**
71      * @brief Try to add new gc workers task based on specific implementation
72      * @param task gc worker task for pool
73      *
74      * @return true if task successfully added to pool, false - otherwise
75      */
76     virtual bool TryAddTask(GCWorkersTask &&task) = 0;
77 
78     /**
79      * @brief Run sended gc workers task from gc task pool on a worker
80      * @param task sended gc workers task
81      * @param worker_data specific data for one worker if needed
82      */
83     void RunGCWorkersTask(GCWorkersTask *task, void *workerData = nullptr);
84 
85 private:
86     // Wait for all sended tasks, time in ms
87     static constexpr uint64_t ALL_GC_TASKS_FINISH_WAIT_TIMEOUT = 1U;
88 
89     void IncreaseSolvedTasks();
90 
ResetTasks()91     ALWAYS_INLINE void ResetTasks() REQUIRES(allSolvedTasksCondVarLock_)
92     {
93         solvedTasks_ = 0U;
94         sendedTasks_ = 0U;
95         solvedTasksSnapshot_ = 0U;
96     }
97 
98     GC *gc_ {nullptr};
99 
100     os::memory::Mutex allSolvedTasksCondVarLock_;
101     /**
102      * @brief Conditional varible is used for waiting for all gc tasks at some point
103      * @see WaitUntilTasksEnd
104      * @see IncreaseSolvedTasks
105      */
106     os::memory::ConditionVariable allSolvedTasksCondVar_ GUARDED_BY(allSolvedTasksCondVarLock_);
107     // CC-OFFNXT(G.FMT.03) project code style
GUARDED_BY(allSolvedTasksCondVarLock_)108     size_t solvedTasksSnapshot_ GUARDED_BY(allSolvedTasksCondVarLock_) {0U};
109     std::atomic_size_t solvedTasks_ {0U};
110     std::atomic_size_t sendedTasks_ {0U};
111 };
112 
113 }  // namespace ark::mem
114 
115 #endif  // PANDA_RUNTIME_MEM_GC_WORKERS_GC_WORKERS_TASK_POOL_H
116