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 #include "libpandabase/taskmanager/task_scheduler.h"
17 #include "libpandabase/taskmanager/task_queue_set.h"
18 #include "libpandabase/taskmanager/task.h"
19 #include "libpandabase/utils/logger.h"
20 #include "libpandabase/os/thread.h"
21
22 namespace ark::taskmanager::internal {
23
WorkerThread(TaskScheduler * scheduler,TaskTimeStatsBase * taskTimeStats,const std::string & name)24 WorkerThread::WorkerThread(TaskScheduler *scheduler, TaskTimeStatsBase *taskTimeStats, const std::string &name)
25 : scheduler_(scheduler), taskTimeStats_(taskTimeStats), name_(name)
26 {
27 thread_ = new std::thread(&WorkerThread::WorkerLoop, this);
28 [[maybe_unused]] auto setNameCore = os::thread::SetThreadName(thread_->native_handle(), name.c_str());
29 ASSERT_PRINT(setNameCore == 0, setNameCore);
30 }
31
AddForegroundTask(TaskPtr task)32 void WorkerThread::AddForegroundTask(TaskPtr task)
33 {
34 foregroundTaskQueue_.Push(std::move(task));
35 }
36
AddBackgroundTask(TaskPtr task)37 void WorkerThread::AddBackgroundTask(TaskPtr task)
38 {
39 backgroundTaskQueue_.Push(std::move(task));
40 }
41
Join()42 void WorkerThread::Join()
43 {
44 thread_->join();
45 }
46
IsEmpty() const47 bool WorkerThread::IsEmpty() const
48 {
49 return foregroundTaskQueue_.IsEmpty() && backgroundTaskQueue_.IsEmpty();
50 }
51
Size() const52 size_t WorkerThread::Size() const
53 {
54 return foregroundTaskQueue_.Size() + backgroundTaskQueue_.Size();
55 }
56
PopTask()57 TaskPtr WorkerThread::PopTask()
58 {
59 TaskPtr task;
60 if (foregroundTaskQueue_.TryPop(&task)) {
61 return task;
62 }
63 backgroundTaskQueue_.TryPop(&task);
64 return task;
65 }
66
PopForegroundTask()67 TaskPtr WorkerThread::PopForegroundTask()
68 {
69 TaskPtr task;
70 foregroundTaskQueue_.TryPop(&task);
71 return task;
72 }
73
PopBackgroundTask()74 TaskPtr WorkerThread::PopBackgroundTask()
75 {
76 TaskPtr task;
77 backgroundTaskQueue_.TryPop(&task);
78 return task;
79 }
80
WorkerLoop()81 void WorkerThread::WorkerLoop()
82 {
83 if (taskTimeStats_ != nullptr) {
84 taskTimeStats_->RegisterWorkerThread();
85 }
86 auto finishCond = false;
87 while (!finishCond) {
88 finishCond = scheduler_->FillWithTasks(this);
89 ExecuteTasksFromLocalQueue();
90 }
91 }
92
ExecuteTasksFromLocalQueue()93 size_t WorkerThread::ExecuteTasksFromLocalQueue()
94 {
95 size_t executed = 0;
96 // CC-OFFNXT(G.CTL.03): false positive
97 while (true) {
98 auto task = PopTask();
99 if (task == nullptr) {
100 break;
101 }
102 task->RunTask();
103 executed++;
104 }
105 return executed;
106 }
107
Start()108 void WorkerThread::Start()
109 {
110 os::memory::LockHolder<os::memory::Mutex> lockHolder(startWaitLock_);
111 start_ = true;
112 startWaitCondVar_.SignalAll();
113 }
114
WaitForStart()115 void WorkerThread::WaitForStart()
116 {
117 os::memory::LockHolder<os::memory::Mutex> lockHolder(startWaitLock_);
118 while (!start_) {
119 startWaitCondVar_.Wait(&startWaitLock_);
120 }
121 }
122
GetWorkerName() const123 std::string WorkerThread::GetWorkerName() const
124 {
125 return name_;
126 }
127
~WorkerThread()128 WorkerThread::~WorkerThread()
129 {
130 delete thread_;
131 }
132
133 } // namespace ark::taskmanager::internal
134