command_queue_mt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/os/memory.h"
  33. #include "core/os/mutex.h"
  34. #include "core/os/semaphore.h"
  35. #include "core/string/print_string.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) class P##N
  203. #define PARAM_DECL(N) typename GetSimpleTypeT<P##N>::type_t p##N
  204. #define DECL_CMD(N) \
  205. template <class T, class 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 <class T, class M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) class 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 <class T, class 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 <class T, class 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. CMD_TYPE(N) *cmd = allocate_and_lock<CMD_TYPE(N)>(); \
  242. cmd->instance = p_instance; \
  243. cmd->method = p_method; \
  244. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  245. unlock(); \
  246. if (sync) \
  247. sync->post(); \
  248. }
  249. #define CMD_RET_TYPE(N) CommandRet##N<T, M, COMMA_SEP_LIST(TYPE_ARG, N) COMMA(N) R>
  250. #define DECL_PUSH_AND_RET(N) \
  251. template <class T, class M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) class R> \
  252. void push_and_ret(T *p_instance, M p_method, COMMA_SEP_LIST(PARAM, N) COMMA(N) R *r_ret) { \
  253. SyncSemaphore *ss = _alloc_sync_sem(); \
  254. CMD_RET_TYPE(N) *cmd = allocate_and_lock<CMD_RET_TYPE(N)>(); \
  255. cmd->instance = p_instance; \
  256. cmd->method = p_method; \
  257. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  258. cmd->ret = r_ret; \
  259. cmd->sync_sem = ss; \
  260. unlock(); \
  261. if (sync) \
  262. sync->post(); \
  263. ss->sem.wait(); \
  264. ss->in_use = false; \
  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 <class T, class 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. SyncSemaphore *ss = _alloc_sync_sem(); \
  271. CMD_SYNC_TYPE(N) *cmd = allocate_and_lock<CMD_SYNC_TYPE(N)>(); \
  272. cmd->instance = p_instance; \
  273. cmd->method = p_method; \
  274. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  275. cmd->sync_sem = ss; \
  276. unlock(); \
  277. if (sync) \
  278. sync->post(); \
  279. ss->sem.wait(); \
  280. ss->in_use = false; \
  281. }
  282. #define MAX_CMD_PARAMS 15
  283. class CommandQueueMT {
  284. struct SyncSemaphore {
  285. Semaphore sem;
  286. bool in_use = false;
  287. };
  288. struct CommandBase {
  289. virtual void call() = 0;
  290. virtual void post() {}
  291. virtual ~CommandBase() {}
  292. };
  293. struct SyncCommand : public CommandBase {
  294. SyncSemaphore *sync_sem = nullptr;
  295. virtual void post() override {
  296. sync_sem->sem.post();
  297. }
  298. };
  299. DECL_CMD(0)
  300. SPACE_SEP_LIST(DECL_CMD, 15)
  301. // Commands that return.
  302. DECL_CMD_RET(0)
  303. SPACE_SEP_LIST(DECL_CMD_RET, 15)
  304. /* commands that don't return but sync */
  305. DECL_CMD_SYNC(0)
  306. SPACE_SEP_LIST(DECL_CMD_SYNC, 15)
  307. /***** BASE *******/
  308. enum {
  309. DEFAULT_COMMAND_MEM_SIZE_KB = 256,
  310. SYNC_SEMAPHORES = 8
  311. };
  312. LocalVector<uint8_t> command_mem;
  313. SyncSemaphore sync_sems[SYNC_SEMAPHORES];
  314. Mutex mutex;
  315. Semaphore *sync = nullptr;
  316. template <class T>
  317. T *allocate() {
  318. // alloc size is size+T+safeguard
  319. uint32_t alloc_size = ((sizeof(T) + 8 - 1) & ~(8 - 1));
  320. uint64_t size = command_mem.size();
  321. command_mem.resize(size + alloc_size + 8);
  322. *(uint64_t *)&command_mem[size] = alloc_size;
  323. T *cmd = memnew_placement(&command_mem[size + 8], T);
  324. return cmd;
  325. }
  326. template <class T>
  327. T *allocate_and_lock() {
  328. lock();
  329. T *ret = allocate<T>();
  330. return ret;
  331. }
  332. void _flush() {
  333. lock();
  334. uint64_t read_ptr = 0;
  335. uint64_t limit = command_mem.size();
  336. while (read_ptr < limit) {
  337. uint64_t size = *(uint64_t *)&command_mem[read_ptr];
  338. read_ptr += 8;
  339. CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[read_ptr]);
  340. cmd->call(); //execute the function
  341. cmd->post(); //release in case it needs sync/ret
  342. cmd->~CommandBase(); //should be done, so erase the command
  343. read_ptr += size;
  344. }
  345. command_mem.clear();
  346. unlock();
  347. }
  348. void lock();
  349. void unlock();
  350. void wait_for_flush();
  351. SyncSemaphore *_alloc_sync_sem();
  352. public:
  353. /* NORMAL PUSH COMMANDS */
  354. DECL_PUSH(0)
  355. SPACE_SEP_LIST(DECL_PUSH, 15)
  356. /* PUSH AND RET COMMANDS */
  357. DECL_PUSH_AND_RET(0)
  358. SPACE_SEP_LIST(DECL_PUSH_AND_RET, 15)
  359. /* PUSH AND RET SYNC COMMANDS*/
  360. DECL_PUSH_AND_SYNC(0)
  361. SPACE_SEP_LIST(DECL_PUSH_AND_SYNC, 15)
  362. _FORCE_INLINE_ void flush_if_pending() {
  363. if (unlikely(command_mem.size() > 0)) {
  364. _flush();
  365. }
  366. }
  367. void flush_all() {
  368. _flush();
  369. }
  370. void wait_and_flush() {
  371. ERR_FAIL_COND(!sync);
  372. sync->wait();
  373. _flush();
  374. }
  375. CommandQueueMT(bool p_sync);
  376. ~CommandQueueMT();
  377. };
  378. #undef ARG
  379. #undef PARAM
  380. #undef TYPE_PARAM
  381. #undef PARAM_DECL
  382. #undef DECL_CMD
  383. #undef DECL_CMD_RET
  384. #undef DECL_CMD_SYNC
  385. #undef TYPE_ARG
  386. #undef CMD_TYPE
  387. #undef CMD_ASSIGN_PARAM
  388. #undef DECL_PUSH
  389. #undef CMD_RET_TYPE
  390. #undef DECL_PUSH_AND_RET
  391. #undef CMD_SYNC_TYPE
  392. #undef DECL_CMD_SYNC
  393. #endif // COMMAND_QUEUE_MT_H