command_queue_mt.h 17 KB

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