worker_thread_pool.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*************************************************************************/
  2. /* worker_thread_pool.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "worker_thread_pool.h"
  31. #include "core/os/os.h"
  32. WorkerThreadPool *WorkerThreadPool::singleton = nullptr;
  33. void WorkerThreadPool::_process_task_queue() {
  34. task_mutex.lock();
  35. Task *task = task_queue.first()->self();
  36. task_queue.remove(task_queue.first());
  37. task_mutex.unlock();
  38. _process_task(task);
  39. }
  40. void WorkerThreadPool::_process_task(Task *p_task) {
  41. bool low_priority = p_task->low_priority;
  42. if (p_task->group) {
  43. // Handling a group
  44. bool do_post = false;
  45. if (p_task->native_group_func) {
  46. while (true) {
  47. uint32_t work_index = p_task->group->index.postincrement();
  48. if (work_index >= p_task->group->max) {
  49. do_post = work_index == p_task->group->max; // First one reaching max handles semaphore and clean-up.
  50. break;
  51. }
  52. p_task->native_group_func(p_task->native_func_userdata, work_index);
  53. }
  54. } else {
  55. Callable::CallError ce;
  56. Variant ret;
  57. Variant arg;
  58. Variant *argptr = &arg;
  59. while (true) {
  60. uint32_t work_index = p_task->group->index.postincrement();
  61. if (work_index >= p_task->group->max) {
  62. do_post = work_index == p_task->group->max; // First one reaching max handles semaphore and clean-up.
  63. break;
  64. }
  65. arg = work_index;
  66. p_task->callable.call((const Variant **)&argptr, 1, ret, ce);
  67. }
  68. }
  69. if (low_priority && use_native_low_priority_threads) {
  70. p_task->completed = true;
  71. p_task->done_semaphore.post();
  72. if (do_post) {
  73. p_task->group->completed.set_to(true);
  74. }
  75. } else {
  76. if (do_post) {
  77. p_task->group->done_semaphore.post();
  78. p_task->group->completed.set_to(true);
  79. }
  80. uint32_t max_users = p_task->group->tasks_used + 1; // Add 1 because the thread waiting for it is also user. Read before to avoid another thread freeing task after increment.
  81. uint32_t finished_users = p_task->group->finished.increment();
  82. if (finished_users == max_users) {
  83. // Get rid of the group, because nobody else is using it.
  84. task_mutex.lock();
  85. group_allocator.free(p_task->group);
  86. task_mutex.unlock();
  87. }
  88. // For groups, tasks get rid of themselves.
  89. task_mutex.lock();
  90. task_allocator.free(p_task);
  91. task_mutex.unlock();
  92. }
  93. } else {
  94. if (p_task->native_func) {
  95. p_task->native_func(p_task->native_func_userdata);
  96. } else {
  97. Callable::CallError ce;
  98. Variant ret;
  99. p_task->callable.call(nullptr, 0, ret, ce);
  100. }
  101. p_task->completed = true;
  102. p_task->done_semaphore.post();
  103. }
  104. if (!use_native_low_priority_threads && low_priority) {
  105. // A low prioriry task was freed, so see if we can move a pending one to the high priority queue.
  106. bool post = false;
  107. task_mutex.lock();
  108. if (low_priority_task_queue.first()) {
  109. Task *low_prio_task = low_priority_task_queue.first()->self();
  110. low_priority_task_queue.remove(low_priority_task_queue.first());
  111. task_queue.add_last(&low_prio_task->task_elem);
  112. post = true;
  113. } else {
  114. low_priority_threads_used.decrement();
  115. }
  116. task_mutex.lock();
  117. if (post) {
  118. task_available_semaphore.post();
  119. }
  120. }
  121. }
  122. void WorkerThreadPool::_thread_function(void *p_user) {
  123. while (true) {
  124. singleton->task_available_semaphore.wait();
  125. if (singleton->exit_threads.is_set()) {
  126. break;
  127. }
  128. singleton->_process_task_queue();
  129. }
  130. }
  131. void WorkerThreadPool::_native_low_priority_thread_function(void *p_user) {
  132. Task *task = (Task *)p_user;
  133. singleton->_process_task(task);
  134. }
  135. void WorkerThreadPool::_post_task(Task *p_task, bool p_high_priority) {
  136. task_mutex.lock();
  137. p_task->low_priority = !p_high_priority;
  138. if (!p_high_priority && use_native_low_priority_threads) {
  139. task_mutex.unlock();
  140. p_task->low_priority_thread = native_thread_allocator.alloc();
  141. p_task->low_priority_thread->start(_native_low_priority_thread_function, p_task); // Pask task directly to thread.
  142. } else if (p_high_priority || low_priority_threads_used.get() < max_low_priority_threads) {
  143. task_queue.add_last(&p_task->task_elem);
  144. if (!p_high_priority) {
  145. low_priority_threads_used.increment();
  146. }
  147. task_mutex.unlock();
  148. task_available_semaphore.post();
  149. } else {
  150. // Too many threads using low priority, must go to queue.
  151. low_priority_task_queue.add_last(&p_task->task_elem);
  152. task_mutex.unlock();
  153. }
  154. }
  155. WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority, const String &p_description) {
  156. task_mutex.lock();
  157. // Get a free task
  158. Task *task = task_allocator.alloc();
  159. TaskID id = last_task++;
  160. task->native_func = p_func;
  161. task->native_func_userdata = p_userdata;
  162. task->description = p_description;
  163. tasks.insert(id, task);
  164. task_mutex.unlock();
  165. _post_task(task, p_high_priority);
  166. return id;
  167. }
  168. WorkerThreadPool::TaskID WorkerThreadPool::add_task(const Callable &p_action, bool p_high_priority, const String &p_description) {
  169. task_mutex.lock();
  170. // Get a free task
  171. Task *task = task_allocator.alloc();
  172. TaskID id = last_task++;
  173. task->callable = p_action;
  174. task->description = p_description;
  175. tasks.insert(id, task);
  176. task_mutex.unlock();
  177. _post_task(task, p_high_priority);
  178. return id;
  179. }
  180. bool WorkerThreadPool::is_task_completed(TaskID p_task_id) const {
  181. task_mutex.lock();
  182. const Task *const *taskp = tasks.getptr(p_task_id);
  183. if (!taskp) {
  184. task_mutex.unlock();
  185. ERR_FAIL_V_MSG(false, "Invalid Task ID"); // Invalid task
  186. }
  187. bool completed = (*taskp)->completed;
  188. task_mutex.unlock();
  189. return completed;
  190. }
  191. void WorkerThreadPool::wait_for_task_completion(TaskID p_task_id) {
  192. task_mutex.lock();
  193. Task **taskp = tasks.getptr(p_task_id);
  194. if (!taskp) {
  195. task_mutex.unlock();
  196. ERR_FAIL_MSG("Invalid Task ID"); // Invalid task
  197. }
  198. Task *task = *taskp;
  199. if (task->waiting) {
  200. String description = task->description;
  201. task_mutex.unlock();
  202. if (description.is_empty()) {
  203. ERR_FAIL_MSG("Another thread is waiting on this task: " + itos(p_task_id)); // Invalid task
  204. } else {
  205. ERR_FAIL_MSG("Another thread is waiting on this task: " + description + " (" + itos(p_task_id) + ")"); // Invalid task
  206. }
  207. }
  208. task->waiting = true;
  209. task_mutex.unlock();
  210. if (use_native_low_priority_threads && task->low_priority) {
  211. task->low_priority_thread->wait_to_finish();
  212. native_thread_allocator.free(task->low_priority_thread);
  213. } else {
  214. int *index = thread_ids.getptr(Thread::get_caller_id());
  215. if (index) {
  216. // We are an actual process thread, we must not be blocked so continue processing stuff if available.
  217. while (true) {
  218. if (task->done_semaphore.try_wait()) {
  219. // If done, exit
  220. break;
  221. }
  222. if (task_available_semaphore.try_wait()) {
  223. // Solve tasks while they are around.
  224. _process_task_queue();
  225. continue;
  226. }
  227. OS::get_singleton()->delay_usec(1); // Microsleep, this could be converted to waiting for multiple objects in supported platforms for a bit more performance.
  228. }
  229. } else {
  230. task->done_semaphore.wait();
  231. }
  232. }
  233. task_mutex.lock();
  234. tasks.erase(p_task_id);
  235. task_allocator.free(task);
  236. task_mutex.unlock();
  237. }
  238. WorkerThreadPool::GroupID WorkerThreadPool::add_native_group_task(void (*p_func)(void *, uint32_t), void *p_userdata, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  239. ERR_FAIL_COND_V(p_elements <= 0, INVALID_TASK_ID);
  240. if (p_tasks < 0) {
  241. p_tasks = threads.size();
  242. }
  243. task_mutex.lock();
  244. Group *group = group_allocator.alloc();
  245. GroupID id = last_task++;
  246. group->max = p_elements;
  247. group->self = id;
  248. group->tasks_used = p_tasks;
  249. Task **tasks_posted = (Task **)alloca(sizeof(Task *) * p_tasks);
  250. for (int i = 0; i < p_tasks; i++) {
  251. Task *task = task_allocator.alloc();
  252. task->native_group_func = p_func;
  253. task->native_func_userdata = p_userdata;
  254. task->description = p_description;
  255. task->group = group;
  256. tasks_posted[i] = task;
  257. // No task ID is used.
  258. }
  259. groups[id] = group;
  260. task_mutex.unlock();
  261. if (!p_high_priority && use_native_low_priority_threads) {
  262. group->low_priority_native_tasks.resize(p_tasks);
  263. }
  264. for (int i = 0; i < p_tasks; i++) {
  265. _post_task(tasks_posted[i], p_high_priority);
  266. if (!p_high_priority && use_native_low_priority_threads) {
  267. group->low_priority_native_tasks[i] = tasks_posted[i];
  268. }
  269. }
  270. return id;
  271. }
  272. WorkerThreadPool::GroupID WorkerThreadPool::add_group_task(const Callable &p_action, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  273. ERR_FAIL_COND_V(p_elements <= 0, INVALID_TASK_ID);
  274. if (p_tasks < 0) {
  275. p_tasks = threads.size();
  276. }
  277. task_mutex.lock();
  278. Group *group = group_allocator.alloc();
  279. GroupID id = last_task++;
  280. group->max = p_elements;
  281. group->self = id;
  282. group->tasks_used = p_tasks;
  283. Task **tasks_posted = (Task **)alloca(sizeof(Task *) * p_tasks);
  284. for (int i = 0; i < p_tasks; i++) {
  285. Task *task = task_allocator.alloc();
  286. task->callable = p_action;
  287. task->description = p_description;
  288. task->group = group;
  289. tasks_posted[i] = task;
  290. // No task ID is used.
  291. }
  292. groups[id] = group;
  293. task_mutex.unlock();
  294. if (!p_high_priority && use_native_low_priority_threads) {
  295. group->low_priority_native_tasks.resize(p_tasks);
  296. }
  297. for (int i = 0; i < p_tasks; i++) {
  298. _post_task(tasks_posted[i], p_high_priority);
  299. if (!p_high_priority && use_native_low_priority_threads) {
  300. group->low_priority_native_tasks[i] = tasks_posted[i];
  301. }
  302. }
  303. return id;
  304. }
  305. bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const {
  306. task_mutex.lock();
  307. const Group *const *groupp = groups.getptr(p_group);
  308. if (!groupp) {
  309. task_mutex.unlock();
  310. ERR_FAIL_V_MSG(false, "Invalid Group ID");
  311. }
  312. bool completed = (*groupp)->completed.is_set();
  313. task_mutex.unlock();
  314. return completed;
  315. }
  316. void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) {
  317. task_mutex.lock();
  318. Group **groupp = groups.getptr(p_group);
  319. task_mutex.unlock();
  320. if (!groupp) {
  321. ERR_FAIL_MSG("Invalid Group ID");
  322. }
  323. Group *group = *groupp;
  324. if (group->low_priority_native_tasks.size() > 0) {
  325. for (uint32_t i = 0; i < group->low_priority_native_tasks.size(); i++) {
  326. group->low_priority_native_tasks[i]->low_priority_thread->wait_to_finish();
  327. native_thread_allocator.free(group->low_priority_native_tasks[i]->low_priority_thread);
  328. task_mutex.lock();
  329. task_allocator.free(group->low_priority_native_tasks[i]);
  330. task_mutex.unlock();
  331. }
  332. task_mutex.lock();
  333. group_allocator.free(group);
  334. task_mutex.unlock();
  335. } else {
  336. group->done_semaphore.wait();
  337. uint32_t max_users = group->tasks_used + 1; // Add 1 because the thread waiting for it is also user. Read before to avoid another thread freeing task after increment.
  338. uint32_t finished_users = group->finished.increment(); // fetch happens before inc, so increment later.
  339. if (finished_users == max_users) {
  340. // All tasks using this group are gone (finished before the group), so clear the gorup too.
  341. task_mutex.lock();
  342. group_allocator.free(group);
  343. task_mutex.unlock();
  344. }
  345. }
  346. groups.erase(p_group); // Threads do not access this, so safe to erase here.
  347. }
  348. void WorkerThreadPool::init(int p_thread_count, bool p_use_native_threads_low_priority, float p_low_priority_task_ratio) {
  349. ERR_FAIL_COND(threads.size() > 0);
  350. if (p_thread_count < 0) {
  351. p_thread_count = OS::get_singleton()->get_default_thread_pool_size();
  352. }
  353. if (p_use_native_threads_low_priority) {
  354. max_low_priority_threads = 0;
  355. } else {
  356. max_low_priority_threads = CLAMP(p_thread_count * p_low_priority_task_ratio, 1, p_thread_count);
  357. }
  358. use_native_low_priority_threads = p_use_native_threads_low_priority;
  359. threads.resize(p_thread_count);
  360. for (uint32_t i = 0; i < threads.size(); i++) {
  361. threads[i].index = i;
  362. threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i]);
  363. thread_ids.insert(threads[i].thread.get_id(), i);
  364. }
  365. }
  366. void WorkerThreadPool::finish() {
  367. if (threads.size() == 0) {
  368. return;
  369. }
  370. task_mutex.lock();
  371. SelfList<Task> *E = low_priority_task_queue.first();
  372. while (E) {
  373. print_error("Task waiting was never re-claimed: " + E->self()->description);
  374. E = E->next();
  375. }
  376. task_mutex.unlock();
  377. exit_threads.set_to(true);
  378. for (uint32_t i = 0; i < threads.size(); i++) {
  379. task_available_semaphore.post();
  380. }
  381. for (uint32_t i = 0; i < threads.size(); i++) {
  382. threads[i].thread.wait_to_finish();
  383. }
  384. threads.clear();
  385. }
  386. void WorkerThreadPool::_bind_methods() {
  387. ClassDB::bind_method(D_METHOD("add_task", "action", "high_priority", "description"), &WorkerThreadPool::add_task, DEFVAL(false), DEFVAL(String()));
  388. ClassDB::bind_method(D_METHOD("is_task_completed", "task_id"), &WorkerThreadPool::is_task_completed);
  389. ClassDB::bind_method(D_METHOD("wait_for_task_completion", "task_id"), &WorkerThreadPool::wait_for_task_completion);
  390. ClassDB::bind_method(D_METHOD("add_group_task", "action", "elements", "tasks_needed", "high_priority", "description"), &WorkerThreadPool::add_group_task, DEFVAL(-1), DEFVAL(false), DEFVAL(String()));
  391. ClassDB::bind_method(D_METHOD("is_group_task_completed", "group_id"), &WorkerThreadPool::is_group_task_completed);
  392. ClassDB::bind_method(D_METHOD("wait_for_group_task_completion", "group_id"), &WorkerThreadPool::wait_for_group_task_completion);
  393. }
  394. WorkerThreadPool::WorkerThreadPool() {
  395. singleton = this;
  396. }
  397. WorkerThreadPool::~WorkerThreadPool() {
  398. finish();
  399. }