sq_slave_vm.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. ** Rings: Multiple Lua States
  3. ** $Id: rings.c,v 1.24 2008/06/30 17:52:31 carregal Exp $
  4. ** See Copyright Notice in license.html
  5. */
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "squirrel.h"
  9. #include <sqstdblob.h>
  10. #include <sqstdsystem.h>
  11. #include <sqstdio.h>
  12. #include <sqstdmath.h>
  13. #include <sqstdstring.h>
  14. #ifdef SLAVE_VM_WITH_OS_THREADS
  15. #include "mongoose.h"
  16. #endif // SLAVE_VM_WITH_OS_THREADS
  17. /*
  18. ** Copies values from State src to State dst.
  19. */
  20. static SQRESULT copy_values_between_vms (HSQUIRRELVM dst, HSQUIRRELVM src, int argc, int argIdx)
  21. {
  22. SQRESULT _rc_;
  23. sq_reservestack(dst, argc + 20);
  24. argc += argIdx; //we will work with argc args starting at argIdx
  25. for (; argIdx < argc; argIdx++)
  26. {
  27. switch (sq_gettype(src, argIdx))
  28. {
  29. case OT_INTEGER:
  30. SQ_GET_INTEGER(src, argIdx, vint);
  31. sq_pushinteger(dst, vint);
  32. break;
  33. case OT_FLOAT:
  34. SQ_GET_FLOAT(src, argIdx, vfloat);
  35. sq_pushfloat (dst, vfloat);
  36. break;
  37. case OT_BOOL:
  38. SQ_GET_BOOL(src, argIdx, vbool);
  39. sq_pushbool (dst, vbool);
  40. break;
  41. case OT_STRING:
  42. {
  43. SQ_GET_STRING(src, argIdx, vstr)
  44. sq_pushstring (dst, vstr, vstr_size);
  45. }
  46. break;
  47. case OT_ARRAY:
  48. {
  49. SQInteger size = sq_getsize(src, argIdx);
  50. sq_newarray(dst, size);
  51. for(SQInteger i=0; i<size; ++i)
  52. {
  53. sq_pushinteger(src, i);
  54. sq_get(src, -2);
  55. sq_pushinteger(dst, i);
  56. if(copy_values_between_vms(dst, src, 1, sq_gettop(src)) != SQ_OK) return SQ_ERROR;
  57. sq_poptop(src);
  58. sq_set(dst, -3);
  59. }
  60. }
  61. break;
  62. case OT_TABLE:
  63. {
  64. sq_newtable(dst);
  65. sq_pushnull(src);
  66. while(sq_next(src, -2) == SQ_OK)
  67. {
  68. SQInteger src_top = sq_gettop(src);
  69. if(copy_values_between_vms(dst, src, 1, src_top-1) != SQ_OK
  70. || copy_values_between_vms(dst, src, 1, src_top) != SQ_OK) return SQ_ERROR;
  71. sq_newslot(dst, -3, SQFalse);
  72. sq_pop(src, 2);
  73. }
  74. sq_pop(src,1);
  75. }
  76. break;
  77. case OT_USERPOINTER:
  78. {
  79. SQUserPointer ptr;
  80. sq_getuserpointer(src, argIdx, &ptr);
  81. sq_pushuserpointer(dst, ptr);
  82. }
  83. break;
  84. case OT_NULL:
  85. sq_pushnull(dst);
  86. break;
  87. default:
  88. return SQ_ERROR;
  89. }
  90. }
  91. return SQ_OK;
  92. }
  93. struct SlaveVM_st {
  94. HSQUIRRELVM v;
  95. HSQUIRRELVM master_vm;
  96. #ifdef SLAVE_VM_WITH_OS_THREADS
  97. mg_thread_mutex_t mutex;
  98. mg_thread_cond_t cond_var;
  99. mg_thread_t thread_id;
  100. int slave_state, master_state;
  101. bool isRunningAsThread;
  102. #endif // SLAVE_VM_WITH_OS_THREADS
  103. };
  104. static const SQChar sq_slave_vm_TAG[] = _SC("SlaveVM");
  105. static SQRESULT get_slave_vm_instance(HSQUIRRELVM v, SQInteger idx, SlaveVM_st **svm_st)
  106. {
  107. if(sq_getinstanceup(v, idx, (SQUserPointer*)svm_st, (void*)sq_slave_vm_TAG) != SQ_OK) return SQ_ERROR;
  108. if(!*svm_st) return sq_throwerror(v, _SC("slave vm already closed"));
  109. return SQ_OK;
  110. }
  111. #define GET_sq_slave_vm_INSTANCE(v, idx) \
  112. SlaveVM_st *self=NULL; HSQUIRRELVM svm; \
  113. if(get_slave_vm_instance(v, idx, &self) != SQ_OK) return SQ_ERROR; \
  114. svm = self->v;
  115. static SQRESULT sq_slave_vm__tostring (HSQUIRRELVM v)
  116. {
  117. GET_sq_slave_vm_INSTANCE(v, 1);
  118. sq_pushfstring (v, _SC("Squirrel vm (%p)"), svm);
  119. return 1;
  120. }
  121. #ifdef SLAVE_VM_WITH_OS_THREADS
  122. static int releaseSlaveThread(SlaveVM_st *self)
  123. {
  124. if(self->isRunningAsThread)
  125. {
  126. mg_thread_cond_destroy(&self->cond_var);
  127. mg_thread_mutex_destroy(&self->mutex);
  128. }
  129. return 0;
  130. }
  131. #endif // SLAVE_VM_WITH_OS_THREADS
  132. static SQRESULT sq_slave_vm_release_hook(SQUserPointer p, SQInteger size, void */*ep*/)
  133. {
  134. SlaveVM_st *self = (SlaveVM_st*)p;
  135. if(self && self->v)
  136. {
  137. #ifdef SLAVE_VM_WITH_OS_THREADS
  138. releaseSlaveThread(self);
  139. #endif // SLAVE_VM_WITH_OS_THREADS
  140. sq_close(self->v);
  141. self->v = NULL;
  142. sq_free(self, sizeof(*self));
  143. }
  144. return 0;
  145. }
  146. #ifdef SLAVE_VM_WITH_OS_THREADS
  147. #define SQ_getforeignptr_slave(v) \
  148. SlaveVM_st *self = (SlaveVM_st*)sq_getforeignptr(v);\
  149. if(!self) return sq_throwerror(v, _SC("No slavevm foreignptr found"));
  150. static SQRESULT sq_vm_thread_lock(HSQUIRRELVM v)
  151. {
  152. SQ_getforeignptr_slave(v);
  153. sq_pushinteger(v, mg_thread_mutex_lock(&self->mutex));
  154. return 1;
  155. }
  156. /*
  157. static SQRESULT sq_vm_thread_unlock(HSQUIRRELVM v)
  158. {
  159. SQ_getforeignptr_slave(v);
  160. sq_pushinteger(v, mg_thread_mutex_unlock(&self->mutex));
  161. return 1;
  162. }
  163. */
  164. static SQRESULT sq_vm_thread_cond_wait(HSQUIRRELVM v)
  165. {
  166. SQ_getforeignptr_slave(v);
  167. sq_pushinteger(v, mg_thread_cond_wait(&self->cond_var, &self->mutex));
  168. return 1;
  169. }
  170. static SQRESULT sq_vm_thread_master_state(HSQUIRRELVM v)
  171. {
  172. SQ_getforeignptr_slave(v);
  173. sq_pushinteger(v, self->master_state);
  174. return 1;
  175. }
  176. static SQRESULT sq_vm_thread_slave_state(HSQUIRRELVM v)
  177. {
  178. SQ_FUNC_VARS(v);
  179. SQ_getforeignptr_slave(v);
  180. SQ_OPT_INTEGER(v, 2, state, 0);
  181. sq_pushinteger(v, self->slave_state);
  182. if(sq_gettop(v) > 1) self->slave_state = state;
  183. return 1;
  184. }
  185. #endif // SLAVE_VM_WITH_OS_THREADS
  186. /*
  187. ** Creates a new SQuirrel vm.
  188. */
  189. static SQRESULT sq_slave_vm_constructor (HSQUIRRELVM v)
  190. {
  191. SQ_FUNC_VARS(v);
  192. SQ_OPT_INTEGER(v, 2, stack_size, 1024);
  193. SQ_OPT_BOOL(v, 3, with_libraries, false);
  194. SQ_OPT_BOOL(v, 4, run_as_thread, false);
  195. HSQUIRRELVM self = sq_open(stack_size);
  196. /* Initialize environment */
  197. sq_setprintfunc(self,sq_getprintfunc(v),sq_geterrorfunc(v));
  198. if(with_libraries)
  199. {
  200. /* load base libraries */
  201. sq_pushroottable(self);
  202. sqstd_register_bloblib(self);
  203. sqstd_register_iolib(self);
  204. sqstd_register_systemlib(self);
  205. sqstd_register_mathlib(self);
  206. sqstd_register_stringlib(self);
  207. sq_poptop(self); //remove root table
  208. }
  209. SlaveVM_st *svm_st = (SlaveVM_st *)sq_malloc(sizeof(SlaveVM_st));
  210. memset(svm_st, 0, sizeof(*svm_st));
  211. #ifdef SLAVE_VM_WITH_OS_THREADS
  212. sq_setforeignptr(self, svm_st);
  213. if(run_as_thread)
  214. {
  215. svm_st->isRunningAsThread = true;
  216. svm_st->master_vm = v;
  217. mg_thread_mutex_init(&svm_st->mutex, NULL);
  218. mg_thread_cond_init(&svm_st->cond_var, NULL);
  219. sq_pushroottable(self);
  220. //sq_insertfunc(self, _SC("slavevm_thread_lock"), sq_vm_thread_lock, 1, _SC("."), SQFalse);
  221. //sq_insertfunc(self, _SC("slavevm_unlock"), sq_vm_thread_unlock, 1, _SC("."), SQFalse);
  222. sq_insertfunc(self, _SC("slavevm_thread_cond_wait"), sq_vm_thread_cond_wait, 1, _SC("."), SQFalse);
  223. sq_insertfunc(self, _SC("slavevm_thread_master_state"), sq_vm_thread_master_state, 1, _SC("."), SQFalse);
  224. sq_insertfunc(self, _SC("slavevm_thread_slave_state"), sq_vm_thread_slave_state, -1, _SC(".i"), SQFalse);
  225. sq_poptop(self); //remove root table
  226. }
  227. #endif // SLAVE_VM_WITH_OS_THREADS
  228. svm_st->v = self;
  229. svm_st->master_vm = v;
  230. sq_setinstanceup(v, 1, svm_st);
  231. sq_setreleasehook(v, 1, sq_slave_vm_release_hook);
  232. return 1;
  233. }
  234. static SQRESULT sq_slave_vm_preload_lib (HSQUIRRELVM v)
  235. {
  236. SQ_FUNC_VARS_NO_TOP(v);
  237. GET_sq_slave_vm_INSTANCE(v, 1);
  238. SQ_GET_STRING(v, 2, module_name);
  239. SQFUNCTION sq_func = sq_get_preload_module_func(v, module_name);
  240. SQBool rc = sq_func != NULL;
  241. if(rc)
  242. {
  243. sq_pushroottable(svm);
  244. sq_func(svm);
  245. sq_poptop(svm); //remove root table
  246. }
  247. sq_pushbool(v, rc);
  248. return 1;
  249. }
  250. #ifdef SLAVE_VM_WITH_OS_THREADS
  251. static SQRESULT checkTryLock(HSQUIRRELVM v, SlaveVM_st *self)
  252. {
  253. if(self->isRunningAsThread && mg_thread_mutex_trylock(&self->mutex))
  254. {
  255. return sq_throwerror(v, _SC("Could not aquire slave lock"));
  256. }
  257. return SQ_OK;
  258. }
  259. #endif // SLAVE_VM_WITH_OS_THREADS
  260. static SQRESULT sq_slave_vm_close(HSQUIRRELVM v)
  261. {
  262. GET_sq_slave_vm_INSTANCE(v, 1);
  263. #ifdef SLAVE_VM_WITH_OS_THREADS
  264. if(checkTryLock(v, self)) return SQ_ERROR;
  265. releaseSlaveThread(self);
  266. #endif // SLAVE_VM_WITH_OS_THREADS
  267. sq_close(svm);
  268. sq_setinstanceup(v, 1, 0); //next calls will fail with "vm is closed"
  269. return 0;
  270. }
  271. static SQRESULT sq_slave_vm_set_include_path(HSQUIRRELVM v)
  272. {
  273. SQ_FUNC_VARS_NO_TOP(v);
  274. GET_sq_slave_vm_INSTANCE(v, 1);
  275. #ifdef SLAVE_VM_WITH_OS_THREADS
  276. if(checkTryLock(v, self)) return SQ_ERROR;
  277. releaseSlaveThread(self);
  278. #endif // SLAVE_VM_WITH_OS_THREADS
  279. SQ_GET_STRING(v, 2, include_path);
  280. sq_set_include_path(svm, include_path);
  281. return 0;
  282. }
  283. static SQRESULT sq_slave_vm_get_include_path(HSQUIRRELVM v)
  284. {
  285. SQ_FUNC_VARS_NO_TOP(v);
  286. GET_sq_slave_vm_INSTANCE(v, 1);
  287. #ifdef SLAVE_VM_WITH_OS_THREADS
  288. if(checkTryLock(v, self)) return SQ_ERROR;
  289. releaseSlaveThread(self);
  290. #endif // SLAVE_VM_WITH_OS_THREADS
  291. const SQChar *include_path = sq_get_include_path(svm);
  292. if(include_path) sq_pushstring(v, include_path, -1);
  293. else sq_pushnull(v);
  294. return 1;
  295. }
  296. static SQRESULT sq_slave_vm_call(HSQUIRRELVM v)
  297. {
  298. SQ_FUNC_VARS_NO_TOP(v);
  299. GET_sq_slave_vm_INSTANCE(v, 1);
  300. SQ_GET_BOOL(v, 2, has_ret_val);
  301. SQ_GET_STRING(v, 3, func_name);
  302. #ifdef SLAVE_VM_WITH_OS_THREADS
  303. if(checkTryLock(v, self)) return SQ_ERROR;
  304. #endif // SLAVE_VM_WITH_OS_THREADS
  305. SQInteger top = sq_gettop(svm);
  306. SQRESULT result = SQ_ERROR;
  307. sq_pushroottable(svm);
  308. sq_pushstring(svm, func_name, func_name_size);
  309. if(sq_get(svm, -2) == SQ_OK)
  310. {
  311. switch(sq_gettype(svm, -1))
  312. {
  313. case OT_CLOSURE:
  314. case OT_NATIVECLOSURE:
  315. case OT_CLASS:
  316. {
  317. sq_pushroottable(svm);
  318. int argc = sq_gettop(v) - 3;
  319. if(argc && copy_values_between_vms(svm, v, argc, 4) != SQ_OK)
  320. {
  321. result = sq_throwerror(v, sq_getlasterror_str(svm));
  322. goto cleanup;
  323. }
  324. if(sq_call(svm, argc+1, has_ret_val, SQFalse) != SQ_OK)
  325. {
  326. result = sq_throwerror(v, sq_getlasterror_str(svm));
  327. goto cleanup;
  328. }
  329. if(has_ret_val)
  330. {
  331. if(copy_values_between_vms(v, svm, 1, sq_gettop(svm)) == SQ_OK) result = 1;
  332. }
  333. else result = SQ_OK;
  334. }
  335. }
  336. }
  337. else
  338. {
  339. result = sq_throwerror(v, sq_getlasterror_str(svm));
  340. }
  341. cleanup:
  342. sq_settop(svm, top);
  343. #ifdef SLAVE_VM_WITH_OS_THREADS
  344. if(self->isRunningAsThread) mg_thread_mutex_unlock(&self->mutex);
  345. #endif // SLAVE_VM_WITH_OS_THREADS
  346. return result;
  347. }
  348. static SQRESULT sq_slave_vm_set(HSQUIRRELVM v)
  349. {
  350. GET_sq_slave_vm_INSTANCE(v, 1);
  351. #ifdef SLAVE_VM_WITH_OS_THREADS
  352. if(checkTryLock(v, self)) return SQ_ERROR;
  353. #endif // SLAVE_VM_WITH_OS_THREADS
  354. SQInteger top = sq_gettop(svm);
  355. SQRESULT result;
  356. sq_pushroottable(svm);
  357. if(copy_values_between_vms(svm, v, 1, 2) == SQ_OK
  358. && copy_values_between_vms(svm, v, 1, 3) == SQ_OK)
  359. {
  360. result = sq_newslot(svm, -3, SQFalse);
  361. if(result == SQ_ERROR) sq_throwerror(v, sq_getlasterror_str(svm));
  362. }
  363. else result = SQ_ERROR;
  364. sq_settop(svm, top);
  365. #ifdef SLAVE_VM_WITH_OS_THREADS
  366. if(self->isRunningAsThread) mg_thread_mutex_unlock(&self->mutex);
  367. #endif // SLAVE_VM_WITH_OS_THREADS
  368. return result;
  369. }
  370. static SQRESULT sq_slave_vm_get(HSQUIRRELVM v)
  371. {
  372. GET_sq_slave_vm_INSTANCE(v, 1);
  373. #ifdef SLAVE_VM_WITH_OS_THREADS
  374. if(checkTryLock(v, self)) return SQ_ERROR;
  375. #endif // SLAVE_VM_WITH_OS_THREADS
  376. SQInteger top = sq_gettop(svm);
  377. SQRESULT result = SQ_ERROR;
  378. sq_pushroottable(svm);
  379. if(copy_values_between_vms(svm, v, 1, 2) == SQ_OK)
  380. {
  381. if(sq_get(svm, -2) == SQ_OK
  382. && copy_values_between_vms(v, svm, 1, sq_gettop(svm)) == SQ_OK)
  383. {
  384. result = 1;
  385. }
  386. else
  387. {
  388. if(sq_gettop(v) == 3) result = 1; //we have a default value
  389. else sq_throwerror(v, sq_getlasterror_str(svm));
  390. }
  391. }
  392. sq_settop(svm, top);
  393. #ifdef SLAVE_VM_WITH_OS_THREADS
  394. if(self->isRunningAsThread) mg_thread_mutex_unlock(&self->mutex);
  395. #endif // SLAVE_VM_WITH_OS_THREADS
  396. return result;
  397. }
  398. static SQRESULT sq_slave_vm_dofile(HSQUIRRELVM v)
  399. {
  400. SQ_FUNC_VARS(v);
  401. GET_sq_slave_vm_INSTANCE(v, 1);
  402. SQ_GET_STRING(v, 2, file_name);
  403. SQ_OPT_BOOL(v, 3, retval, false);
  404. SQ_OPT_BOOL(v, 4, printerror, false);
  405. SQ_OPT_BOOL(v, 5, show_warnings, false);
  406. #ifdef SLAVE_VM_WITH_OS_THREADS
  407. if(checkTryLock(v, self)) return SQ_ERROR;
  408. #endif // SLAVE_VM_WITH_OS_THREADS
  409. SQInteger top = sq_gettop(svm);
  410. SQRESULT result = SQ_ERROR;
  411. sq_pushroottable(svm); //important always push the root table, because sqstd_dofile will try to do a sq_push(v, -2)
  412. if(sqstd_dofile(svm, file_name, retval, printerror, show_warnings) >= 0)
  413. {
  414. if(retval)
  415. {
  416. if(copy_values_between_vms(v, svm, 1, sq_gettop(svm)) == SQ_OK) result = 1;
  417. }
  418. else result = SQ_OK;
  419. }
  420. else sq_throwerror(v, sq_getlasterror_str(svm));
  421. sq_settop(svm, top);
  422. #ifdef SLAVE_VM_WITH_OS_THREADS
  423. if(self->isRunningAsThread) mg_thread_mutex_unlock(&self->mutex);
  424. #endif // SLAVE_VM_WITH_OS_THREADS
  425. return result;
  426. }
  427. static SQRESULT sq_slave_vm_loadfile(HSQUIRRELVM v)
  428. {
  429. SQ_FUNC_VARS(v);
  430. GET_sq_slave_vm_INSTANCE(v, 1);
  431. SQ_GET_STRING(v, 2, func_name);
  432. SQ_GET_STRING(v, 3, file_name);
  433. SQ_OPT_BOOL(v, 4, printerror, false);
  434. SQ_OPT_BOOL(v, 5, show_warnings, false);
  435. #ifdef SLAVE_VM_WITH_OS_THREADS
  436. if(checkTryLock(v, self)) return SQ_ERROR;
  437. #endif // SLAVE_VM_WITH_OS_THREADS
  438. SQInteger top = sq_gettop(svm);
  439. SQRESULT result = SQ_ERROR;
  440. sq_pushroottable(svm);
  441. sq_pushstring(svm, func_name, func_name_size);
  442. if(sqstd_loadfile(svm, file_name, printerror, show_warnings) >= 0)
  443. {
  444. result = sq_newslot(svm, -3, SQFalse);
  445. }
  446. sq_settop(svm, top);
  447. #ifdef SLAVE_VM_WITH_OS_THREADS
  448. if(self->isRunningAsThread) mg_thread_mutex_unlock(&self->mutex);
  449. #endif // SLAVE_VM_WITH_OS_THREADS
  450. return result;
  451. }
  452. static SQRESULT sq_slave_vm_compilestring(HSQUIRRELVM v)
  453. {
  454. SQ_FUNC_VARS(v);
  455. GET_sq_slave_vm_INSTANCE(v, 1);
  456. SQ_GET_STRING(v, 2, func_name);
  457. SQ_GET_STRING(v, 3, str_script);
  458. SQ_OPT_BOOL(v, 4, printerror, false);
  459. SQ_OPT_BOOL(v, 5, show_warnings, false);
  460. SQ_OPT_INTEGER(v, 6, max_nested_includes, 0);
  461. #ifdef SLAVE_VM_WITH_OS_THREADS
  462. if(checkTryLock(v, self)) return SQ_ERROR;
  463. #endif // SLAVE_VM_WITH_OS_THREADS
  464. SQInteger top = sq_gettop(svm);
  465. SQRESULT result = SQ_ERROR;
  466. sq_pushroottable(svm);
  467. sq_pushstring(svm, func_name, func_name_size);
  468. if(sq_compilebuffer(svm, str_script, str_script_size, func_name, printerror, show_warnings, max_nested_includes) >= 0)
  469. {
  470. result = sq_newslot(svm, -3, SQFalse);
  471. }
  472. else
  473. {
  474. const SQChar *last_error = sq_getlasterror_str(svm);
  475. if(last_error) {
  476. SQInteger line, column;
  477. sq_getlasterror_line_col(svm, &line, &column);
  478. result = sq_throwerror(v, _SC("compilestring %s %d:%d: %s"), func_name, line, column, last_error);
  479. }
  480. }
  481. sq_settop(svm, top);
  482. #ifdef SLAVE_VM_WITH_OS_THREADS
  483. if(self->isRunningAsThread) mg_thread_mutex_unlock(&self->mutex);
  484. #endif // SLAVE_VM_WITH_OS_THREADS
  485. return result;
  486. }
  487. extern SQInteger _g_io_dostring(HSQUIRRELVM v);
  488. static SQRESULT sq_slave_vm_dostring(HSQUIRRELVM v)
  489. {
  490. SQ_FUNC_VARS(v);
  491. GET_sq_slave_vm_INSTANCE(v, 1);
  492. SQ_GET_STRING(v, 2, str);
  493. SQ_OPT_BOOL(v, 3, retval, false);
  494. #ifdef SLAVE_VM_WITH_OS_THREADS
  495. if(checkTryLock(v, self)) return SQ_ERROR;
  496. #endif // SLAVE_VM_WITH_OS_THREADS
  497. SQInteger top = sq_gettop(svm);
  498. sq_pushroottable(svm); //important always push the root table, because sqstd_dofile will try to do a sq_push(v, -2)
  499. sq_pushstring(svm, str, str_size);
  500. sq_pushbool(svm, retval);
  501. SQRESULT result = _g_io_dostring(svm);
  502. if(result == SQ_ERROR) sq_settop(svm, top);
  503. #ifdef SLAVE_VM_WITH_OS_THREADS
  504. if(self->isRunningAsThread) mg_thread_mutex_unlock(&self->mutex);
  505. #endif // SLAVE_VM_WITH_OS_THREADS
  506. return result;
  507. }
  508. static SQRESULT sq_slave_vm_is_thread_idle(HSQUIRRELVM v)
  509. {
  510. #ifdef SLAVE_VM_WITH_OS_THREADS
  511. GET_sq_slave_vm_INSTANCE(v, 1);
  512. int rc = mg_thread_mutex_trylock(&self->mutex) == 0;
  513. if(rc) mg_thread_mutex_unlock(&self->mutex);
  514. sq_pushbool(v, rc);
  515. #else
  516. sq_pushbool(v, SQTrue);
  517. #endif
  518. return 1;
  519. }
  520. static SQRESULT sq_slave_vm_is_runing_as_thread(HSQUIRRELVM v)
  521. {
  522. #ifdef SLAVE_VM_WITH_OS_THREADS
  523. GET_sq_slave_vm_INSTANCE(v, 1);
  524. sq_pushbool(v, self->isRunningAsThread);
  525. #else
  526. sq_pushbool(v, SQFalse);
  527. #endif
  528. return 1;
  529. }
  530. #ifdef SLAVE_VM_WITH_OS_THREADS
  531. #define check_run_as_thread(v) \
  532. if(!self->isRunningAsThread) return sq_throwerror(v, _SC("Slave VM not running as thread"))
  533. static void *slaveThreadTask(void *p)
  534. {
  535. SlaveVM_st *self = (SlaveVM_st*)p;
  536. self->thread_id = mg_thread_self();
  537. HSQUIRRELVM svm = self->v;
  538. int rc;
  539. SQInteger top, argc;
  540. rc = sq_getinteger(svm, -1, &top) == SQ_OK;
  541. if(rc)
  542. {
  543. rc = sq_getinteger(svm, -2, &argc) == SQ_OK;
  544. if(rc)
  545. {
  546. sq_pop(svm, 2); //remove top & argc
  547. rc = sq_call(svm, argc+1, SQFalse, SQFalse);
  548. }
  549. sq_settop(svm, top);
  550. }
  551. rc = mg_thread_mutex_unlock(&self->mutex);
  552. self->thread_id = 0;
  553. //printf("%d:%d:%s\n", __LINE__, rc, __FILE__);
  554. return NULL;
  555. }
  556. extern void sq_system_sleep(int n);
  557. static SQRESULT sq_slave_vm_thread_run(HSQUIRRELVM v)
  558. {
  559. GET_sq_slave_vm_INSTANCE(v, 1);
  560. check_run_as_thread(v);
  561. int rc;
  562. const SQChar *func_name;
  563. SQInteger func_name_size;
  564. if(self->thread_id) return sq_throwerror(v, _SC("slavevm already running in a thread"));
  565. SQInteger top = sq_gettop(svm);
  566. rc = mg_thread_mutex_trylock(&self->mutex) == 0;
  567. if(rc)
  568. {
  569. sq_getstr_and_size(v, 2, &func_name, &func_name_size);
  570. sq_pushstring(svm, func_name, func_name_size);
  571. sq_getonroottable(svm);
  572. //printf("%d:%s\n", __LINE__, __FILE__);
  573. rc = (sq_gettype(svm, -1) == OT_CLOSURE);
  574. if( rc )
  575. {
  576. //printf("%d:%s\n", __LINE__, __FILE__);
  577. sq_pushroottable(svm);
  578. int argc = sq_gettop(v) - 2;
  579. rc = ( (argc == 0) || (copy_values_between_vms(svm, v, argc, 3) == SQ_OK));
  580. if(rc)
  581. {
  582. sq_pushinteger(svm, argc);
  583. sq_pushinteger(svm, top);
  584. rc = mg_start_thread(slaveThreadTask, self) == 0;
  585. if(rc) goto done;
  586. }
  587. //printf("%d:%d:%s\n", __LINE__, rc, __FILE__);
  588. }
  589. sq_settop(svm, top); //something went wrong reset slave stack
  590. }
  591. done:
  592. sq_pushbool(v, rc);
  593. return 1;
  594. }
  595. static SQRESULT sq_slave_vm_thread_trylock(HSQUIRRELVM v)
  596. {
  597. GET_sq_slave_vm_INSTANCE(v, 1);
  598. check_run_as_thread(v);
  599. sq_pushinteger(v, mg_thread_mutex_trylock(&self->mutex));
  600. return 1;
  601. }
  602. static SQRESULT sq_slave_vm_thread_unlock(HSQUIRRELVM v)
  603. {
  604. GET_sq_slave_vm_INSTANCE(v, 1);
  605. check_run_as_thread(v);
  606. sq_pushinteger(v, mg_thread_mutex_unlock(&self->mutex));
  607. return 1;
  608. }
  609. static SQRESULT sq_slave_vm_thread_cond_signal(HSQUIRRELVM v)
  610. {
  611. GET_sq_slave_vm_INSTANCE(v, 1);
  612. check_run_as_thread(v);
  613. sq_pushinteger(v, mg_thread_cond_signal(&self->cond_var));
  614. return 1;
  615. }
  616. static SQRESULT sq_slave_vm_thread_master_state(HSQUIRRELVM v)
  617. {
  618. SQ_FUNC_VARS(v);
  619. GET_sq_slave_vm_INSTANCE(v, 1);
  620. check_run_as_thread(v);
  621. SQ_OPT_INTEGER(v, 2, state, 0);
  622. sq_pushinteger(v, self->master_state);
  623. if(sq_gettop(v) > 1) self->master_state = state;
  624. return 1;
  625. }
  626. static SQRESULT sq_slave_vm_thread_slave_state(HSQUIRRELVM v)
  627. {
  628. GET_sq_slave_vm_INSTANCE(v, 1);
  629. check_run_as_thread(v);
  630. sq_pushinteger(v, self->slave_state);
  631. return 1;
  632. }
  633. #endif // SLAVE_VM_WITH_OS_THREADS
  634. #ifdef __cplusplus
  635. extern "C" {
  636. #endif
  637. SQRESULT sqext_register_sq_slave_vm(HSQUIRRELVM v)
  638. {
  639. const SQChar get_set_validation_mask[] = _SC("x s|n|p s|n|b|a|t|p|o");
  640. sq_pushstring(v,sq_slave_vm_TAG, -1);
  641. sq_newclass(v, SQFalse);
  642. sq_settypetag(v,-1,(void*)sq_slave_vm_TAG);
  643. sq_insertfunc(v, _SC("constructor"), sq_slave_vm_constructor, -1, _SC("xibb"), SQFalse);
  644. sq_insertfunc(v, _SC("_tostring"), sq_slave_vm__tostring, 1, _SC("x"), SQFalse);
  645. sq_insertfunc(v, _SC("close"), sq_slave_vm_close, 1, _SC("x"), SQFalse);
  646. sq_insertfunc(v, _SC("set_include_path"), sq_slave_vm_set_include_path, 2, _SC("xs"), SQFalse);
  647. sq_insertfunc(v, _SC("get_include_path"), sq_slave_vm_get_include_path, 1, _SC("x"), SQFalse);
  648. sq_insertfunc(v, _SC("set"), sq_slave_vm_set, 3, get_set_validation_mask, SQFalse);
  649. sq_insertfunc(v, _SC("_set"), sq_slave_vm_set, 3, get_set_validation_mask, SQFalse);
  650. sq_insertfunc(v, _SC("get"), sq_slave_vm_get, -2, get_set_validation_mask, SQFalse);
  651. sq_insertfunc(v, _SC("_get"), sq_slave_vm_get, -2, get_set_validation_mask, SQFalse);
  652. sq_insertfunc(v, _SC("dofile"), sq_slave_vm_dofile, -2, _SC("xsbbb"), SQFalse);
  653. sq_insertfunc(v, _SC("loadfile"), sq_slave_vm_loadfile, -3, _SC("xssbb"), SQFalse);
  654. sq_insertfunc(v, _SC("dostring"), sq_slave_vm_dostring, -2, _SC("xsb"), SQFalse);
  655. sq_insertfunc(v, _SC("compilestring"), sq_slave_vm_compilestring, -3, _SC("xssbbi"), SQFalse);
  656. sq_insertfunc(v, _SC("call"), sq_slave_vm_call, -3, _SC("xbs"), SQFalse);
  657. sq_insertfunc(v, _SC("is_thread_idle"), sq_slave_vm_is_thread_idle, 1, _SC("x"), SQFalse);
  658. sq_insertfunc(v, _SC("is_runing_as_thread"), sq_slave_vm_is_runing_as_thread, 1, _SC("x"), SQFalse);
  659. #ifdef SLAVE_VM_WITH_OS_THREADS
  660. sq_insertfunc(v, _SC("thread_run"), sq_slave_vm_thread_run, -2, _SC("xs"), SQFalse);
  661. sq_insertfunc(v, _SC("thread_trylock"), sq_slave_vm_thread_trylock, 1, _SC("x"), SQFalse);
  662. sq_insertfunc(v, _SC("thread_unlock"), sq_slave_vm_thread_unlock, 1, _SC("x"), SQFalse);
  663. sq_insertfunc(v, _SC("thread_cond_signal"), sq_slave_vm_thread_cond_signal, 1, _SC("x"), SQFalse);
  664. sq_insertfunc(v, _SC("thread_master_state"), sq_slave_vm_thread_master_state, -1, _SC("xi"), SQFalse);
  665. sq_insertfunc(v, _SC("thread_slave_state"), sq_slave_vm_thread_slave_state, 1, _SC("x"), SQFalse);
  666. #endif // SLAVE_VM_WITH_OS_THREADS
  667. sq_insertfunc(v, _SC("preload_lib"), sq_slave_vm_preload_lib, 2, _SC("xs"), SQFalse);
  668. sq_newslot(v,-3,SQTrue); //push sq_slave_vm class
  669. return 0;
  670. }
  671. #ifdef __cplusplus
  672. }
  673. #endif