command_queue_mt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <reduzio@gmail.com>
  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. }
  244. #define CMD_SYNC_TYPE(N) CommandSync##N<T, M COMMA(N) COMMA_SEP_LIST(TYPE_ARG, N)>
  245. #define DECL_PUSH_AND_SYNC(N) \
  246. template <class T, class M COMMA(N) COMMA_SEP_LIST(TYPE_PARAM, N)> \
  247. void push_and_sync(T *p_instance, M p_method COMMA(N) COMMA_SEP_LIST(PARAM, N)) { \
  248. SyncSemaphore *ss = _alloc_sync_sem(); \
  249. CMD_SYNC_TYPE(N) *cmd = allocate_and_lock<CMD_SYNC_TYPE(N)>(); \
  250. cmd->instance = p_instance; \
  251. cmd->method = p_method; \
  252. SEMIC_SEP_LIST(CMD_ASSIGN_PARAM, N); \
  253. cmd->sync_sem = ss; \
  254. unlock(); \
  255. if (sync) sync->post(); \
  256. ss->sem->wait(); \
  257. }
  258. #define MAX_CMD_PARAMS 13
  259. class CommandQueueMT {
  260. struct SyncSemaphore {
  261. Semaphore *sem;
  262. bool in_use;
  263. };
  264. struct CommandBase {
  265. virtual void call() = 0;
  266. virtual void post(){};
  267. virtual ~CommandBase(){};
  268. };
  269. struct SyncCommand : public CommandBase {
  270. SyncSemaphore *sync_sem;
  271. virtual void post() {
  272. sync_sem->sem->post();
  273. sync_sem->in_use = false;
  274. }
  275. };
  276. DECL_CMD(0)
  277. SPACE_SEP_LIST(DECL_CMD, 13)
  278. /* comands that return */
  279. DECL_CMD_RET(0)
  280. SPACE_SEP_LIST(DECL_CMD_RET, 13)
  281. /* commands that don't return but sync */
  282. DECL_CMD_SYNC(0)
  283. SPACE_SEP_LIST(DECL_CMD_SYNC, 13)
  284. /***** BASE *******/
  285. enum {
  286. COMMAND_MEM_SIZE_KB = 256,
  287. COMMAND_MEM_SIZE = COMMAND_MEM_SIZE_KB * 1024,
  288. SYNC_SEMAPHORES = 8
  289. };
  290. uint8_t command_mem[COMMAND_MEM_SIZE];
  291. uint32_t read_ptr;
  292. uint32_t write_ptr;
  293. uint32_t dealloc_ptr;
  294. SyncSemaphore sync_sems[SYNC_SEMAPHORES];
  295. Mutex *mutex;
  296. Semaphore *sync;
  297. template <class T>
  298. T *allocate() {
  299. // alloc size is size+T+safeguard
  300. uint32_t alloc_size = sizeof(T) + sizeof(uint32_t);
  301. tryagain:
  302. if (write_ptr < dealloc_ptr) {
  303. // behind dealloc_ptr, check that there is room
  304. if ((dealloc_ptr - write_ptr) <= alloc_size) {
  305. // There is no more room, try to deallocate something
  306. if (dealloc_one()) {
  307. goto tryagain;
  308. }
  309. return NULL;
  310. }
  311. } else if (write_ptr >= dealloc_ptr) {
  312. // ahead of dealloc_ptr, check that there is room
  313. if ((COMMAND_MEM_SIZE - write_ptr) < alloc_size + sizeof(uint32_t)) {
  314. // no room at the end, wrap down;
  315. if (dealloc_ptr == 0) { // don't want write_ptr to become dealloc_ptr
  316. // There is no more room, try to deallocate something
  317. if (dealloc_one()) {
  318. goto tryagain;
  319. }
  320. return NULL;
  321. }
  322. // if this happens, it's a bug
  323. ERR_FAIL_COND_V((COMMAND_MEM_SIZE - write_ptr) < sizeof(uint32_t), NULL);
  324. // zero means, wrap to beginning
  325. uint32_t *p = (uint32_t *)&command_mem[write_ptr];
  326. *p = 0;
  327. write_ptr = 0;
  328. goto tryagain;
  329. }
  330. }
  331. // Allocate the size and the 'in use' bit.
  332. // First bit used to mark if command is still in use (1)
  333. // or if it has been destroyed and can be deallocated (0).
  334. uint32_t *p = (uint32_t *)&command_mem[write_ptr];
  335. *p = (sizeof(T) << 1) | 1;
  336. write_ptr += sizeof(uint32_t);
  337. // allocate the command
  338. T *cmd = memnew_placement(&command_mem[write_ptr], T);
  339. write_ptr += sizeof(T);
  340. return cmd;
  341. }
  342. template <class T>
  343. T *allocate_and_lock() {
  344. lock();
  345. T *ret;
  346. while ((ret = allocate<T>()) == NULL) {
  347. unlock();
  348. // sleep a little until fetch happened and some room is made
  349. wait_for_flush();
  350. lock();
  351. }
  352. return ret;
  353. }
  354. bool flush_one(bool p_lock = true) {
  355. if (p_lock) lock();
  356. tryagain:
  357. // tried to read an empty queue
  358. if (read_ptr == write_ptr)
  359. return false;
  360. uint32_t size_ptr = read_ptr;
  361. uint32_t size = *(uint32_t *)&command_mem[read_ptr] >> 1;
  362. if (size == 0) {
  363. //end of ringbuffer, wrap
  364. read_ptr = 0;
  365. goto tryagain;
  366. }
  367. read_ptr += sizeof(uint32_t);
  368. CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[read_ptr]);
  369. read_ptr += size;
  370. if (p_lock) unlock();
  371. cmd->call();
  372. if (p_lock) lock();
  373. cmd->post();
  374. cmd->~CommandBase();
  375. *(uint32_t *)&command_mem[size_ptr] &= ~1;
  376. if (p_lock) unlock();
  377. return true;
  378. }
  379. void lock();
  380. void unlock();
  381. void wait_for_flush();
  382. SyncSemaphore *_alloc_sync_sem();
  383. bool dealloc_one();
  384. public:
  385. /* NORMAL PUSH COMMANDS */
  386. DECL_PUSH(0)
  387. SPACE_SEP_LIST(DECL_PUSH, 13)
  388. /* PUSH AND RET COMMANDS */
  389. DECL_PUSH_AND_RET(0)
  390. SPACE_SEP_LIST(DECL_PUSH_AND_RET, 13)
  391. /* PUSH AND RET SYNC COMMANDS*/
  392. DECL_PUSH_AND_SYNC(0)
  393. SPACE_SEP_LIST(DECL_PUSH_AND_SYNC, 13)
  394. void wait_and_flush_one() {
  395. ERR_FAIL_COND(!sync);
  396. sync->wait();
  397. flush_one();
  398. }
  399. void flush_all() {
  400. //ERR_FAIL_COND(sync);
  401. lock();
  402. while (flush_one(false))
  403. ;
  404. unlock();
  405. }
  406. CommandQueueMT(bool p_sync);
  407. ~CommandQueueMT();
  408. };
  409. #undef ARG
  410. #undef PARAM
  411. #undef TYPE_PARAM
  412. #undef PARAM_DECL
  413. #undef DECL_CMD
  414. #undef DECL_CMD_RET
  415. #undef DECL_CMD_SYNC
  416. #undef TYPE_ARG
  417. #undef CMD_TYPE
  418. #undef CMD_ASSIGN_PARAM
  419. #undef DECL_PUSH
  420. #undef CMD_RET_TYPE
  421. #undef DECL_PUSH_AND_RET
  422. #undef CMD_SYNC_TYPE
  423. #undef DECL_CMD_SYNC
  424. #endif