worker_thread_pool.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**************************************************************************/
  2. /* worker_thread_pool.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. void WorkerThreadPool::Task::free_template_userdata() {
  33. ERR_FAIL_COND(!template_userdata);
  34. ERR_FAIL_COND(native_func_userdata == nullptr);
  35. BaseTemplateUserdata *btu = (BaseTemplateUserdata *)native_func_userdata;
  36. memdelete(btu);
  37. }
  38. WorkerThreadPool *WorkerThreadPool::singleton = nullptr;
  39. void WorkerThreadPool::_process_task_queue() {
  40. task_mutex.lock();
  41. Task *task = task_queue.first()->self();
  42. task_queue.remove(task_queue.first());
  43. task_mutex.unlock();
  44. _process_task(task);
  45. }
  46. void WorkerThreadPool::_process_task(Task *p_task) {
  47. bool low_priority = p_task->low_priority;
  48. if (p_task->group) {
  49. // Handling a group
  50. bool do_post = false;
  51. Callable::CallError ce;
  52. Variant ret;
  53. Variant arg;
  54. Variant *argptr = &arg;
  55. while (true) {
  56. uint32_t work_index = p_task->group->index.postincrement();
  57. if (work_index >= p_task->group->max) {
  58. break;
  59. }
  60. if (p_task->native_group_func) {
  61. p_task->native_group_func(p_task->native_func_userdata, work_index);
  62. } else if (p_task->template_userdata) {
  63. p_task->template_userdata->callback_indexed(work_index);
  64. } else {
  65. arg = work_index;
  66. p_task->callable.callp((const Variant **)&argptr, 1, ret, ce);
  67. }
  68. // This is the only way to ensure posting is done when all tasks are really complete.
  69. uint32_t completed_amount = p_task->group->completed_index.increment();
  70. if (completed_amount == p_task->group->max) {
  71. do_post = true;
  72. }
  73. }
  74. if (do_post && p_task->template_userdata) {
  75. memdelete(p_task->template_userdata); // This is no longer needed at this point, so get rid of it.
  76. }
  77. if (low_priority && use_native_low_priority_threads) {
  78. p_task->completed = true;
  79. p_task->done_semaphore.post();
  80. if (do_post) {
  81. p_task->group->completed.set_to(true);
  82. }
  83. } else {
  84. if (do_post) {
  85. p_task->group->done_semaphore.post();
  86. p_task->group->completed.set_to(true);
  87. }
  88. 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.
  89. uint32_t finished_users = p_task->group->finished.increment();
  90. if (finished_users == max_users) {
  91. // Get rid of the group, because nobody else is using it.
  92. task_mutex.lock();
  93. group_allocator.free(p_task->group);
  94. task_mutex.unlock();
  95. }
  96. // For groups, tasks get rid of themselves.
  97. task_mutex.lock();
  98. task_allocator.free(p_task);
  99. task_mutex.unlock();
  100. }
  101. } else {
  102. if (p_task->native_func) {
  103. p_task->native_func(p_task->native_func_userdata);
  104. } else if (p_task->template_userdata) {
  105. p_task->template_userdata->callback();
  106. memdelete(p_task->template_userdata);
  107. } else {
  108. Callable::CallError ce;
  109. Variant ret;
  110. p_task->callable.callp(nullptr, 0, ret, ce);
  111. }
  112. p_task->completed = true;
  113. p_task->done_semaphore.post();
  114. }
  115. if (!use_native_low_priority_threads && low_priority) {
  116. // A low prioriry task was freed, so see if we can move a pending one to the high priority queue.
  117. bool post = false;
  118. task_mutex.lock();
  119. if (low_priority_task_queue.first()) {
  120. Task *low_prio_task = low_priority_task_queue.first()->self();
  121. low_priority_task_queue.remove(low_priority_task_queue.first());
  122. task_queue.add_last(&low_prio_task->task_elem);
  123. post = true;
  124. } else {
  125. low_priority_threads_used.decrement();
  126. }
  127. task_mutex.lock();
  128. if (post) {
  129. task_available_semaphore.post();
  130. }
  131. }
  132. }
  133. void WorkerThreadPool::_thread_function(void *p_user) {
  134. while (true) {
  135. singleton->task_available_semaphore.wait();
  136. if (singleton->exit_threads.is_set()) {
  137. break;
  138. }
  139. singleton->_process_task_queue();
  140. }
  141. }
  142. void WorkerThreadPool::_native_low_priority_thread_function(void *p_user) {
  143. Task *task = (Task *)p_user;
  144. singleton->_process_task(task);
  145. }
  146. void WorkerThreadPool::_post_task(Task *p_task, bool p_high_priority) {
  147. task_mutex.lock();
  148. p_task->low_priority = !p_high_priority;
  149. if (!p_high_priority && use_native_low_priority_threads) {
  150. task_mutex.unlock();
  151. p_task->low_priority_thread = native_thread_allocator.alloc();
  152. p_task->low_priority_thread->start(_native_low_priority_thread_function, p_task); // Pask task directly to thread.
  153. } else if (p_high_priority || low_priority_threads_used.get() < max_low_priority_threads) {
  154. task_queue.add_last(&p_task->task_elem);
  155. if (!p_high_priority) {
  156. low_priority_threads_used.increment();
  157. }
  158. task_mutex.unlock();
  159. task_available_semaphore.post();
  160. } else {
  161. // Too many threads using low priority, must go to queue.
  162. low_priority_task_queue.add_last(&p_task->task_elem);
  163. task_mutex.unlock();
  164. }
  165. }
  166. WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority, const String &p_description) {
  167. return _add_task(Callable(), p_func, p_userdata, nullptr, p_high_priority, p_description);
  168. }
  169. WorkerThreadPool::TaskID WorkerThreadPool::_add_task(const Callable &p_callable, void (*p_func)(void *), void *p_userdata, BaseTemplateUserdata *p_template_userdata, bool p_high_priority, const String &p_description) {
  170. task_mutex.lock();
  171. // Get a free task
  172. Task *task = task_allocator.alloc();
  173. TaskID id = last_task++;
  174. task->callable = p_callable;
  175. task->native_func = p_func;
  176. task->native_func_userdata = p_userdata;
  177. task->description = p_description;
  178. task->template_userdata = p_template_userdata;
  179. tasks.insert(id, task);
  180. task_mutex.unlock();
  181. _post_task(task, p_high_priority);
  182. return id;
  183. }
  184. WorkerThreadPool::TaskID WorkerThreadPool::add_task(const Callable &p_action, bool p_high_priority, const String &p_description) {
  185. return _add_task(p_action, nullptr, nullptr, nullptr, p_high_priority, p_description);
  186. }
  187. bool WorkerThreadPool::is_task_completed(TaskID p_task_id) const {
  188. task_mutex.lock();
  189. const Task *const *taskp = tasks.getptr(p_task_id);
  190. if (!taskp) {
  191. task_mutex.unlock();
  192. ERR_FAIL_V_MSG(false, "Invalid Task ID"); // Invalid task
  193. }
  194. bool completed = (*taskp)->completed;
  195. task_mutex.unlock();
  196. return completed;
  197. }
  198. void WorkerThreadPool::wait_for_task_completion(TaskID p_task_id) {
  199. task_mutex.lock();
  200. Task **taskp = tasks.getptr(p_task_id);
  201. if (!taskp) {
  202. task_mutex.unlock();
  203. ERR_FAIL_MSG("Invalid Task ID"); // Invalid task
  204. }
  205. Task *task = *taskp;
  206. if (task->waiting) {
  207. String description = task->description;
  208. task_mutex.unlock();
  209. if (description.is_empty()) {
  210. ERR_FAIL_MSG("Another thread is waiting on this task: " + itos(p_task_id)); // Invalid task
  211. } else {
  212. ERR_FAIL_MSG("Another thread is waiting on this task: " + description + " (" + itos(p_task_id) + ")"); // Invalid task
  213. }
  214. }
  215. task->waiting = true;
  216. task_mutex.unlock();
  217. if (use_native_low_priority_threads && task->low_priority) {
  218. task->low_priority_thread->wait_to_finish();
  219. native_thread_allocator.free(task->low_priority_thread);
  220. } else {
  221. int *index = thread_ids.getptr(Thread::get_caller_id());
  222. if (index) {
  223. // We are an actual process thread, we must not be blocked so continue processing stuff if available.
  224. while (true) {
  225. if (task->done_semaphore.try_wait()) {
  226. // If done, exit
  227. break;
  228. }
  229. if (task_available_semaphore.try_wait()) {
  230. // Solve tasks while they are around.
  231. _process_task_queue();
  232. continue;
  233. }
  234. OS::get_singleton()->delay_usec(1); // Microsleep, this could be converted to waiting for multiple objects in supported platforms for a bit more performance.
  235. }
  236. } else {
  237. task->done_semaphore.wait();
  238. }
  239. }
  240. task_mutex.lock();
  241. tasks.erase(p_task_id);
  242. task_allocator.free(task);
  243. task_mutex.unlock();
  244. }
  245. WorkerThreadPool::GroupID WorkerThreadPool::_add_group_task(const Callable &p_callable, void (*p_func)(void *, uint32_t), void *p_userdata, BaseTemplateUserdata *p_template_userdata, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  246. ERR_FAIL_COND_V(p_elements < 0, INVALID_TASK_ID);
  247. if (p_tasks < 0) {
  248. p_tasks = threads.size();
  249. }
  250. task_mutex.lock();
  251. Group *group = group_allocator.alloc();
  252. GroupID id = last_task++;
  253. group->max = p_elements;
  254. group->self = id;
  255. Task **tasks_posted = nullptr;
  256. if (p_elements == 0) {
  257. // Should really not call it with zero Elements, but at least it should work.
  258. group->completed.set_to(true);
  259. group->done_semaphore.post();
  260. group->tasks_used = 0;
  261. p_tasks = 0;
  262. if (p_template_userdata) {
  263. memdelete(p_template_userdata);
  264. }
  265. } else {
  266. group->tasks_used = p_tasks;
  267. tasks_posted = (Task **)alloca(sizeof(Task *) * p_tasks);
  268. for (int i = 0; i < p_tasks; i++) {
  269. Task *task = task_allocator.alloc();
  270. task->native_group_func = p_func;
  271. task->native_func_userdata = p_userdata;
  272. task->description = p_description;
  273. task->group = group;
  274. task->callable = p_callable;
  275. task->template_userdata = p_template_userdata;
  276. tasks_posted[i] = task;
  277. // No task ID is used.
  278. }
  279. }
  280. groups[id] = group;
  281. task_mutex.unlock();
  282. if (!p_high_priority && use_native_low_priority_threads) {
  283. group->low_priority_native_tasks.resize(p_tasks);
  284. }
  285. for (int i = 0; i < p_tasks; i++) {
  286. _post_task(tasks_posted[i], p_high_priority);
  287. if (!p_high_priority && use_native_low_priority_threads) {
  288. group->low_priority_native_tasks[i] = tasks_posted[i];
  289. }
  290. }
  291. return id;
  292. }
  293. 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) {
  294. return _add_group_task(Callable(), p_func, p_userdata, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  295. }
  296. WorkerThreadPool::GroupID WorkerThreadPool::add_group_task(const Callable &p_action, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
  297. return _add_group_task(p_action, nullptr, nullptr, nullptr, p_elements, p_tasks, p_high_priority, p_description);
  298. }
  299. uint32_t WorkerThreadPool::get_group_processed_element_count(GroupID p_group) const {
  300. task_mutex.lock();
  301. const Group *const *groupp = groups.getptr(p_group);
  302. if (!groupp) {
  303. task_mutex.unlock();
  304. ERR_FAIL_V_MSG(0, "Invalid Group ID");
  305. }
  306. uint32_t elements = (*groupp)->completed_index.get();
  307. task_mutex.unlock();
  308. return elements;
  309. }
  310. bool WorkerThreadPool::is_group_task_completed(GroupID p_group) const {
  311. task_mutex.lock();
  312. const Group *const *groupp = groups.getptr(p_group);
  313. if (!groupp) {
  314. task_mutex.unlock();
  315. ERR_FAIL_V_MSG(false, "Invalid Group ID");
  316. }
  317. bool completed = (*groupp)->completed.is_set();
  318. task_mutex.unlock();
  319. return completed;
  320. }
  321. void WorkerThreadPool::wait_for_group_task_completion(GroupID p_group) {
  322. task_mutex.lock();
  323. Group **groupp = groups.getptr(p_group);
  324. task_mutex.unlock();
  325. if (!groupp) {
  326. ERR_FAIL_MSG("Invalid Group ID");
  327. }
  328. Group *group = *groupp;
  329. if (group->low_priority_native_tasks.size() > 0) {
  330. for (Task *task : group->low_priority_native_tasks) {
  331. task->low_priority_thread->wait_to_finish();
  332. native_thread_allocator.free(task->low_priority_thread);
  333. task_mutex.lock();
  334. task_allocator.free(task);
  335. task_mutex.unlock();
  336. }
  337. task_mutex.lock();
  338. group_allocator.free(group);
  339. task_mutex.unlock();
  340. } else {
  341. group->done_semaphore.wait();
  342. 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.
  343. uint32_t finished_users = group->finished.increment(); // fetch happens before inc, so increment later.
  344. if (finished_users == max_users) {
  345. // All tasks using this group are gone (finished before the group), so clear the group too.
  346. task_mutex.lock();
  347. group_allocator.free(group);
  348. task_mutex.unlock();
  349. }
  350. }
  351. task_mutex.lock(); // This mutex is needed when Physics 2D and/or 3D is selected to run on a separate thread.
  352. groups.erase(p_group);
  353. task_mutex.unlock();
  354. }
  355. void WorkerThreadPool::init(int p_thread_count, bool p_use_native_threads_low_priority, float p_low_priority_task_ratio) {
  356. ERR_FAIL_COND(threads.size() > 0);
  357. if (p_thread_count < 0) {
  358. p_thread_count = OS::get_singleton()->get_default_thread_pool_size();
  359. }
  360. if (p_use_native_threads_low_priority) {
  361. max_low_priority_threads = 0;
  362. } else {
  363. max_low_priority_threads = CLAMP(p_thread_count * p_low_priority_task_ratio, 1, p_thread_count);
  364. }
  365. use_native_low_priority_threads = p_use_native_threads_low_priority;
  366. threads.resize(p_thread_count);
  367. for (uint32_t i = 0; i < threads.size(); i++) {
  368. threads[i].index = i;
  369. threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i]);
  370. thread_ids.insert(threads[i].thread.get_id(), i);
  371. }
  372. }
  373. void WorkerThreadPool::finish() {
  374. if (threads.size() == 0) {
  375. return;
  376. }
  377. task_mutex.lock();
  378. SelfList<Task> *E = low_priority_task_queue.first();
  379. while (E) {
  380. print_error("Task waiting was never re-claimed: " + E->self()->description);
  381. E = E->next();
  382. }
  383. task_mutex.unlock();
  384. exit_threads.set_to(true);
  385. for (uint32_t i = 0; i < threads.size(); i++) {
  386. task_available_semaphore.post();
  387. }
  388. for (ThreadData &data : threads) {
  389. data.thread.wait_to_finish();
  390. }
  391. threads.clear();
  392. }
  393. void WorkerThreadPool::_bind_methods() {
  394. ClassDB::bind_method(D_METHOD("add_task", "action", "high_priority", "description"), &WorkerThreadPool::add_task, DEFVAL(false), DEFVAL(String()));
  395. ClassDB::bind_method(D_METHOD("is_task_completed", "task_id"), &WorkerThreadPool::is_task_completed);
  396. ClassDB::bind_method(D_METHOD("wait_for_task_completion", "task_id"), &WorkerThreadPool::wait_for_task_completion);
  397. 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()));
  398. ClassDB::bind_method(D_METHOD("is_group_task_completed", "group_id"), &WorkerThreadPool::is_group_task_completed);
  399. ClassDB::bind_method(D_METHOD("get_group_processed_element_count", "group_id"), &WorkerThreadPool::get_group_processed_element_count);
  400. ClassDB::bind_method(D_METHOD("wait_for_group_task_completion", "group_id"), &WorkerThreadPool::wait_for_group_task_completion);
  401. }
  402. WorkerThreadPool::WorkerThreadPool() {
  403. singleton = this;
  404. }
  405. WorkerThreadPool::~WorkerThreadPool() {
  406. finish();
  407. }