command_queue_mt.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. class CommandQueueMT {
  41. struct SyncSemaphore {
  42. Semaphore *sem;
  43. bool in_use;
  44. };
  45. struct CommandBase {
  46. virtual void call() = 0;
  47. virtual ~CommandBase(){};
  48. };
  49. template <class T, class M>
  50. struct Command0 : public CommandBase {
  51. T *instance;
  52. M method;
  53. virtual void call() { (instance->*method)(); }
  54. };
  55. template <class T, class M, class P1>
  56. struct Command1 : public CommandBase {
  57. T *instance;
  58. M method;
  59. typename GetSimpleTypeT<P1>::type_t p1;
  60. virtual void call() { (instance->*method)(p1); }
  61. };
  62. template <class T, class M, class P1, class P2>
  63. struct Command2 : public CommandBase {
  64. T *instance;
  65. M method;
  66. typename GetSimpleTypeT<P1>::type_t p1;
  67. typename GetSimpleTypeT<P2>::type_t p2;
  68. virtual void call() { (instance->*method)(p1, p2); }
  69. };
  70. template <class T, class M, class P1, class P2, class P3>
  71. struct Command3 : public CommandBase {
  72. T *instance;
  73. M method;
  74. typename GetSimpleTypeT<P1>::type_t p1;
  75. typename GetSimpleTypeT<P2>::type_t p2;
  76. typename GetSimpleTypeT<P3>::type_t p3;
  77. virtual void call() { (instance->*method)(p1, p2, p3); }
  78. };
  79. template <class T, class M, class P1, class P2, class P3, class P4>
  80. struct Command4 : public CommandBase {
  81. T *instance;
  82. M method;
  83. typename GetSimpleTypeT<P1>::type_t p1;
  84. typename GetSimpleTypeT<P2>::type_t p2;
  85. typename GetSimpleTypeT<P3>::type_t p3;
  86. typename GetSimpleTypeT<P4>::type_t p4;
  87. virtual void call() { (instance->*method)(p1, p2, p3, p4); }
  88. };
  89. template <class T, class M, class P1, class P2, class P3, class P4, class P5>
  90. struct Command5 : public CommandBase {
  91. T *instance;
  92. M method;
  93. typename GetSimpleTypeT<P1>::type_t p1;
  94. typename GetSimpleTypeT<P2>::type_t p2;
  95. typename GetSimpleTypeT<P3>::type_t p3;
  96. typename GetSimpleTypeT<P4>::type_t p4;
  97. typename GetSimpleTypeT<P5>::type_t p5;
  98. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5); }
  99. };
  100. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6>
  101. struct Command6 : public CommandBase {
  102. T *instance;
  103. M method;
  104. typename GetSimpleTypeT<P1>::type_t p1;
  105. typename GetSimpleTypeT<P2>::type_t p2;
  106. typename GetSimpleTypeT<P3>::type_t p3;
  107. typename GetSimpleTypeT<P4>::type_t p4;
  108. typename GetSimpleTypeT<P5>::type_t p5;
  109. typename GetSimpleTypeT<P6>::type_t p6;
  110. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6); }
  111. };
  112. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7>
  113. struct Command7 : public CommandBase {
  114. T *instance;
  115. M method;
  116. typename GetSimpleTypeT<P1>::type_t p1;
  117. typename GetSimpleTypeT<P2>::type_t p2;
  118. typename GetSimpleTypeT<P3>::type_t p3;
  119. typename GetSimpleTypeT<P4>::type_t p4;
  120. typename GetSimpleTypeT<P5>::type_t p5;
  121. typename GetSimpleTypeT<P6>::type_t p6;
  122. typename GetSimpleTypeT<P7>::type_t p7;
  123. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7); }
  124. };
  125. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
  126. struct Command8 : public CommandBase {
  127. T *instance;
  128. M method;
  129. typename GetSimpleTypeT<P1>::type_t p1;
  130. typename GetSimpleTypeT<P2>::type_t p2;
  131. typename GetSimpleTypeT<P3>::type_t p3;
  132. typename GetSimpleTypeT<P4>::type_t p4;
  133. typename GetSimpleTypeT<P5>::type_t p5;
  134. typename GetSimpleTypeT<P6>::type_t p6;
  135. typename GetSimpleTypeT<P7>::type_t p7;
  136. typename GetSimpleTypeT<P8>::type_t p8;
  137. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8); }
  138. };
  139. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9>
  140. struct Command9 : public CommandBase {
  141. T *instance;
  142. M method;
  143. typename GetSimpleTypeT<P1>::type_t p1;
  144. typename GetSimpleTypeT<P2>::type_t p2;
  145. typename GetSimpleTypeT<P3>::type_t p3;
  146. typename GetSimpleTypeT<P4>::type_t p4;
  147. typename GetSimpleTypeT<P5>::type_t p5;
  148. typename GetSimpleTypeT<P6>::type_t p6;
  149. typename GetSimpleTypeT<P7>::type_t p7;
  150. typename GetSimpleTypeT<P8>::type_t p8;
  151. typename GetSimpleTypeT<P9>::type_t p9;
  152. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9); }
  153. };
  154. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10>
  155. struct Command10 : public CommandBase {
  156. T *instance;
  157. M method;
  158. typename GetSimpleTypeT<P1>::type_t p1;
  159. typename GetSimpleTypeT<P2>::type_t p2;
  160. typename GetSimpleTypeT<P3>::type_t p3;
  161. typename GetSimpleTypeT<P4>::type_t p4;
  162. typename GetSimpleTypeT<P5>::type_t p5;
  163. typename GetSimpleTypeT<P6>::type_t p6;
  164. typename GetSimpleTypeT<P7>::type_t p7;
  165. typename GetSimpleTypeT<P8>::type_t p8;
  166. typename GetSimpleTypeT<P9>::type_t p9;
  167. typename GetSimpleTypeT<P10>::type_t p10;
  168. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10); }
  169. };
  170. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11>
  171. struct Command11 : public CommandBase {
  172. T *instance;
  173. M method;
  174. typename GetSimpleTypeT<P1>::type_t p1;
  175. typename GetSimpleTypeT<P2>::type_t p2;
  176. typename GetSimpleTypeT<P3>::type_t p3;
  177. typename GetSimpleTypeT<P4>::type_t p4;
  178. typename GetSimpleTypeT<P5>::type_t p5;
  179. typename GetSimpleTypeT<P6>::type_t p6;
  180. typename GetSimpleTypeT<P7>::type_t p7;
  181. typename GetSimpleTypeT<P8>::type_t p8;
  182. typename GetSimpleTypeT<P9>::type_t p9;
  183. typename GetSimpleTypeT<P10>::type_t p10;
  184. typename GetSimpleTypeT<P11>::type_t p11;
  185. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11); }
  186. };
  187. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12>
  188. struct Command12 : public CommandBase {
  189. T *instance;
  190. M method;
  191. typename GetSimpleTypeT<P1>::type_t p1;
  192. typename GetSimpleTypeT<P2>::type_t p2;
  193. typename GetSimpleTypeT<P3>::type_t p3;
  194. typename GetSimpleTypeT<P4>::type_t p4;
  195. typename GetSimpleTypeT<P5>::type_t p5;
  196. typename GetSimpleTypeT<P6>::type_t p6;
  197. typename GetSimpleTypeT<P7>::type_t p7;
  198. typename GetSimpleTypeT<P8>::type_t p8;
  199. typename GetSimpleTypeT<P9>::type_t p9;
  200. typename GetSimpleTypeT<P10>::type_t p10;
  201. typename GetSimpleTypeT<P11>::type_t p11;
  202. typename GetSimpleTypeT<P12>::type_t p12;
  203. virtual void call() { (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12); }
  204. };
  205. /* comands that return */
  206. template <class T, class M, class R>
  207. struct CommandRet0 : public CommandBase {
  208. T *instance;
  209. M method;
  210. R *ret;
  211. SyncSemaphore *sync;
  212. virtual void call() {
  213. *ret = (instance->*method)();
  214. sync->sem->post();
  215. sync->in_use = false;
  216. }
  217. };
  218. template <class T, class M, class P1, class R>
  219. struct CommandRet1 : public CommandBase {
  220. T *instance;
  221. M method;
  222. typename GetSimpleTypeT<P1>::type_t p1;
  223. R *ret;
  224. SyncSemaphore *sync;
  225. virtual void call() {
  226. *ret = (instance->*method)(p1);
  227. sync->sem->post();
  228. sync->in_use = false;
  229. }
  230. };
  231. template <class T, class M, class P1, class P2, class R>
  232. struct CommandRet2 : public CommandBase {
  233. T *instance;
  234. M method;
  235. typename GetSimpleTypeT<P1>::type_t p1;
  236. typename GetSimpleTypeT<P2>::type_t p2;
  237. R *ret;
  238. SyncSemaphore *sync;
  239. virtual void call() {
  240. *ret = (instance->*method)(p1, p2);
  241. sync->sem->post();
  242. sync->in_use = false;
  243. }
  244. };
  245. template <class T, class M, class P1, class P2, class P3, class R>
  246. struct CommandRet3 : public CommandBase {
  247. T *instance;
  248. M method;
  249. typename GetSimpleTypeT<P1>::type_t p1;
  250. typename GetSimpleTypeT<P2>::type_t p2;
  251. typename GetSimpleTypeT<P3>::type_t p3;
  252. R *ret;
  253. SyncSemaphore *sync;
  254. virtual void call() {
  255. *ret = (instance->*method)(p1, p2, p3);
  256. sync->sem->post();
  257. sync->in_use = false;
  258. }
  259. };
  260. template <class T, class M, class P1, class P2, class P3, class P4, class R>
  261. struct CommandRet4 : public CommandBase {
  262. T *instance;
  263. M method;
  264. typename GetSimpleTypeT<P1>::type_t p1;
  265. typename GetSimpleTypeT<P2>::type_t p2;
  266. typename GetSimpleTypeT<P3>::type_t p3;
  267. typename GetSimpleTypeT<P4>::type_t p4;
  268. R *ret;
  269. SyncSemaphore *sync;
  270. virtual void call() {
  271. *ret = (instance->*method)(p1, p2, p3, p4);
  272. sync->sem->post();
  273. sync->in_use = false;
  274. }
  275. };
  276. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class R>
  277. struct CommandRet5 : public CommandBase {
  278. T *instance;
  279. M method;
  280. typename GetSimpleTypeT<P1>::type_t p1;
  281. typename GetSimpleTypeT<P2>::type_t p2;
  282. typename GetSimpleTypeT<P3>::type_t p3;
  283. typename GetSimpleTypeT<P4>::type_t p4;
  284. typename GetSimpleTypeT<P5>::type_t p5;
  285. R *ret;
  286. SyncSemaphore *sync;
  287. virtual void call() {
  288. *ret = (instance->*method)(p1, p2, p3, p4, p5);
  289. sync->sem->post();
  290. sync->in_use = false;
  291. }
  292. };
  293. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class R>
  294. struct CommandRet6 : public CommandBase {
  295. T *instance;
  296. M method;
  297. typename GetSimpleTypeT<P1>::type_t p1;
  298. typename GetSimpleTypeT<P2>::type_t p2;
  299. typename GetSimpleTypeT<P3>::type_t p3;
  300. typename GetSimpleTypeT<P4>::type_t p4;
  301. typename GetSimpleTypeT<P5>::type_t p5;
  302. typename GetSimpleTypeT<P6>::type_t p6;
  303. R *ret;
  304. SyncSemaphore *sync;
  305. virtual void call() {
  306. *ret = (instance->*method)(p1, p2, p3, p4, p5, p6);
  307. sync->sem->post();
  308. sync->in_use = false;
  309. }
  310. };
  311. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class R>
  312. struct CommandRet7 : public CommandBase {
  313. T *instance;
  314. M method;
  315. typename GetSimpleTypeT<P1>::type_t p1;
  316. typename GetSimpleTypeT<P2>::type_t p2;
  317. typename GetSimpleTypeT<P3>::type_t p3;
  318. typename GetSimpleTypeT<P4>::type_t p4;
  319. typename GetSimpleTypeT<P5>::type_t p5;
  320. typename GetSimpleTypeT<P6>::type_t p6;
  321. typename GetSimpleTypeT<P7>::type_t p7;
  322. R *ret;
  323. SyncSemaphore *sync;
  324. virtual void call() {
  325. *ret = (instance->*method)(p1, p2, p3, p4, p5, p6, p7);
  326. sync->sem->post();
  327. sync->in_use = false;
  328. }
  329. };
  330. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class R>
  331. struct CommandRet8 : public CommandBase {
  332. T *instance;
  333. M method;
  334. typename GetSimpleTypeT<P1>::type_t p1;
  335. typename GetSimpleTypeT<P2>::type_t p2;
  336. typename GetSimpleTypeT<P3>::type_t p3;
  337. typename GetSimpleTypeT<P4>::type_t p4;
  338. typename GetSimpleTypeT<P5>::type_t p5;
  339. typename GetSimpleTypeT<P6>::type_t p6;
  340. typename GetSimpleTypeT<P7>::type_t p7;
  341. typename GetSimpleTypeT<P8>::type_t p8;
  342. R *ret;
  343. SyncSemaphore *sync;
  344. virtual void call() {
  345. *ret = (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8);
  346. sync->sem->post();
  347. sync->in_use = false;
  348. }
  349. };
  350. /** commands that don't return but sync */
  351. /* comands that return */
  352. template <class T, class M>
  353. struct CommandSync0 : public CommandBase {
  354. T *instance;
  355. M method;
  356. SyncSemaphore *sync;
  357. virtual void call() {
  358. (instance->*method)();
  359. sync->sem->post();
  360. sync->in_use = false;
  361. }
  362. };
  363. template <class T, class M, class P1>
  364. struct CommandSync1 : public CommandBase {
  365. T *instance;
  366. M method;
  367. typename GetSimpleTypeT<P1>::type_t p1;
  368. SyncSemaphore *sync;
  369. virtual void call() {
  370. (instance->*method)(p1);
  371. sync->sem->post();
  372. sync->in_use = false;
  373. }
  374. };
  375. template <class T, class M, class P1, class P2>
  376. struct CommandSync2 : public CommandBase {
  377. T *instance;
  378. M method;
  379. typename GetSimpleTypeT<P1>::type_t p1;
  380. typename GetSimpleTypeT<P2>::type_t p2;
  381. SyncSemaphore *sync;
  382. virtual void call() {
  383. (instance->*method)(p1, p2);
  384. sync->sem->post();
  385. sync->in_use = false;
  386. }
  387. };
  388. template <class T, class M, class P1, class P2, class P3>
  389. struct CommandSync3 : public CommandBase {
  390. T *instance;
  391. M method;
  392. typename GetSimpleTypeT<P1>::type_t p1;
  393. typename GetSimpleTypeT<P2>::type_t p2;
  394. typename GetSimpleTypeT<P3>::type_t p3;
  395. SyncSemaphore *sync;
  396. virtual void call() {
  397. (instance->*method)(p1, p2, p3);
  398. sync->sem->post();
  399. sync->in_use = false;
  400. }
  401. };
  402. template <class T, class M, class P1, class P2, class P3, class P4>
  403. struct CommandSync4 : public CommandBase {
  404. T *instance;
  405. M method;
  406. typename GetSimpleTypeT<P1>::type_t p1;
  407. typename GetSimpleTypeT<P2>::type_t p2;
  408. typename GetSimpleTypeT<P3>::type_t p3;
  409. typename GetSimpleTypeT<P4>::type_t p4;
  410. SyncSemaphore *sync;
  411. virtual void call() {
  412. (instance->*method)(p1, p2, p3, p4);
  413. sync->sem->post();
  414. sync->in_use = false;
  415. }
  416. };
  417. template <class T, class M, class P1, class P2, class P3, class P4, class P5>
  418. struct CommandSync5 : public CommandBase {
  419. T *instance;
  420. M method;
  421. typename GetSimpleTypeT<P1>::type_t p1;
  422. typename GetSimpleTypeT<P2>::type_t p2;
  423. typename GetSimpleTypeT<P3>::type_t p3;
  424. typename GetSimpleTypeT<P4>::type_t p4;
  425. typename GetSimpleTypeT<P5>::type_t p5;
  426. SyncSemaphore *sync;
  427. virtual void call() {
  428. (instance->*method)(p1, p2, p3, p4, p5);
  429. sync->sem->post();
  430. sync->in_use = false;
  431. }
  432. };
  433. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6>
  434. struct CommandSync6 : public CommandBase {
  435. T *instance;
  436. M method;
  437. typename GetSimpleTypeT<P1>::type_t p1;
  438. typename GetSimpleTypeT<P2>::type_t p2;
  439. typename GetSimpleTypeT<P3>::type_t p3;
  440. typename GetSimpleTypeT<P4>::type_t p4;
  441. typename GetSimpleTypeT<P5>::type_t p5;
  442. typename GetSimpleTypeT<P6>::type_t p6;
  443. SyncSemaphore *sync;
  444. virtual void call() {
  445. (instance->*method)(p1, p2, p3, p4, p5, p6);
  446. sync->sem->post();
  447. sync->in_use = false;
  448. }
  449. };
  450. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7>
  451. struct CommandSync7 : public CommandBase {
  452. T *instance;
  453. M method;
  454. typename GetSimpleTypeT<P1>::type_t p1;
  455. typename GetSimpleTypeT<P2>::type_t p2;
  456. typename GetSimpleTypeT<P3>::type_t p3;
  457. typename GetSimpleTypeT<P4>::type_t p4;
  458. typename GetSimpleTypeT<P5>::type_t p5;
  459. typename GetSimpleTypeT<P6>::type_t p6;
  460. typename GetSimpleTypeT<P7>::type_t p7;
  461. SyncSemaphore *sync;
  462. virtual void call() {
  463. (instance->*method)(p1, p2, p3, p4, p5, p6, p7);
  464. sync->sem->post();
  465. sync->in_use = false;
  466. }
  467. };
  468. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
  469. struct CommandSync8 : public CommandBase {
  470. T *instance;
  471. M method;
  472. typename GetSimpleTypeT<P1>::type_t p1;
  473. typename GetSimpleTypeT<P2>::type_t p2;
  474. typename GetSimpleTypeT<P3>::type_t p3;
  475. typename GetSimpleTypeT<P4>::type_t p4;
  476. typename GetSimpleTypeT<P5>::type_t p5;
  477. typename GetSimpleTypeT<P6>::type_t p6;
  478. typename GetSimpleTypeT<P7>::type_t p7;
  479. typename GetSimpleTypeT<P8>::type_t p8;
  480. SyncSemaphore *sync;
  481. virtual void call() {
  482. (instance->*method)(p1, p2, p3, p4, p5, p6, p7, p8);
  483. sync->sem->post();
  484. sync->in_use = false;
  485. }
  486. };
  487. /***** BASE *******/
  488. enum {
  489. COMMAND_MEM_SIZE_KB = 256,
  490. COMMAND_MEM_SIZE = COMMAND_MEM_SIZE_KB * 1024,
  491. SYNC_SEMAPHORES = 8
  492. };
  493. uint8_t command_mem[COMMAND_MEM_SIZE];
  494. uint32_t read_ptr;
  495. uint32_t write_ptr;
  496. SyncSemaphore sync_sems[SYNC_SEMAPHORES];
  497. Mutex *mutex;
  498. Semaphore *sync;
  499. template <class T>
  500. T *allocate() {
  501. // alloc size is size+T+safeguard
  502. uint32_t alloc_size = sizeof(T) + sizeof(uint32_t);
  503. tryagain:
  504. if (write_ptr < read_ptr) {
  505. // behind read_ptr, check that there is room
  506. if ((read_ptr - write_ptr) <= alloc_size)
  507. return NULL;
  508. } else if (write_ptr >= read_ptr) {
  509. // ahead of read_ptr, check that there is room
  510. if ((COMMAND_MEM_SIZE - write_ptr) < alloc_size + 4) {
  511. // no room at the end, wrap down;
  512. if (read_ptr == 0) // don't want write_ptr to become read_ptr
  513. return NULL;
  514. // if this happens, it's a bug
  515. ERR_FAIL_COND_V((COMMAND_MEM_SIZE - write_ptr) < sizeof(uint32_t), NULL);
  516. // zero means, wrap to beginning
  517. uint32_t *p = (uint32_t *)&command_mem[write_ptr];
  518. *p = 0;
  519. write_ptr = 0;
  520. goto tryagain;
  521. }
  522. }
  523. // allocate the size
  524. uint32_t *p = (uint32_t *)&command_mem[write_ptr];
  525. *p = sizeof(T);
  526. write_ptr += sizeof(uint32_t);
  527. // allocate the command
  528. T *cmd = memnew_placement(&command_mem[write_ptr], T);
  529. write_ptr += sizeof(T);
  530. return cmd;
  531. }
  532. template <class T>
  533. T *allocate_and_lock() {
  534. lock();
  535. T *ret;
  536. while ((ret = allocate<T>()) == NULL) {
  537. unlock();
  538. // sleep a little until fetch happened and some room is made
  539. wait_for_flush();
  540. lock();
  541. }
  542. return ret;
  543. }
  544. bool flush_one() {
  545. tryagain:
  546. // tried to read an empty queue
  547. if (read_ptr == write_ptr)
  548. return false;
  549. uint32_t size = *(uint32_t *)(&command_mem[read_ptr]);
  550. if (size == 0) {
  551. //end of ringbuffer, wrap
  552. read_ptr = 0;
  553. goto tryagain;
  554. }
  555. read_ptr += sizeof(uint32_t);
  556. CommandBase *cmd = reinterpret_cast<CommandBase *>(&command_mem[read_ptr]);
  557. cmd->call();
  558. cmd->~CommandBase();
  559. read_ptr += size;
  560. return true;
  561. }
  562. void lock();
  563. void unlock();
  564. void wait_for_flush();
  565. SyncSemaphore *_alloc_sync_sem();
  566. public:
  567. /* NORMAL PUSH COMMANDS */
  568. template <class T, class M>
  569. void push(T *p_instance, M p_method) {
  570. Command0<T, M> *cmd = allocate_and_lock<Command0<T, M> >();
  571. cmd->instance = p_instance;
  572. cmd->method = p_method;
  573. unlock();
  574. if (sync) sync->post();
  575. }
  576. template <class T, class M, class P1>
  577. void push(T *p_instance, M p_method, P1 p1) {
  578. Command1<T, M, P1> *cmd = allocate_and_lock<Command1<T, M, P1> >();
  579. cmd->instance = p_instance;
  580. cmd->method = p_method;
  581. cmd->p1 = p1;
  582. unlock();
  583. if (sync) sync->post();
  584. }
  585. template <class T, class M, class P1, class P2>
  586. void push(T *p_instance, M p_method, P1 p1, P2 p2) {
  587. Command2<T, M, P1, P2> *cmd = allocate_and_lock<Command2<T, M, P1, P2> >();
  588. cmd->instance = p_instance;
  589. cmd->method = p_method;
  590. cmd->p1 = p1;
  591. cmd->p2 = p2;
  592. unlock();
  593. if (sync) sync->post();
  594. }
  595. template <class T, class M, class P1, class P2, class P3>
  596. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3) {
  597. Command3<T, M, P1, P2, P3> *cmd = allocate_and_lock<Command3<T, M, P1, P2, P3> >();
  598. cmd->instance = p_instance;
  599. cmd->method = p_method;
  600. cmd->p1 = p1;
  601. cmd->p2 = p2;
  602. cmd->p3 = p3;
  603. unlock();
  604. if (sync) sync->post();
  605. }
  606. template <class T, class M, class P1, class P2, class P3, class P4>
  607. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4) {
  608. Command4<T, M, P1, P2, P3, P4> *cmd = allocate_and_lock<Command4<T, M, P1, P2, P3, P4> >();
  609. cmd->instance = p_instance;
  610. cmd->method = p_method;
  611. cmd->p1 = p1;
  612. cmd->p2 = p2;
  613. cmd->p3 = p3;
  614. cmd->p4 = p4;
  615. unlock();
  616. if (sync) sync->post();
  617. }
  618. template <class T, class M, class P1, class P2, class P3, class P4, class P5>
  619. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  620. Command5<T, M, P1, P2, P3, P4, P5> *cmd = allocate_and_lock<Command5<T, M, P1, P2, P3, P4, P5> >();
  621. cmd->instance = p_instance;
  622. cmd->method = p_method;
  623. cmd->p1 = p1;
  624. cmd->p2 = p2;
  625. cmd->p3 = p3;
  626. cmd->p4 = p4;
  627. cmd->p5 = p5;
  628. unlock();
  629. if (sync) sync->post();
  630. }
  631. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6>
  632. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
  633. Command6<T, M, P1, P2, P3, P4, P5, P6> *cmd = allocate_and_lock<Command6<T, M, P1, P2, P3, P4, P5, P6> >();
  634. cmd->instance = p_instance;
  635. cmd->method = p_method;
  636. cmd->p1 = p1;
  637. cmd->p2 = p2;
  638. cmd->p3 = p3;
  639. cmd->p4 = p4;
  640. cmd->p5 = p5;
  641. cmd->p6 = p6;
  642. unlock();
  643. if (sync) sync->post();
  644. }
  645. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7>
  646. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
  647. Command7<T, M, P1, P2, P3, P4, P5, P6, P7> *cmd = allocate_and_lock<Command7<T, M, P1, P2, P3, P4, P5, P6, P7> >();
  648. cmd->instance = p_instance;
  649. cmd->method = p_method;
  650. cmd->p1 = p1;
  651. cmd->p2 = p2;
  652. cmd->p3 = p3;
  653. cmd->p4 = p4;
  654. cmd->p5 = p5;
  655. cmd->p6 = p6;
  656. cmd->p7 = p7;
  657. unlock();
  658. if (sync) sync->post();
  659. }
  660. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
  661. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) {
  662. Command8<T, M, P1, P2, P3, P4, P5, P6, P7, P8> *cmd = allocate_and_lock<Command8<T, M, P1, P2, P3, P4, P5, P6, P7, P8> >();
  663. cmd->instance = p_instance;
  664. cmd->method = p_method;
  665. cmd->p1 = p1;
  666. cmd->p2 = p2;
  667. cmd->p3 = p3;
  668. cmd->p4 = p4;
  669. cmd->p5 = p5;
  670. cmd->p6 = p6;
  671. cmd->p7 = p7;
  672. cmd->p8 = p8;
  673. unlock();
  674. if (sync) sync->post();
  675. }
  676. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9>
  677. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9) {
  678. Command9<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9> *cmd = allocate_and_lock<Command9<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9> >();
  679. cmd->instance = p_instance;
  680. cmd->method = p_method;
  681. cmd->p1 = p1;
  682. cmd->p2 = p2;
  683. cmd->p3 = p3;
  684. cmd->p4 = p4;
  685. cmd->p5 = p5;
  686. cmd->p6 = p6;
  687. cmd->p7 = p7;
  688. cmd->p8 = p8;
  689. cmd->p9 = p9;
  690. unlock();
  691. if (sync) sync->post();
  692. }
  693. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10>
  694. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10) {
  695. Command10<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> *cmd = allocate_and_lock<Command10<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10> >();
  696. cmd->instance = p_instance;
  697. cmd->method = p_method;
  698. cmd->p1 = p1;
  699. cmd->p2 = p2;
  700. cmd->p3 = p3;
  701. cmd->p4 = p4;
  702. cmd->p5 = p5;
  703. cmd->p6 = p6;
  704. cmd->p7 = p7;
  705. cmd->p8 = p8;
  706. cmd->p9 = p9;
  707. cmd->p10 = p10;
  708. unlock();
  709. if (sync) sync->post();
  710. }
  711. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11>
  712. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11) {
  713. Command11<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11> *cmd = allocate_and_lock<Command11<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11> >();
  714. cmd->instance = p_instance;
  715. cmd->method = p_method;
  716. cmd->p1 = p1;
  717. cmd->p2 = p2;
  718. cmd->p3 = p3;
  719. cmd->p4 = p4;
  720. cmd->p5 = p5;
  721. cmd->p6 = p6;
  722. cmd->p7 = p7;
  723. cmd->p8 = p8;
  724. cmd->p9 = p9;
  725. cmd->p10 = p10;
  726. cmd->p11 = p11;
  727. unlock();
  728. if (sync) sync->post();
  729. }
  730. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class P9, class P10, class P11, class P12>
  731. void push(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, P9 p9, P10 p10, P11 p11, P12 p12) {
  732. Command12<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12> *cmd = allocate_and_lock<Command12<T, M, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12> >();
  733. cmd->instance = p_instance;
  734. cmd->method = p_method;
  735. cmd->p1 = p1;
  736. cmd->p2 = p2;
  737. cmd->p3 = p3;
  738. cmd->p4 = p4;
  739. cmd->p5 = p5;
  740. cmd->p6 = p6;
  741. cmd->p7 = p7;
  742. cmd->p8 = p8;
  743. cmd->p9 = p9;
  744. cmd->p10 = p10;
  745. cmd->p11 = p11;
  746. cmd->p12 = p12;
  747. unlock();
  748. if (sync) sync->post();
  749. }
  750. /*** PUSH AND RET COMMANDS ***/
  751. template <class T, class M, class R>
  752. void push_and_ret(T *p_instance, M p_method, R *r_ret) {
  753. SyncSemaphore *ss = _alloc_sync_sem();
  754. CommandRet0<T, M, R> *cmd = allocate_and_lock<CommandRet0<T, M, R> >();
  755. cmd->instance = p_instance;
  756. cmd->method = p_method;
  757. cmd->ret = r_ret;
  758. cmd->sync = ss;
  759. unlock();
  760. if (sync) sync->post();
  761. ss->sem->wait();
  762. }
  763. template <class T, class M, class P1, class R>
  764. void push_and_ret(T *p_instance, M p_method, P1 p1, R *r_ret) {
  765. SyncSemaphore *ss = _alloc_sync_sem();
  766. CommandRet1<T, M, P1, R> *cmd = allocate_and_lock<CommandRet1<T, M, P1, R> >();
  767. cmd->instance = p_instance;
  768. cmd->method = p_method;
  769. cmd->p1 = p1;
  770. cmd->ret = r_ret;
  771. cmd->sync = ss;
  772. unlock();
  773. if (sync) sync->post();
  774. ss->sem->wait();
  775. }
  776. template <class T, class M, class P1, class P2, class R>
  777. void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, R *r_ret) {
  778. SyncSemaphore *ss = _alloc_sync_sem();
  779. CommandRet2<T, M, P1, P2, R> *cmd = allocate_and_lock<CommandRet2<T, M, P1, P2, R> >();
  780. cmd->instance = p_instance;
  781. cmd->method = p_method;
  782. cmd->p1 = p1;
  783. cmd->p2 = p2;
  784. cmd->ret = r_ret;
  785. cmd->sync = ss;
  786. unlock();
  787. if (sync) sync->post();
  788. ss->sem->wait();
  789. }
  790. template <class T, class M, class P1, class P2, class P3, class R>
  791. void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, R *r_ret) {
  792. SyncSemaphore *ss = _alloc_sync_sem();
  793. CommandRet3<T, M, P1, P2, P3, R> *cmd = allocate_and_lock<CommandRet3<T, M, P1, P2, P3, R> >();
  794. cmd->instance = p_instance;
  795. cmd->method = p_method;
  796. cmd->p1 = p1;
  797. cmd->p2 = p2;
  798. cmd->p3 = p3;
  799. cmd->ret = r_ret;
  800. cmd->sync = ss;
  801. unlock();
  802. if (sync) sync->post();
  803. ss->sem->wait();
  804. }
  805. template <class T, class M, class P1, class P2, class P3, class P4, class R>
  806. void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, R *r_ret) {
  807. SyncSemaphore *ss = _alloc_sync_sem();
  808. CommandRet4<T, M, P1, P2, P3, P4, R> *cmd = allocate_and_lock<CommandRet4<T, M, P1, P2, P3, P4, R> >();
  809. cmd->instance = p_instance;
  810. cmd->method = p_method;
  811. cmd->p1 = p1;
  812. cmd->p2 = p2;
  813. cmd->p3 = p3;
  814. cmd->p4 = p4;
  815. cmd->ret = r_ret;
  816. cmd->sync = ss;
  817. unlock();
  818. if (sync) sync->post();
  819. ss->sem->wait();
  820. }
  821. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class R>
  822. void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, R *r_ret) {
  823. SyncSemaphore *ss = _alloc_sync_sem();
  824. CommandRet5<T, M, P1, P2, P3, P4, P5, R> *cmd = allocate_and_lock<CommandRet5<T, M, P1, P2, P3, P4, P5, R> >();
  825. cmd->instance = p_instance;
  826. cmd->method = p_method;
  827. cmd->p1 = p1;
  828. cmd->p2 = p2;
  829. cmd->p3 = p3;
  830. cmd->p4 = p4;
  831. cmd->p5 = p5;
  832. cmd->ret = r_ret;
  833. cmd->sync = ss;
  834. unlock();
  835. if (sync) sync->post();
  836. ss->sem->wait();
  837. }
  838. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class R>
  839. void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, R *r_ret) {
  840. SyncSemaphore *ss = _alloc_sync_sem();
  841. CommandRet6<T, M, P1, P2, P3, P4, P5, P6, R> *cmd = allocate_and_lock<CommandRet6<T, M, P1, P2, P3, P4, P5, P6, R> >();
  842. cmd->instance = p_instance;
  843. cmd->method = p_method;
  844. cmd->p1 = p1;
  845. cmd->p2 = p2;
  846. cmd->p3 = p3;
  847. cmd->p4 = p4;
  848. cmd->p5 = p5;
  849. cmd->p6 = p6;
  850. cmd->ret = r_ret;
  851. cmd->sync = ss;
  852. unlock();
  853. if (sync) sync->post();
  854. ss->sem->wait();
  855. }
  856. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class R>
  857. void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, R *r_ret) {
  858. SyncSemaphore *ss = _alloc_sync_sem();
  859. CommandRet7<T, M, P1, P2, P3, P4, P5, P6, P7, R> *cmd = allocate_and_lock<CommandRet7<T, M, P1, P2, P3, P4, P5, P6, P7, R> >();
  860. cmd->instance = p_instance;
  861. cmd->method = p_method;
  862. cmd->p1 = p1;
  863. cmd->p2 = p2;
  864. cmd->p3 = p3;
  865. cmd->p4 = p4;
  866. cmd->p5 = p5;
  867. cmd->p6 = p6;
  868. cmd->p7 = p7;
  869. cmd->ret = r_ret;
  870. cmd->sync = ss;
  871. unlock();
  872. if (sync) sync->post();
  873. ss->sem->wait();
  874. }
  875. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8, class R>
  876. void push_and_ret(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8, R *r_ret) {
  877. SyncSemaphore *ss = _alloc_sync_sem();
  878. CommandRet8<T, M, P1, P2, P3, P4, P5, P6, P7, P8, R> *cmd = allocate_and_lock<CommandRet8<T, M, P1, P2, P3, P4, P5, P6, P7, P8, R> >();
  879. cmd->instance = p_instance;
  880. cmd->method = p_method;
  881. cmd->p1 = p1;
  882. cmd->p2 = p2;
  883. cmd->p3 = p3;
  884. cmd->p4 = p4;
  885. cmd->p5 = p5;
  886. cmd->p6 = p6;
  887. cmd->p7 = p7;
  888. cmd->p8 = p8;
  889. cmd->ret = r_ret;
  890. cmd->sync = ss;
  891. unlock();
  892. if (sync) sync->post();
  893. ss->sem->wait();
  894. }
  895. template <class T, class M>
  896. void push_and_sync(T *p_instance, M p_method) {
  897. SyncSemaphore *ss = _alloc_sync_sem();
  898. CommandSync0<T, M> *cmd = allocate_and_lock<CommandSync0<T, M> >();
  899. cmd->instance = p_instance;
  900. cmd->method = p_method;
  901. cmd->sync = ss;
  902. unlock();
  903. if (sync) sync->post();
  904. ss->sem->wait();
  905. }
  906. template <class T, class M, class P1>
  907. void push_and_sync(T *p_instance, M p_method, P1 p1) {
  908. SyncSemaphore *ss = _alloc_sync_sem();
  909. CommandSync1<T, M, P1> *cmd = allocate_and_lock<CommandSync1<T, M, P1> >();
  910. cmd->instance = p_instance;
  911. cmd->method = p_method;
  912. cmd->p1 = p1;
  913. cmd->sync = ss;
  914. unlock();
  915. if (sync) sync->post();
  916. ss->sem->wait();
  917. }
  918. template <class T, class M, class P1, class P2>
  919. void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2) {
  920. SyncSemaphore *ss = _alloc_sync_sem();
  921. CommandSync2<T, M, P1, P2> *cmd = allocate_and_lock<CommandSync2<T, M, P1, P2> >();
  922. cmd->instance = p_instance;
  923. cmd->method = p_method;
  924. cmd->p1 = p1;
  925. cmd->p2 = p2;
  926. cmd->sync = ss;
  927. unlock();
  928. if (sync) sync->post();
  929. ss->sem->wait();
  930. }
  931. template <class T, class M, class P1, class P2, class P3>
  932. void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3) {
  933. SyncSemaphore *ss = _alloc_sync_sem();
  934. CommandSync3<T, M, P1, P2, P3> *cmd = allocate_and_lock<CommandSync3<T, M, P1, P2, P3> >();
  935. cmd->instance = p_instance;
  936. cmd->method = p_method;
  937. cmd->p1 = p1;
  938. cmd->p2 = p2;
  939. cmd->p3 = p3;
  940. cmd->sync = ss;
  941. unlock();
  942. if (sync) sync->post();
  943. ss->sem->wait();
  944. }
  945. template <class T, class M, class P1, class P2, class P3, class P4>
  946. void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4) {
  947. SyncSemaphore *ss = _alloc_sync_sem();
  948. CommandSync4<T, M, P1, P2, P3, P4> *cmd = allocate_and_lock<CommandSync4<T, M, P1, P2, P3, P4> >();
  949. cmd->instance = p_instance;
  950. cmd->method = p_method;
  951. cmd->p1 = p1;
  952. cmd->p2 = p2;
  953. cmd->p3 = p3;
  954. cmd->p4 = p4;
  955. cmd->sync = ss;
  956. unlock();
  957. if (sync) sync->post();
  958. ss->sem->wait();
  959. }
  960. template <class T, class M, class P1, class P2, class P3, class P4, class P5>
  961. void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  962. SyncSemaphore *ss = _alloc_sync_sem();
  963. CommandSync5<T, M, P1, P2, P3, P4, P5> *cmd = allocate_and_lock<CommandSync5<T, M, P1, P2, P3, P4, P5> >();
  964. cmd->instance = p_instance;
  965. cmd->method = p_method;
  966. cmd->p1 = p1;
  967. cmd->p2 = p2;
  968. cmd->p3 = p3;
  969. cmd->p4 = p4;
  970. cmd->p5 = p5;
  971. cmd->sync = ss;
  972. unlock();
  973. if (sync) sync->post();
  974. ss->sem->wait();
  975. }
  976. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6>
  977. void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6) {
  978. SyncSemaphore *ss = _alloc_sync_sem();
  979. CommandSync6<T, M, P1, P2, P3, P4, P5, P6> *cmd = allocate_and_lock<CommandSync6<T, M, P1, P2, P3, P4, P5, P6> >();
  980. cmd->instance = p_instance;
  981. cmd->method = p_method;
  982. cmd->p1 = p1;
  983. cmd->p2 = p2;
  984. cmd->p3 = p3;
  985. cmd->p4 = p4;
  986. cmd->p5 = p5;
  987. cmd->p6 = p6;
  988. cmd->sync = ss;
  989. unlock();
  990. if (sync) sync->post();
  991. ss->sem->wait();
  992. }
  993. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7>
  994. void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7) {
  995. SyncSemaphore *ss = _alloc_sync_sem();
  996. CommandSync7<T, M, P1, P2, P3, P4, P5, P6, P7> *cmd = allocate_and_lock<CommandSync7<T, M, P1, P2, P3, P4, P5, P6, P7> >();
  997. cmd->instance = p_instance;
  998. cmd->method = p_method;
  999. cmd->p1 = p1;
  1000. cmd->p2 = p2;
  1001. cmd->p3 = p3;
  1002. cmd->p4 = p4;
  1003. cmd->p5 = p5;
  1004. cmd->p6 = p6;
  1005. cmd->p7 = p7;
  1006. cmd->sync = ss;
  1007. unlock();
  1008. if (sync) sync->post();
  1009. ss->sem->wait();
  1010. }
  1011. template <class T, class M, class P1, class P2, class P3, class P4, class P5, class P6, class P7, class P8>
  1012. void push_and_sync(T *p_instance, M p_method, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5, P6 p6, P7 p7, P8 p8) {
  1013. SyncSemaphore *ss = _alloc_sync_sem();
  1014. CommandSync8<T, M, P1, P2, P3, P4, P5, P6, P7, P8> *cmd = allocate_and_lock<CommandSync8<T, M, P1, P2, P3, P4, P5, P6, P7, P8> >();
  1015. cmd->instance = p_instance;
  1016. cmd->method = p_method;
  1017. cmd->p1 = p1;
  1018. cmd->p2 = p2;
  1019. cmd->p3 = p3;
  1020. cmd->p4 = p4;
  1021. cmd->p5 = p5;
  1022. cmd->p6 = p6;
  1023. cmd->p7 = p7;
  1024. cmd->p8 = p8;
  1025. cmd->sync = ss;
  1026. unlock();
  1027. if (sync) sync->post();
  1028. ss->sem->wait();
  1029. }
  1030. void wait_and_flush_one() {
  1031. ERR_FAIL_COND(!sync);
  1032. sync->wait();
  1033. lock();
  1034. flush_one();
  1035. unlock();
  1036. }
  1037. void flush_all() {
  1038. //ERR_FAIL_COND(sync);
  1039. lock();
  1040. while (true) {
  1041. bool exit = !flush_one();
  1042. if (exit)
  1043. break;
  1044. }
  1045. unlock();
  1046. }
  1047. CommandQueueMT(bool p_sync);
  1048. ~CommandQueueMT();
  1049. };
  1050. #endif