command_queue_mt.h 15 KB

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