command_queue_mt.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*************************************************************************/
  2. /* command_queue_mt.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #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/simple_type.h"
  36. #include "core/typedefs.h"
  37. #define COMMA(N) _COMMA_##N
  38. #define _COMMA_0
  39. #define _COMMA_1 ,
  40. #define _COMMA_2 ,
  41. #define _COMMA_3 ,
  42. #define _COMMA_4 ,
  43. #define _COMMA_5 ,
  44. #define _COMMA_6 ,
  45. #define _COMMA_7 ,
  46. #define _COMMA_8 ,
  47. #define _COMMA_9 ,
  48. #define _COMMA_10 ,
  49. #define _COMMA_11 ,
  50. #define _COMMA_12 ,
  51. #define _COMMA_13 ,
  52. #define _COMMA_14 ,
  53. #define _COMMA_15 ,
  54. // 1-based comma separated list of ITEMs
  55. #define COMMA_SEP_LIST(ITEM, LENGTH) _COMMA_SEP_LIST_##LENGTH(ITEM)
  56. #define _COMMA_SEP_LIST_15(ITEM) \
  57. _COMMA_SEP_LIST_14(ITEM) \
  58. , ITEM(15)
  59. #define _COMMA_SEP_LIST_14(ITEM) \
  60. _COMMA_SEP_LIST_13(ITEM) \
  61. , ITEM(14)
  62. #define _COMMA_SEP_LIST_13(ITEM) \
  63. _COMMA_SEP_LIST_12(ITEM) \
  64. , ITEM(13)
  65. #define _COMMA_SEP_LIST_12(ITEM) \
  66. _COMMA_SEP_LIST_11(ITEM) \
  67. , ITEM(12)
  68. #define _COMMA_SEP_LIST_11(ITEM) \
  69. _COMMA_SEP_LIST_10(ITEM) \
  70. , ITEM(11)
  71. #define _COMMA_SEP_LIST_10(ITEM) \
  72. _COMMA_SEP_LIST_9(ITEM) \
  73. , ITEM(10)
  74. #define _COMMA_SEP_LIST_9(ITEM) \
  75. _COMMA_SEP_LIST_8(ITEM) \
  76. , ITEM(9)
  77. #define _COMMA_SEP_LIST_8(ITEM) \
  78. _COMMA_SEP_LIST_7(ITEM) \
  79. , ITEM(8)
  80. #define _COMMA_SEP_LIST_7(ITEM) \
  81. _COMMA_SEP_LIST_6(ITEM) \
  82. , ITEM(7)
  83. #define _COMMA_SEP_LIST_6(ITEM) \
  84. _COMMA_SEP_LIST_5(ITEM) \
  85. , ITEM(6)
  86. #define _COMMA_SEP_LIST_5(ITEM) \
  87. _COMMA_SEP_LIST_4(ITEM) \
  88. , ITEM(5)
  89. #define _COMMA_SEP_LIST_4(ITEM) \
  90. _COMMA_SEP_LIST_3(ITEM) \
  91. , ITEM(4)
  92. #define _COMMA_SEP_LIST_3(ITEM) \
  93. _COMMA_SEP_LIST_2(ITEM) \
  94. , ITEM(3)
  95. #define _COMMA_SEP_LIST_2(ITEM) \
  96. _COMMA_SEP_LIST_1(ITEM) \
  97. , ITEM(2)
  98. #define _COMMA_SEP_LIST_1(ITEM) \
  99. _COMMA_SEP_LIST_0(ITEM) \
  100. ITEM(1)
  101. #define _COMMA_SEP_LIST_0(ITEM)
  102. // 1-based semicolon separated list of ITEMs
  103. #define SEMIC_SEP_LIST(ITEM, LENGTH) _SEMIC_SEP_LIST_##LENGTH(ITEM)
  104. #define _SEMIC_SEP_LIST_15(ITEM) \
  105. _SEMIC_SEP_LIST_14(ITEM); \
  106. ITEM(15)
  107. #define _SEMIC_SEP_LIST_14(ITEM) \
  108. _SEMIC_SEP_LIST_13(ITEM); \
  109. ITEM(14)
  110. #define _SEMIC_SEP_LIST_13(ITEM) \
  111. _SEMIC_SEP_LIST_12(ITEM); \
  112. ITEM(13)
  113. #define _SEMIC_SEP_LIST_12(ITEM) \
  114. _SEMIC_SEP_LIST_11(ITEM); \
  115. ITEM(12)
  116. #define _SEMIC_SEP_LIST_11(ITEM) \
  117. _SEMIC_SEP_LIST_10(ITEM); \
  118. ITEM(11)
  119. #define _SEMIC_SEP_LIST_10(ITEM) \
  120. _SEMIC_SEP_LIST_9(ITEM); \
  121. ITEM(10)
  122. #define _SEMIC_SEP_LIST_9(ITEM) \
  123. _SEMIC_SEP_LIST_8(ITEM); \
  124. ITEM(9)
  125. #define _SEMIC_SEP_LIST_8(ITEM) \
  126. _SEMIC_SEP_LIST_7(ITEM); \
  127. ITEM(8)
  128. #define _SEMIC_SEP_LIST_7(ITEM) \
  129. _SEMIC_SEP_LIST_6(ITEM); \
  130. ITEM(7)
  131. #define _SEMIC_SEP_LIST_6(ITEM) \
  132. _SEMIC_SEP_LIST_5(ITEM); \
  133. ITEM(6)
  134. #define _SEMIC_SEP_LIST_5(ITEM) \
  135. _SEMIC_SEP_LIST_4(ITEM); \
  136. ITEM(5)
  137. #define _SEMIC_SEP_LIST_4(ITEM) \
  138. _SEMIC_SEP_LIST_3(ITEM); \
  139. ITEM(4)
  140. #define _SEMIC_SEP_LIST_3(ITEM) \
  141. _SEMIC_SEP_LIST_2(ITEM); \
  142. ITEM(3)
  143. #define _SEMIC_SEP_LIST_2(ITEM) \
  144. _SEMIC_SEP_LIST_1(ITEM); \
  145. ITEM(2)
  146. #define _SEMIC_SEP_LIST_1(ITEM) \
  147. _SEMIC_SEP_LIST_0(ITEM) \
  148. ITEM(1)
  149. #define _SEMIC_SEP_LIST_0(ITEM)
  150. // 1-based space separated list of ITEMs
  151. #define SPACE_SEP_LIST(ITEM, LENGTH) _SPACE_SEP_LIST_##LENGTH(ITEM)
  152. #define _SPACE_SEP_LIST_15(ITEM) \
  153. _SPACE_SEP_LIST_14(ITEM) \
  154. ITEM(15)
  155. #define _SPACE_SEP_LIST_14(ITEM) \
  156. _SPACE_SEP_LIST_13(ITEM) \
  157. ITEM(14)
  158. #define _SPACE_SEP_LIST_13(ITEM) \
  159. _SPACE_SEP_LIST_12(ITEM) \
  160. ITEM(13)
  161. #define _SPACE_SEP_LIST_12(ITEM) \
  162. _SPACE_SEP_LIST_11(ITEM) \
  163. ITEM(12)
  164. #define _SPACE_SEP_LIST_11(ITEM) \
  165. _SPACE_SEP_LIST_10(ITEM) \
  166. ITEM(11)
  167. #define _SPACE_SEP_LIST_10(ITEM) \
  168. _SPACE_SEP_LIST_9(ITEM) \
  169. ITEM(10)
  170. #define _SPACE_SEP_LIST_9(ITEM) \
  171. _SPACE_SEP_LIST_8(ITEM) \
  172. ITEM(9)
  173. #define _SPACE_SEP_LIST_8(ITEM) \
  174. _SPACE_SEP_LIST_7(ITEM) \
  175. ITEM(8)
  176. #define _SPACE_SEP_LIST_7(ITEM) \
  177. _SPACE_SEP_LIST_6(ITEM) \
  178. ITEM(7)
  179. #define _SPACE_SEP_LIST_6(ITEM) \
  180. _SPACE_SEP_LIST_5(ITEM) \
  181. ITEM(6)
  182. #define _SPACE_SEP_LIST_5(ITEM) \
  183. _SPACE_SEP_LIST_4(ITEM) \
  184. ITEM(5)
  185. #define _SPACE_SEP_LIST_4(ITEM) \
  186. _SPACE_SEP_LIST_3(ITEM) \
  187. ITEM(4)
  188. #define _SPACE_SEP_LIST_3(ITEM) \
  189. _SPACE_SEP_LIST_2(ITEM) \
  190. ITEM(3)
  191. #define _SPACE_SEP_LIST_2(ITEM) \
  192. _SPACE_SEP_LIST_1(ITEM) \
  193. ITEM(2)
  194. #define _SPACE_SEP_LIST_1(ITEM) \
  195. _SPACE_SEP_LIST_0(ITEM) \
  196. ITEM(1)
  197. #define _SPACE_SEP_LIST_0(ITEM)
  198. #define ARG(N) p##N
  199. #define PARAM(N) P##N p##N
  200. #define TYPE_PARAM(N) class P##N
  201. #define PARAM_DECL(N) typename GetSimpleTypeT<P##N>::type_t p##N
  202. #define DECL_CMD(N) \
  203. template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  204. struct Command##N : public CommandBase { \
  205. T *instance; \
  206. M method; \
  207. SEMIC_SEP_LIST(PARAM_DECL, N); \
  208. virtual void call() { \
  209. (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  210. } \
  211. };
  212. #define DECL_CMD_RET(N) \
  213. template <class T, class M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) class R> \
  214. struct CommandRet##N : public SyncCommand { \
  215. R *ret; \
  216. T *instance; \
  217. M method; \
  218. SEMIC_SEP_LIST(PARAM_DECL, N); \
  219. virtual void call() { \
  220. *ret = (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  221. } \
  222. };
  223. #define DECL_CMD_SYNC(N) \
  224. template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  225. struct CommandSync##N : public SyncCommand { \
  226. T *instance; \
  227. M method; \
  228. SEMIC_SEP_LIST(PARAM_DECL, N); \
  229. virtual void call() { \
  230. (instance->*method)(COMMA_SEP_LIST(ARG, N)); \
  231. } \
  232. };
  233. #define TYPE_ARG(N) P##N
  234. #define CMD_TYPE(N) Command##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
  235. #define CMD_ASSIGN_PARAM(N) cmd->p##N = p##N
  236. #define DECL_PUSH(N) \
  237. template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  238. void push(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
  239. CMD_TYPE(N) *cmd = allocate_and_lock<CMD_TYPE(N)>(); \
  240. cmd->instance = p_instance; \
  241. cmd->method = p_method; \
  242. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  243. unlock(); \
  244. if (sync) \
  245. sync->post(); \
  246. }
  247. #define CMD_RET_TYPE(N) CommandRet##N<T, M, COMMA_SEP_LIST(TYPE_ARG, N) COMMA(N) R>
  248. #define DECL_PUSH_AND_RET(N) \
  249. template <class T, class M, COMMA_SEP_LIST(TYPE_PARAM, N) COMMA(N) class R> \
  250. void push_and_ret(T *p_instance, M p_method, COMMA_SEP_LIST(PARAM, N) COMMA(N) R *r_ret) { \
  251. SyncSemaphore *ss = _alloc_sync_sem(); \
  252. CMD_RET_TYPE(N) *cmd = allocate_and_lock<CMD_RET_TYPE(N)>(); \
  253. cmd->instance = p_instance; \
  254. cmd->method = p_method; \
  255. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  256. cmd->ret = r_ret; \
  257. cmd->sync_sem = ss; \
  258. unlock(); \
  259. if (sync) \
  260. sync->post(); \
  261. ss->sem.wait(); \
  262. ss->in_use = false; \
  263. }
  264. #define CMD_SYNC_TYPE(N) CommandSync##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
  265. #define DECL_PUSH_AND_SYNC(N) \
  266. template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  267. void push_and_sync(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
  268. SyncSemaphore *ss = _alloc_sync_sem(); \
  269. CMD_SYNC_TYPE(N) *cmd = allocate_and_lock<CMD_SYNC_TYPE(N)>(); \
  270. cmd->instance = p_instance; \
  271. cmd->method = p_method; \
  272. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  273. cmd->sync_sem = ss; \
  274. unlock(); \
  275. if (sync) \
  276. sync->post(); \
  277. ss->sem.wait(); \
  278. ss->in_use = false; \
  279. }
  280. #define MAX_CMD_PARAMS 15
  281. class CommandQueueMT {
  282. struct SyncSemaphore {
  283. Semaphore sem;
  284. bool in_use = false;
  285. };
  286. struct CommandBase {
  287. virtual void call() = 0;
  288. virtual void post() {}
  289. virtual ~CommandBase() {}
  290. };
  291. struct SyncCommand : public CommandBase {
  292. SyncSemaphore *sync_sem;
  293. virtual void post() {
  294. sync_sem->sem.post();
  295. }
  296. };
  297. DECL_CMD(0)
  298. SPACE_SEP_LIST(DECL_CMD, 15)
  299. /* comands that return */
  300. DECL_CMD_RET(0)
  301. SPACE_SEP_LIST(DECL_CMD_RET, 15)
  302. /* commands that don't return but sync */
  303. DECL_CMD_SYNC(0)
  304. SPACE_SEP_LIST(DECL_CMD_SYNC, 15)
  305. /***** BASE *******/
  306. enum {
  307. COMMAND_MEM_SIZE_KB = 256,
  308. COMMAND_MEM_SIZE = COMMAND_MEM_SIZE_KB * 1024,
  309. SYNC_SEMAPHORES = 8
  310. };
  311. uint8_t *command_mem = (uint8_t *)memalloc(COMMAND_MEM_SIZE);
  312. uint32_t read_ptr = 0;
  313. uint32_t write_ptr = 0;
  314. uint32_t dealloc_ptr = 0;
  315. SyncSemaphore sync_sems[SYNC_SEMAPHORES];
  316. Mutex mutex;
  317. Semaphore *sync = nullptr;
  318. template <class T>
  319. T *allocate() {
  320. // alloc size is size+T+safeguard
  321. uint32_t alloc_size = ((sizeof(T) + 8 - 1) & ~(8 - 1)) + 8;
  322. tryagain:
  323. if (write_ptr < dealloc_ptr) {
  324. // behind dealloc_ptr, check that there is room
  325. if ((dealloc_ptr - write_ptr) <= alloc_size) {
  326. // There is no more room, try to deallocate something
  327. if (dealloc_one()) {
  328. goto tryagain;
  329. }
  330. return nullptr;
  331. }
  332. } else {
  333. // ahead of dealloc_ptr, check that there is room
  334. if ((COMMAND_MEM_SIZE - write_ptr) < alloc_size + sizeof(uint32_t)) {
  335. // no room at the end, wrap down;
  336. if (dealloc_ptr == 0) { // don't want write_ptr to become dealloc_ptr
  337. // There is no more room, try to deallocate something
  338. if (dealloc_one()) {
  339. goto tryagain;
  340. }
  341. return nullptr;
  342. }
  343. // if this happens, it's a bug
  344. ERR_FAIL_COND_V((COMMAND_MEM_SIZE - write_ptr) < 8, nullptr);
  345. // zero means, wrap to beginning
  346. uint32_t *p = (uint32_t *)&command_mem[write_ptr];
  347. *p = 0;
  348. write_ptr = 0;
  349. goto tryagain;
  350. }
  351. }
  352. // Allocate the size and the 'in use' bit.
  353. // First bit used to mark if command is still in use (1)
  354. // or if it has been destroyed and can be deallocated (0).
  355. uint32_t size = (sizeof(T) + 8 - 1) & ~(8 - 1);
  356. uint32_t *p = (uint32_t *)&command_mem[write_ptr];
  357. *p = (size << 1) | 1;
  358. write_ptr += 8;
  359. // allocate the command
  360. T *cmd = memnew_placement(&command_mem[write_ptr], T);
  361. write_ptr += size;
  362. return cmd;
  363. }
  364. template <class T>
  365. T *allocate_and_lock() {
  366. lock();
  367. T *ret;
  368. while ((ret = allocate<T>()) == nullptr) {
  369. unlock();
  370. // sleep a little until fetch happened and some room is made
  371. wait_for_flush();
  372. lock();
  373. }
  374. return ret;
  375. }
  376. bool flush_one(bool p_lock = true) {
  377. if (p_lock) {
  378. lock();
  379. }
  380. tryagain:
  381. // tried to read an empty queue
  382. if (read_ptr == write_ptr) {
  383. if (p_lock) {
  384. unlock();
  385. }
  386. return false;
  387. }
  388. uint32_t size_ptr = read_ptr;
  389. uint32_t size = *(uint32_t *)&command_mem[read_ptr] >> 1;
  390. if (size == 0) {
  391. //end of ringbuffer, wrap
  392. read_ptr = 0;
  393. goto tryagain;
  394. }
  395. read_ptr += 8;
  396. CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[read_ptr]);
  397. read_ptr += size;
  398. if (p_lock) {
  399. unlock();
  400. }
  401. cmd->call();
  402. if (p_lock) {
  403. lock();
  404. }
  405. cmd->post();
  406. cmd->~CommandBase();
  407. *(uint32_t *)&command_mem[size_ptr] &= ~1;
  408. if (p_lock) {
  409. unlock();
  410. }
  411. return true;
  412. }
  413. void lock();
  414. void unlock();
  415. void wait_for_flush();
  416. SyncSemaphore *_alloc_sync_sem();
  417. bool dealloc_one();
  418. public:
  419. /* NORMAL PUSH COMMANDS */
  420. DECL_PUSH(0)
  421. SPACE_SEP_LIST(DECL_PUSH, 15)
  422. /* PUSH AND RET COMMANDS */
  423. DECL_PUSH_AND_RET(0)
  424. SPACE_SEP_LIST(DECL_PUSH_AND_RET, 15)
  425. /* PUSH AND RET SYNC COMMANDS*/
  426. DECL_PUSH_AND_SYNC(0)
  427. SPACE_SEP_LIST(DECL_PUSH_AND_SYNC, 15)
  428. void wait_and_flush_one() {
  429. ERR_FAIL_COND(!sync);
  430. sync->wait();
  431. flush_one();
  432. }
  433. void flush_all() {
  434. //ERR_FAIL_COND(sync);
  435. lock();
  436. while (flush_one(false)) {
  437. }
  438. unlock();
  439. }
  440. CommandQueueMT(bool p_sync);
  441. ~CommandQueueMT();
  442. };
  443. #undef ARG
  444. #undef PARAM
  445. #undef TYPE_PARAM
  446. #undef PARAM_DECL
  447. #undef DECL_CMD
  448. #undef DECL_CMD_RET
  449. #undef DECL_CMD_SYNC
  450. #undef TYPE_ARG
  451. #undef CMD_TYPE
  452. #undef CMD_ASSIGN_PARAM
  453. #undef DECL_PUSH
  454. #undef CMD_RET_TYPE
  455. #undef DECL_PUSH_AND_RET
  456. #undef CMD_SYNC_TYPE
  457. #undef DECL_CMD_SYNC
  458. #endif // COMMAND_QUEUE_MT_H