• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unistd.h>
19 
20 namespace Uscript {
21 static thread_local float g_scriptProportion = 1.0f;
22 static ThreadPool* g_threadPool = nullptr;
23 static std::mutex g_initMutex;
24 
SetScriptProportion(float proportion)25 void SetScriptProportion(float proportion)
26 {
27     g_scriptProportion = proportion;
28 }
29 
GetScriptProportion()30 float GetScriptProportion()
31 {
32     return g_scriptProportion;
33 }
34 
CreateThreadPool(int number)35 ThreadPool* ThreadPool::CreateThreadPool(int number)
36 {
37     std::lock_guard<std::mutex> lock(g_initMutex);
38     if (g_threadPool != nullptr) {
39         return g_threadPool;
40     }
41     g_threadPool = new ThreadPool();
42     g_threadPool->Init(number);
43     return g_threadPool;
44 }
45 
Destroy()46 void ThreadPool::Destroy()
47 {
48     if (g_threadPool == nullptr) {
49         return;
50     }
51     std::lock_guard<std::mutex> lock(g_initMutex);
52     delete g_threadPool;
53     g_threadPool = nullptr;
54 }
55 
Init(int32_t number)56 void ThreadPool::Init(int32_t number)
57 {
58     threadNumber_ = number;
59     taskQueue_.resize(threadPoolMaxTasks);
60     for (size_t t = 0; t < taskQueue_.size(); ++t) {
61         taskQueue_[t].available = true;
62         for (int32_t i = 0; i < threadNumber_; ++i) {
63             taskQueue_[t].subTaskFlag.emplace_back(new std::atomic_bool { false });
64         }
65     }
66     // Create workers
67     for (int32_t threadIndex = 0; threadIndex < threadNumber_; ++threadIndex) {
68         workers_.emplace_back(std::thread(ThreadPool::ThreadExecute, this, threadIndex));
69     }
70 }
71 
ThreadRun(int32_t threadIndex)72 void ThreadPool::ThreadRun(int32_t threadIndex)
73 {
74     USCRIPT_LOGI("Create new thread successfully, tid: %d", gettid());
75     while (!stop_) {
76         for (int32_t k = 0; k < threadPoolMaxTasks; ++k) {
77             if (*taskQueue_[k].subTaskFlag[threadIndex]) {
78                 taskQueue_[k].task.processor(threadIndex);
79                 *taskQueue_[k].subTaskFlag[threadIndex] = false;
80             }
81         }
82         std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 50ms
83     }
84 }
85 
~ThreadPool()86 ThreadPool::~ThreadPool()
87 {
88     {
89         std::lock_guard<std::mutex> lock(queueMutex_);
90         stop_ = true;
91     }
92     for (auto& worker : workers_) {
93         worker.join();
94     }
95     for (auto& task : taskQueue_) {
96         for (auto c : task.subTaskFlag) {
97             delete c;
98         }
99     }
100 }
101 
AddTask(Task && task)102 void ThreadPool::AddTask(Task &&task)
103 {
104     if (g_threadPool != nullptr) {
105         g_threadPool->AddNewTask(std::move(task));
106     }
107 }
108 
AddNewTask(Task && task)109 void ThreadPool::AddNewTask(Task &&task)
110 {
111     int32_t index = AcquireWorkIndex();
112     if (index < 0) {
113         USCRIPT_LOGI("ThreadPool::AddNewTask Failed");
114         return;
115     }
116 
117     RunTask(std::move(task), index);
118     // Works done. make this task available
119     std::lock_guard<std::mutex> lock(queueMutex_);
120     taskQueue_[index].available = true;
121 }
122 
AcquireWorkIndex()123 int32_t ThreadPool::AcquireWorkIndex()
124 {
125     std::lock_guard<std::mutex> lock(queueMutex_);
126     for (int32_t i = 0; i < threadPoolMaxTasks; ++i) {
127         if (taskQueue_[i].available) {
128             taskQueue_[i].available = false;
129             return i;
130         }
131     }
132     return -1;
133 }
134 
RunTask(Task && task,int32_t index)135 void ThreadPool::RunTask(Task &&task, int32_t index)
136 {
137     int32_t workSize = task.workSize;
138     taskQueue_[index].task = std::move(task);
139     // Mark each task should be executed
140     int32_t num = workSize > threadNumber_ ? threadNumber_ : workSize;
141     for (int32_t i = 0; i < num; ++i) {
142         *taskQueue_[index].subTaskFlag[i] = true;
143     }
144 
145     bool complete = true;
146     do {
147         std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 50ms
148         complete = true;
149         // 检查是否所有子任务执行结束
150         for (int32_t i = 0; i < num; ++i) {
151             if (*taskQueue_[index].subTaskFlag[i]) {
152                 complete = false;
153                 break;
154             }
155         }
156     } while (!complete);
157 }
158 } // namespace Uscript
159