1 /*
2 * Copyright (c) 2021 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 #include "thread_pool.h"
16 #include <cstring>
17 #include "script_utils.h"
18
19 namespace Uscript {
20 static ThreadPool* g_threadPool = nullptr;
21 static std::mutex g_initMutex;
22
CreateThreadPool(int number)23 ThreadPool* ThreadPool::CreateThreadPool(int number)
24 {
25 if (number <= 1) {
26 USCRIPT_LOGE("Invalid number %d", number);
27 return nullptr;
28 }
29 std::lock_guard<std::mutex> lock(g_initMutex);
30 if (g_threadPool != nullptr) {
31 return g_threadPool;
32 }
33 g_threadPool = new ThreadPool();
34 g_threadPool->Init(number);
35 return g_threadPool;
36 }
37
Destroy()38 void ThreadPool::Destroy()
39 {
40 if (g_threadPool == nullptr) {
41 return;
42 }
43 std::lock_guard<std::mutex> lock(g_initMutex);
44 delete g_threadPool;
45 g_threadPool = nullptr;
46 }
47
Init(int32_t numberThread)48 void ThreadPool::Init(int32_t numberThread)
49 {
50 threadNumber_ = numberThread;
51 taskQueue_.resize(THREAD_POOL_MAX_TASKS);
52 for (size_t t = 0; t < taskQueue_.size(); ++t) {
53 taskQueue_[t].available = true;
54 for (int32_t i = 0; i < threadNumber_; ++i) {
55 taskQueue_[t].subTaskFlag.emplace_back(new std::atomic_bool { false });
56 }
57 }
58 // Create workers
59 for (int32_t threadIndex = 1; threadIndex < threadNumber_; ++threadIndex) {
60 workers_.emplace_back(std::thread(ThreadPool::ThreadExecute, this, threadIndex));
61 }
62 }
63
ThreadRun(int32_t threadIndex)64 void ThreadPool::ThreadRun(int32_t threadIndex)
65 {
66 while (!stop_) {
67 for (int32_t k = 0; k < THREAD_POOL_MAX_TASKS; ++k) {
68 if (*taskQueue_[k].subTaskFlag[threadIndex]) {
69 taskQueue_[k].task.processor(threadIndex);
70 *taskQueue_[k].subTaskFlag[threadIndex] = false;
71 }
72 }
73 std::this_thread::yield();
74 }
75 printf("ThreadPool::ThreadRun %d exit \n", threadIndex);
76 }
77
~ThreadPool()78 ThreadPool::~ThreadPool()
79 {
80 {
81 std::lock_guard<std::mutex> lock(queueMutex_);
82 stop_ = true;
83 }
84 for (auto& worker : workers_) {
85 worker.join();
86 }
87 for (auto& task : taskQueue_) {
88 for (auto c : task.subTaskFlag) {
89 delete c;
90 }
91 }
92 }
93
AddTask(Task && task)94 void ThreadPool::AddTask(Task &&task)
95 {
96 if (g_threadPool != nullptr) {
97 g_threadPool->AddNewTask(std::move(task));
98 }
99 }
100
AddNewTask(Task && task)101 void ThreadPool::AddNewTask(Task &&task)
102 {
103 int32_t index = AcquireWorkIndex();
104 USCRIPT_LOGI("ThreadPool::AddNewTask %d ", index);
105
106 // If there are no multi-works
107 // Do not need to enqueue, execute it immediately
108 if (task.workSize <= 1 || index < 0) {
109 for (int32_t i = 0; i < task.workSize; ++i) {
110 task.processor(i);
111 }
112 return;
113 }
114
115 int32_t workSize = task.workSize;
116 // If too much works, Create new task to do the work.
117 if (workSize > threadNumber_) {
118 Task newTask;
119 newTask.workSize = threadNumber_;
120 newTask.processor = [workSize, &task, this](int tId) {
121 for (int v = tId; v < workSize; v += threadNumber_) {
122 task.processor(v);
123 }
124 };
125 RunTask(std::move(newTask), index);
126 } else {
127 RunTask(std::move(task), index);
128 }
129
130 // Works done. make this task available
131 std::lock_guard<std::mutex> lock(queueMutex_);
132 taskQueue_[index].available = true;
133 }
134
AcquireWorkIndex()135 int32_t ThreadPool::AcquireWorkIndex()
136 {
137 std::lock_guard<std::mutex> lock(queueMutex_);
138 for (int32_t i = 0; i < THREAD_POOL_MAX_TASKS; ++i) {
139 if (taskQueue_[i].available) {
140 taskQueue_[i].available = false;
141 return i;
142 }
143 }
144 return -1;
145 }
146
RunTask(Task && task,int32_t index)147 void ThreadPool::RunTask(Task &&task, int32_t index)
148 {
149 taskQueue_[index].task = std::move(task);
150 int32_t workSize = task.workSize;
151 // Mark each task should be executed
152 for (int32_t i = 1; i < workSize; ++i) {
153 *taskQueue_[index].subTaskFlag[i] = true;
154 }
155
156 // Execute first task
157 taskQueue_[index].task.processor(0);
158 bool complete = true;
159 do {
160 std::this_thread::yield();
161 complete = true;
162 // 检查是否所有子任务执行结束
163 for (int32_t i = 1; i < workSize; ++i) {
164 if (*taskQueue_[index].subTaskFlag[i]) {
165 complete = false;
166 break;
167 }
168 }
169 } while (!complete);
170 }
171 } // namespace Uscript
172