command_queue_mt.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /**************************************************************************/
  2. /* command_queue_mt.h */
  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. #ifndef COMMAND_QUEUE_MT_H
  31. #define COMMAND_QUEUE_MT_H
  32. #include "core/object/worker_thread_pool.h"
  33. #include "core/os/condition_variable.h"
  34. #include "core/os/memory.h"
  35. #include "core/os/mutex.h"
  36. #include "core/string/print_string.h"
  37. #include "core/templates/local_vector.h"
  38. #include "core/templates/simple_type.h"
  39. #include "core/typedefs.h"
  40. #define COMMA(N) _COMMA_##N
  41. #define _COMMA_0
  42. #define _COMMA_1 ,
  43. #define _COMMA_2 ,
  44. #define _COMMA_3 ,
  45. #define _COMMA_4 ,
  46. #define _COMMA_5 ,
  47. #define _COMMA_6 ,
  48. #define _COMMA_7 ,
  49. #define _COMMA_8 ,
  50. #define _COMMA_9 ,
  51. #define _COMMA_10 ,
  52. #define _COMMA_11 ,
  53. #define _COMMA_12 ,
  54. #define _COMMA_13 ,
  55. #define _COMMA_14 ,
  56. #define _COMMA_15 ,
  57. // 1-based comma separated list of ITEMs
  58. #define COMMA_SEP_LIST(ITEM, LENGTH) _COMMA_SEP_LIST_##LENGTH(ITEM)
  59. #define _COMMA_SEP_LIST_15(ITEM) \
  60. _COMMA_SEP_LIST_14(ITEM) \
  61. , ITEM(15)
  62. #define _COMMA_SEP_LIST_14(ITEM) \
  63. _COMMA_SEP_LIST_13(ITEM) \
  64. , ITEM(14)
  65. #define _COMMA_SEP_LIST_13(ITEM) \
  66. _COMMA_SEP_LIST_12(ITEM) \
  67. , ITEM(13)
  68. #define _COMMA_SEP_LIST_12(ITEM) \
  69. _COMMA_SEP_LIST_11(ITEM) \
  70. , ITEM(12)
  71. #define _COMMA_SEP_LIST_11(ITEM) \
  72. _COMMA_SEP_LIST_10(ITEM) \
  73. , ITEM(11)
  74. #define _COMMA_SEP_LIST_10(ITEM) \
  75. _COMMA_SEP_LIST_9(ITEM) \
  76. , ITEM(10)
  77. #define _COMMA_SEP_LIST_9(ITEM) \
  78. _COMMA_SEP_LIST_8(ITEM) \
  79. , ITEM(9)
  80. #define _COMMA_SEP_LIST_8(ITEM) \
  81. _COMMA_SEP_LIST_7(ITEM) \
  82. , ITEM(8)
  83. #define _COMMA_SEP_LIST_7(ITEM) \
  84. _COMMA_SEP_LIST_6(ITEM) \
  85. , ITEM(7)
  86. #define _COMMA_SEP_LIST_6(ITEM) \
  87. _COMMA_SEP_LIST_5(ITEM) \
  88. , ITEM(6)
  89. #define _COMMA_SEP_LIST_5(ITEM) \
  90. _COMMA_SEP_LIST_4(ITEM) \
  91. , ITEM(5)
  92. #define _COMMA_SEP_LIST_4(ITEM) \
  93. _COMMA_SEP_LIST_3(ITEM) \
  94. , ITEM(4)
  95. #define _COMMA_SEP_LIST_3(ITEM) \
  96. _COMMA_SEP_LIST_2(ITEM) \
  97. , ITEM(3)
  98. #define _COMMA_SEP_LIST_2(ITEM) \
  99. _COMMA_SEP_LIST_1(ITEM) \
  100. , ITEM(2)
  101. #define _COMMA_SEP_LIST_1(ITEM) \
  102. _COMMA_SEP_LIST_0(ITEM) \
  103. ITEM(1)
  104. #define _COMMA_SEP_LIST_0(ITEM)
  105. // 1-based semicolon separated list of ITEMs
  106. #define SEMIC_SEP_LIST(ITEM, LENGTH) _SEMIC_SEP_LIST_##LENGTH(ITEM)
  107. #define _SEMIC_SEP_LIST_15(ITEM) \
  108. _SEMIC_SEP_LIST_14(ITEM); \
  109. ITEM(15)
  110. #define _SEMIC_SEP_LIST_14(ITEM) \
  111. _SEMIC_SEP_LIST_13(ITEM); \
  112. ITEM(14)
  113. #define _SEMIC_SEP_LIST_13(ITEM) \
  114. _SEMIC_SEP_LIST_12(ITEM); \
  115. ITEM(13)
  116. #define _SEMIC_SEP_LIST_12(ITEM) \
  117. _SEMIC_SEP_LIST_11(ITEM); \
  118. ITEM(12)
  119. #define _SEMIC_SEP_LIST_11(ITEM) \
  120. _SEMIC_SEP_LIST_10(ITEM); \
  121. ITEM(11)
  122. #define _SEMIC_SEP_LIST_10(ITEM) \
  123. _SEMIC_SEP_LIST_9(ITEM); \
  124. ITEM(10)
  125. #define _SEMIC_SEP_LIST_9(ITEM) \
  126. _SEMIC_SEP_LIST_8(ITEM); \
  127. ITEM(9)
  128. #define _SEMIC_SEP_LIST_8(ITEM) \
  129. _SEMIC_SEP_LIST_7(ITEM); \
  130. ITEM(8)
  131. #define _SEMIC_SEP_LIST_7(ITEM) \
  132. _SEMIC_SEP_LIST_6(ITEM); \
  133. ITEM(7)
  134. #define _SEMIC_SEP_LIST_6(ITEM) \
  135. _SEMIC_SEP_LIST_5(ITEM); \
  136. ITEM(6)
  137. #define _SEMIC_SEP_LIST_5(ITEM) \
  138. _SEMIC_SEP_LIST_4(ITEM); \
  139. ITEM(5)
  140. #define _SEMIC_SEP_LIST_4(ITEM) \
  141. _SEMIC_SEP_LIST_3(ITEM); \
  142. ITEM(4)
  143. #define _SEMIC_SEP_LIST_3(ITEM) \
  144. _SEMIC_SEP_LIST_2(ITEM); \
  145. ITEM(3)
  146. #define _SEMIC_SEP_LIST_2(ITEM) \
  147. _SEMIC_SEP_LIST_1(ITEM); \
  148. ITEM(2)
  149. #define _SEMIC_SEP_LIST_1(ITEM) \
  150. _SEMIC_SEP_LIST_0(ITEM) \
  151. ITEM(1)
  152. #define _SEMIC_SEP_LIST_0(ITEM)
  153. // 1-based space separated list of ITEMs
  154. #define SPACE_SEP_LIST(ITEM, LENGTH) _SPACE_SEP_LIST_##LENGTH(ITEM)
  155. #define _SPACE_SEP_LIST_15(ITEM) \
  156. _SPACE_SEP_LIST_14(ITEM) \
  157. ITEM(15)
  158. #define _SPACE_SEP_LIST_14(ITEM) \
  159. _SPACE_SEP_LIST_13(ITEM) \
  160. ITEM(14)
  161. #define _SPACE_SEP_LIST_13(ITEM) \
  162. _SPACE_SEP_LIST_12(ITEM) \
  163. ITEM(13)
  164. #define _SPACE_SEP_LIST_12(ITEM) \
  165. _SPACE_SEP_LIST_11(ITEM) \
  166. ITEM(12)
  167. #define _SPACE_SEP_LIST_11(ITEM) \
  168. _SPACE_SEP_LIST_10(ITEM) \
  169. ITEM(11)
  170. #define _SPACE_SEP_LIST_10(ITEM) \
  171. _SPACE_SEP_LIST_9(ITEM) \
  172. ITEM(10)
  173. #define _SPACE_SEP_LIST_9(ITEM) \
  174. _SPACE_SEP_LIST_8(ITEM) \
  175. ITEM(9)
  176. #define _SPACE_SEP_LIST_8(ITEM) \
  177. _SPACE_SEP_LIST_7(ITEM) \
  178. ITEM(8)
  179. #define _SPACE_SEP_LIST_7(ITEM) \
  180. _SPACE_SEP_LIST_6(ITEM) \
  181. ITEM(7)
  182. #define _SPACE_SEP_LIST_6(ITEM) \
  183. _SPACE_SEP_LIST_5(ITEM) \
  184. ITEM(6)
  185. #define _SPACE_SEP_LIST_5(ITEM) \
  186. _SPACE_SEP_LIST_4(ITEM) \
  187. ITEM(5)
  188. #define _SPACE_SEP_LIST_4(ITEM) \
  189. _SPACE_SEP_LIST_3(ITEM) \
  190. ITEM(4)
  191. #define _SPACE_SEP_LIST_3(ITEM) \
  192. _SPACE_SEP_LIST_2(ITEM) \
  193. ITEM(3)
  194. #define _SPACE_SEP_LIST_2(ITEM) \
  195. _SPACE_SEP_LIST_1(ITEM) \
  196. ITEM(2)
  197. #define _SPACE_SEP_LIST_1(ITEM) \
  198. _SPACE_SEP_LIST_0(ITEM) \
  199. ITEM(1)
  200. #define _SPACE_SEP_LIST_0(ITEM)
  201. #define ARG(N) p##N
  202. #define PARAM(N) P##N p##N
  203. #define TYPE_PARAM(N) typename P##N
  204. #define PARAM_DECL(N) GetSimpleTypeT<P##N> p##N
  205. #define DECL_CMD(N) \
  206. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  207. struct Command##N : public CommandBase { \
  208. T *instance; \
  209. M method; \
  210. SEMIC_SEP_LIST(PARAM_DECL, N); \
  211. virtual void call() override { \
  212. (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  213. } \
  214. };
  215. #define DECL_CMD_RET(N) \
  216. template <typename T, typename M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) typename R> \
  217. struct CommandRet##N : public SyncCommand { \
  218. R *ret; \
  219. T *instance; \
  220. M method; \
  221. SEMIC_SEP_LIST(PARAM_DECL, N); \
  222. virtual void call() override { \
  223. *ret = (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  224. } \
  225. };
  226. #define DECL_CMD_SYNC(N) \
  227. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  228. struct CommandSync##N : public SyncCommand { \
  229. T *instance; \
  230. M method; \
  231. SEMIC_SEP_LIST(PARAM_DECL, N); \
  232. virtual void call() override { \
  233. (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  234. } \
  235. };
  236. #define TYPE_ARG(N) P##N
  237. #define CMD_TYPE(N) Command##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
  238. #define CMD_ASSIGN_PARAM(N) cmd->p##N = p##N
  239. #define DECL_PUSH(N) \
  240. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  241. void push(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
  242. MutexLock mlock(mutex); \
  243. CMD_TYPE(N) *cmd = allocate<CMD_TYPE(N)>(); \
  244. cmd->instance = p_instance; \
  245. cmd->method = p_method; \
  246. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  247. if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
  248. WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
  249. } \
  250. }
  251. #define CMD_RET_TYPE(N) CommandRet##N<T, M, COMMA_SEP_LIST(TYPE_ARG, N) COMMA(N) R>
  252. #define DECL_PUSH_AND_RET(N) \
  253. template <typename T, typename M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) typename R> \
  254. void push_and_ret(T *p_instance, M p_method, COMMA_SEP_LIST(PARAM, N) COMMA(N) R *r_ret) { \
  255. MutexLock mlock(mutex); \
  256. CMD_RET_TYPE(N) *cmd = allocate<CMD_RET_TYPE(N)>(); \
  257. cmd->instance = p_instance; \
  258. cmd->method = p_method; \
  259. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  260. cmd->ret = r_ret; \
  261. if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
  262. WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
  263. } \
  264. sync_tail++; \
  265. _wait_for_sync(mlock); \
  266. }
  267. #define CMD_SYNC_TYPE(N) CommandSync##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
  268. #define DECL_PUSH_AND_SYNC(N) \
  269. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  270. void push_and_sync(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
  271. MutexLock mlock(mutex); \
  272. CMD_SYNC_TYPE(N) *cmd = allocate<CMD_SYNC_TYPE(N)>(); \
  273. cmd->instance = p_instance; \
  274. cmd->method = p_method; \
  275. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  276. if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
  277. WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
  278. } \
  279. sync_tail++; \
  280. _wait_for_sync(mlock); \
  281. }
  282. #define MAX_CMD_PARAMS 15
  283. class CommandQueueMT {
  284. struct CommandBase {
  285. bool sync = false;
  286. virtual void call() = 0;
  287. virtual ~CommandBase() = default;
  288. };
  289. struct SyncCommand : public CommandBase {
  290. virtual void call() override {}
  291. SyncCommand() {
  292. sync = true;
  293. }
  294. };
  295. DECL_CMD(0)
  296. SPACE_SEP_LIST(DECL_CMD, 15)
  297. // Commands that return.
  298. DECL_CMD_RET(0)
  299. SPACE_SEP_LIST(DECL_CMD_RET, 15)
  300. /* commands that don't return but sync */
  301. DECL_CMD_SYNC(0)
  302. SPACE_SEP_LIST(DECL_CMD_SYNC, 15)
  303. /***** BASE *******/
  304. static const uint32_t DEFAULT_COMMAND_MEM_SIZE_KB = 64;
  305. BinaryMutex mutex;
  306. LocalVector<uint8_t> command_mem;
  307. ConditionVariable sync_cond_var;
  308. uint32_t sync_head = 0;
  309. uint32_t sync_tail = 0;
  310. uint32_t sync_awaiters = 0;
  311. WorkerThreadPool::TaskID pump_task_id = WorkerThreadPool::INVALID_TASK_ID;
  312. uint64_t flush_read_ptr = 0;
  313. template <typename T>
  314. T *allocate() {
  315. // alloc size is size+T+safeguard
  316. uint32_t alloc_size = ((sizeof(T) + 8 - 1) & ~(8 - 1));
  317. uint64_t size = command_mem.size();
  318. command_mem.resize(size + alloc_size + 8);
  319. *(uint64_t *)&command_mem[size] = alloc_size;
  320. T *cmd = memnew_placement(&command_mem[size + 8], T);
  321. return cmd;
  322. }
  323. _FORCE_INLINE_ void _prevent_sync_wraparound() {
  324. bool safe_to_reset = !sync_awaiters;
  325. bool already_sync_to_latest = sync_head == sync_tail;
  326. if (safe_to_reset && already_sync_to_latest) {
  327. sync_head = 0;
  328. sync_tail = 0;
  329. }
  330. }
  331. void _flush() {
  332. if (unlikely(flush_read_ptr)) {
  333. // Re-entrant call.
  334. return;
  335. }
  336. MutexLock lock(mutex);
  337. while (flush_read_ptr < command_mem.size()) {
  338. uint64_t size = *(uint64_t *)&command_mem[flush_read_ptr];
  339. flush_read_ptr += 8;
  340. CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
  341. uint32_t allowance_id = WorkerThreadPool::thread_enter_unlock_allowance_zone(lock);
  342. cmd->call();
  343. WorkerThreadPool::thread_exit_unlock_allowance_zone(allowance_id);
  344. // Handle potential realloc due to the command and unlock allowance.
  345. cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
  346. if (unlikely(cmd->sync)) {
  347. sync_head++;
  348. lock.~MutexLock(); // Give an opportunity to awaiters right away.
  349. sync_cond_var.notify_all();
  350. new (&lock) MutexLock(mutex);
  351. // Handle potential realloc happened during unlock.
  352. cmd = reinterpret_cast<CommandBase *>(&command_mem[flush_read_ptr]);
  353. }
  354. cmd->~CommandBase();
  355. flush_read_ptr += size;
  356. }
  357. command_mem.clear();
  358. flush_read_ptr = 0;
  359. _prevent_sync_wraparound();
  360. }
  361. _FORCE_INLINE_ void _wait_for_sync(MutexLock<BinaryMutex> &p_lock) {
  362. sync_awaiters++;
  363. uint32_t sync_head_goal = sync_tail;
  364. do {
  365. sync_cond_var.wait(p_lock);
  366. } while (sync_head < sync_head_goal);
  367. sync_awaiters--;
  368. _prevent_sync_wraparound();
  369. }
  370. void _no_op() {}
  371. public:
  372. /* NORMAL PUSH COMMANDS */
  373. DECL_PUSH(0)
  374. SPACE_SEP_LIST(DECL_PUSH, 15)
  375. /* PUSH AND RET COMMANDS */
  376. DECL_PUSH_AND_RET(0)
  377. SPACE_SEP_LIST(DECL_PUSH_AND_RET, 15)
  378. /* PUSH AND RET SYNC COMMANDS*/
  379. DECL_PUSH_AND_SYNC(0)
  380. SPACE_SEP_LIST(DECL_PUSH_AND_SYNC, 15)
  381. _FORCE_INLINE_ void flush_if_pending() {
  382. if (unlikely(command_mem.size() > 0)) {
  383. _flush();
  384. }
  385. }
  386. void flush_all() {
  387. _flush();
  388. }
  389. void sync() {
  390. push_and_sync(this, &CommandQueueMT::_no_op);
  391. }
  392. void wait_and_flush() {
  393. ERR_FAIL_COND(pump_task_id == WorkerThreadPool::INVALID_TASK_ID);
  394. WorkerThreadPool::get_singleton()->wait_for_task_completion(pump_task_id);
  395. _flush();
  396. }
  397. void set_pump_task_id(WorkerThreadPool::TaskID p_task_id) {
  398. MutexLock lock(mutex);
  399. pump_task_id = p_task_id;
  400. }
  401. CommandQueueMT();
  402. ~CommandQueueMT();
  403. };
  404. #undef ARG
  405. #undef PARAM
  406. #undef TYPE_PARAM
  407. #undef PARAM_DECL
  408. #undef DECL_CMD
  409. #undef DECL_CMD_RET
  410. #undef DECL_CMD_SYNC
  411. #undef TYPE_ARG
  412. #undef CMD_TYPE
  413. #undef CMD_ASSIGN_PARAM
  414. #undef DECL_PUSH
  415. #undef CMD_RET_TYPE
  416. #undef DECL_PUSH_AND_RET
  417. #undef CMD_SYNC_TYPE
  418. #undef DECL_CMD_SYNC
  419. #endif // COMMAND_QUEUE_MT_H