command_queue_mt.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/templates/local_vector.h"
  37. #include "core/templates/simple_type.h"
  38. #include "core/typedefs.h"
  39. #define COMMA(N) _COMMA_##N
  40. #define _COMMA_0
  41. #define _COMMA_1 ,
  42. #define _COMMA_2 ,
  43. #define _COMMA_3 ,
  44. #define _COMMA_4 ,
  45. #define _COMMA_5 ,
  46. #define _COMMA_6 ,
  47. #define _COMMA_7 ,
  48. #define _COMMA_8 ,
  49. #define _COMMA_9 ,
  50. #define _COMMA_10 ,
  51. #define _COMMA_11 ,
  52. #define _COMMA_12 ,
  53. #define _COMMA_13 ,
  54. #define _COMMA_14 ,
  55. #define _COMMA_15 ,
  56. // 1-based comma separated list of ITEMs
  57. #define COMMA_SEP_LIST(ITEM, LENGTH) _COMMA_SEP_LIST_##LENGTH(ITEM)
  58. #define _COMMA_SEP_LIST_15(ITEM) \
  59. _COMMA_SEP_LIST_14(ITEM) \
  60. , ITEM(15)
  61. #define _COMMA_SEP_LIST_14(ITEM) \
  62. _COMMA_SEP_LIST_13(ITEM) \
  63. , ITEM(14)
  64. #define _COMMA_SEP_LIST_13(ITEM) \
  65. _COMMA_SEP_LIST_12(ITEM) \
  66. , ITEM(13)
  67. #define _COMMA_SEP_LIST_12(ITEM) \
  68. _COMMA_SEP_LIST_11(ITEM) \
  69. , ITEM(12)
  70. #define _COMMA_SEP_LIST_11(ITEM) \
  71. _COMMA_SEP_LIST_10(ITEM) \
  72. , ITEM(11)
  73. #define _COMMA_SEP_LIST_10(ITEM) \
  74. _COMMA_SEP_LIST_9(ITEM) \
  75. , ITEM(10)
  76. #define _COMMA_SEP_LIST_9(ITEM) \
  77. _COMMA_SEP_LIST_8(ITEM) \
  78. , ITEM(9)
  79. #define _COMMA_SEP_LIST_8(ITEM) \
  80. _COMMA_SEP_LIST_7(ITEM) \
  81. , ITEM(8)
  82. #define _COMMA_SEP_LIST_7(ITEM) \
  83. _COMMA_SEP_LIST_6(ITEM) \
  84. , ITEM(7)
  85. #define _COMMA_SEP_LIST_6(ITEM) \
  86. _COMMA_SEP_LIST_5(ITEM) \
  87. , ITEM(6)
  88. #define _COMMA_SEP_LIST_5(ITEM) \
  89. _COMMA_SEP_LIST_4(ITEM) \
  90. , ITEM(5)
  91. #define _COMMA_SEP_LIST_4(ITEM) \
  92. _COMMA_SEP_LIST_3(ITEM) \
  93. , ITEM(4)
  94. #define _COMMA_SEP_LIST_3(ITEM) \
  95. _COMMA_SEP_LIST_2(ITEM) \
  96. , ITEM(3)
  97. #define _COMMA_SEP_LIST_2(ITEM) \
  98. _COMMA_SEP_LIST_1(ITEM) \
  99. , ITEM(2)
  100. #define _COMMA_SEP_LIST_1(ITEM) \
  101. _COMMA_SEP_LIST_0(ITEM) \
  102. ITEM(1)
  103. #define _COMMA_SEP_LIST_0(ITEM)
  104. // 1-based semicolon separated list of ITEMs
  105. #define SEMIC_SEP_LIST(ITEM, LENGTH) _SEMIC_SEP_LIST_##LENGTH(ITEM)
  106. #define _SEMIC_SEP_LIST_15(ITEM) \
  107. _SEMIC_SEP_LIST_14(ITEM); \
  108. ITEM(15)
  109. #define _SEMIC_SEP_LIST_14(ITEM) \
  110. _SEMIC_SEP_LIST_13(ITEM); \
  111. ITEM(14)
  112. #define _SEMIC_SEP_LIST_13(ITEM) \
  113. _SEMIC_SEP_LIST_12(ITEM); \
  114. ITEM(13)
  115. #define _SEMIC_SEP_LIST_12(ITEM) \
  116. _SEMIC_SEP_LIST_11(ITEM); \
  117. ITEM(12)
  118. #define _SEMIC_SEP_LIST_11(ITEM) \
  119. _SEMIC_SEP_LIST_10(ITEM); \
  120. ITEM(11)
  121. #define _SEMIC_SEP_LIST_10(ITEM) \
  122. _SEMIC_SEP_LIST_9(ITEM); \
  123. ITEM(10)
  124. #define _SEMIC_SEP_LIST_9(ITEM) \
  125. _SEMIC_SEP_LIST_8(ITEM); \
  126. ITEM(9)
  127. #define _SEMIC_SEP_LIST_8(ITEM) \
  128. _SEMIC_SEP_LIST_7(ITEM); \
  129. ITEM(8)
  130. #define _SEMIC_SEP_LIST_7(ITEM) \
  131. _SEMIC_SEP_LIST_6(ITEM); \
  132. ITEM(7)
  133. #define _SEMIC_SEP_LIST_6(ITEM) \
  134. _SEMIC_SEP_LIST_5(ITEM); \
  135. ITEM(6)
  136. #define _SEMIC_SEP_LIST_5(ITEM) \
  137. _SEMIC_SEP_LIST_4(ITEM); \
  138. ITEM(5)
  139. #define _SEMIC_SEP_LIST_4(ITEM) \
  140. _SEMIC_SEP_LIST_3(ITEM); \
  141. ITEM(4)
  142. #define _SEMIC_SEP_LIST_3(ITEM) \
  143. _SEMIC_SEP_LIST_2(ITEM); \
  144. ITEM(3)
  145. #define _SEMIC_SEP_LIST_2(ITEM) \
  146. _SEMIC_SEP_LIST_1(ITEM); \
  147. ITEM(2)
  148. #define _SEMIC_SEP_LIST_1(ITEM) \
  149. _SEMIC_SEP_LIST_0(ITEM) \
  150. ITEM(1)
  151. #define _SEMIC_SEP_LIST_0(ITEM)
  152. // 1-based space separated list of ITEMs
  153. #define SPACE_SEP_LIST(ITEM, LENGTH) _SPACE_SEP_LIST_##LENGTH(ITEM)
  154. #define _SPACE_SEP_LIST_15(ITEM) \
  155. _SPACE_SEP_LIST_14(ITEM) \
  156. ITEM(15)
  157. #define _SPACE_SEP_LIST_14(ITEM) \
  158. _SPACE_SEP_LIST_13(ITEM) \
  159. ITEM(14)
  160. #define _SPACE_SEP_LIST_13(ITEM) \
  161. _SPACE_SEP_LIST_12(ITEM) \
  162. ITEM(13)
  163. #define _SPACE_SEP_LIST_12(ITEM) \
  164. _SPACE_SEP_LIST_11(ITEM) \
  165. ITEM(12)
  166. #define _SPACE_SEP_LIST_11(ITEM) \
  167. _SPACE_SEP_LIST_10(ITEM) \
  168. ITEM(11)
  169. #define _SPACE_SEP_LIST_10(ITEM) \
  170. _SPACE_SEP_LIST_9(ITEM) \
  171. ITEM(10)
  172. #define _SPACE_SEP_LIST_9(ITEM) \
  173. _SPACE_SEP_LIST_8(ITEM) \
  174. ITEM(9)
  175. #define _SPACE_SEP_LIST_8(ITEM) \
  176. _SPACE_SEP_LIST_7(ITEM) \
  177. ITEM(8)
  178. #define _SPACE_SEP_LIST_7(ITEM) \
  179. _SPACE_SEP_LIST_6(ITEM) \
  180. ITEM(7)
  181. #define _SPACE_SEP_LIST_6(ITEM) \
  182. _SPACE_SEP_LIST_5(ITEM) \
  183. ITEM(6)
  184. #define _SPACE_SEP_LIST_5(ITEM) \
  185. _SPACE_SEP_LIST_4(ITEM) \
  186. ITEM(5)
  187. #define _SPACE_SEP_LIST_4(ITEM) \
  188. _SPACE_SEP_LIST_3(ITEM) \
  189. ITEM(4)
  190. #define _SPACE_SEP_LIST_3(ITEM) \
  191. _SPACE_SEP_LIST_2(ITEM) \
  192. ITEM(3)
  193. #define _SPACE_SEP_LIST_2(ITEM) \
  194. _SPACE_SEP_LIST_1(ITEM) \
  195. ITEM(2)
  196. #define _SPACE_SEP_LIST_1(ITEM) \
  197. _SPACE_SEP_LIST_0(ITEM) \
  198. ITEM(1)
  199. #define _SPACE_SEP_LIST_0(ITEM)
  200. #define ARG(N) p##N
  201. #define PARAM(N) P##N p##N
  202. #define TYPE_PARAM(N) typename P##N
  203. #define PARAM_DECL(N) GetSimpleTypeT<P##N> p##N
  204. #define DECL_CMD(N) \
  205. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  206. struct Command##N : public CommandBase { \
  207. T *instance; \
  208. M method; \
  209. SEMIC_SEP_LIST(PARAM_DECL, N); \
  210. virtual void call() override { \
  211. (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  212. } \
  213. };
  214. #define DECL_CMD_RET(N) \
  215. template <typename T, typename M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) typename R> \
  216. struct CommandRet##N : public SyncCommand { \
  217. R *ret; \
  218. T *instance; \
  219. M method; \
  220. SEMIC_SEP_LIST(PARAM_DECL, N); \
  221. virtual void call() override { \
  222. *ret = (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  223. } \
  224. };
  225. #define DECL_CMD_SYNC(N) \
  226. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  227. struct CommandSync##N : public SyncCommand { \
  228. T *instance; \
  229. M method; \
  230. SEMIC_SEP_LIST(PARAM_DECL, N); \
  231. virtual void call() override { \
  232. (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  233. } \
  234. };
  235. #define TYPE_ARG(N) P##N
  236. #define CMD_TYPE(N) Command##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
  237. #define CMD_ASSIGN_PARAM(N) cmd->p##N = p##N
  238. #define DECL_PUSH(N) \
  239. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  240. void push(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
  241. MutexLock mlock(mutex); \
  242. CMD_TYPE(N) *cmd = allocate<CMD_TYPE(N)>(); \
  243. cmd->instance = p_instance; \
  244. cmd->method = p_method; \
  245. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  246. if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
  247. WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
  248. } \
  249. }
  250. #define CMD_RET_TYPE(N) CommandRet##N<T, M, COMMA_SEP_LIST(TYPE_ARG, N) COMMA(N) R>
  251. #define DECL_PUSH_AND_RET(N) \
  252. template <typename T, typename M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) typename R> \
  253. void push_and_ret(T *p_instance, M p_method, COMMA_SEP_LIST(PARAM, N) COMMA(N) R *r_ret) { \
  254. MutexLock mlock(mutex); \
  255. CMD_RET_TYPE(N) *cmd = allocate<CMD_RET_TYPE(N)>(); \
  256. cmd->instance = p_instance; \
  257. cmd->method = p_method; \
  258. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  259. cmd->ret = r_ret; \
  260. if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
  261. WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
  262. } \
  263. sync_tail++; \
  264. _wait_for_sync(mlock); \
  265. }
  266. #define CMD_SYNC_TYPE(N) CommandSync##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
  267. #define DECL_PUSH_AND_SYNC(N) \
  268. template <typename T, typename M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  269. void push_and_sync(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
  270. MutexLock mlock(mutex); \
  271. CMD_SYNC_TYPE(N) *cmd = allocate<CMD_SYNC_TYPE(N)>(); \
  272. cmd->instance = p_instance; \
  273. cmd->method = p_method; \
  274. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  275. if (pump_task_id != WorkerThreadPool::INVALID_TASK_ID) { \
  276. WorkerThreadPool::get_singleton()->notify_yield_over(pump_task_id); \
  277. } \
  278. sync_tail++; \
  279. _wait_for_sync(mlock); \
  280. }
  281. #define MAX_CMD_PARAMS 15
  282. class CommandQueueMT {
  283. struct CommandBase {
  284. bool sync = false;
  285. virtual void call() = 0;
  286. virtual ~CommandBase() = default;
  287. };
  288. struct SyncCommand : public CommandBase {
  289. virtual void call() override {}
  290. SyncCommand() {
  291. sync = true;
  292. }
  293. };
  294. DECL_CMD(0)
  295. SPACE_SEP_LIST(DECL_CMD, 15)
  296. // Commands that return.
  297. DECL_CMD_RET(0)
  298. SPACE_SEP_LIST(DECL_CMD_RET, 15)
  299. /* commands that don't return but sync */
  300. DECL_CMD_SYNC(0)
  301. SPACE_SEP_LIST(DECL_CMD_SYNC, 15)
  302. /***** BASE *******/
  303. static const uint32_t DEFAULT_COMMAND_MEM_SIZE_KB = 64;
  304. BinaryMutex mutex;
  305. LocalVector<uint8_t> command_mem;
  306. ConditionVariable sync_cond_var;
  307. uint32_t sync_head = 0;
  308. uint32_t sync_tail = 0;
  309. uint32_t sync_awaiters = 0;
  310. WorkerThreadPool::TaskID pump_task_id = WorkerThreadPool::INVALID_TASK_ID;
  311. uint64_t flush_read_ptr = 0;
  312. template <typename T>
  313. T *allocate() {
  314. // alloc size is size+T+safeguard
  315. static_assert(sizeof(T) < UINT32_MAX, "Type too large to fit in the command queue.");
  316. uint32_t alloc_size = ((sizeof(T) + 8U - 1U) & ~(8U - 1U));
  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