command_queue_mt.h 15 KB

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