thread_manager.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "thread_manager.h"
  6. #include "../common/wasm_c_api_internal.h"
  7. #if WASM_ENABLE_INTERP != 0
  8. #include "../interpreter/wasm_runtime.h"
  9. #endif
  10. #if WASM_ENABLE_AOT != 0
  11. #include "../aot/aot_runtime.h"
  12. #endif
  13. #if WASM_ENABLE_DEBUG_INTERP != 0
  14. #include "debug_engine.h"
  15. #endif
  16. typedef struct {
  17. bh_list_link l;
  18. void (*destroy_cb)(WASMCluster *);
  19. } DestroyCallBackNode;
  20. static bh_list destroy_callback_list_head;
  21. static bh_list *const destroy_callback_list = &destroy_callback_list_head;
  22. static bh_list cluster_list_head;
  23. static bh_list *const cluster_list = &cluster_list_head;
  24. static korp_mutex cluster_list_lock;
  25. static korp_mutex _exception_lock;
  26. typedef void (*list_visitor)(void *, void *);
  27. static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM;
  28. /* Set the maximum thread number, if this function is not called,
  29. the max thread num is defined by CLUSTER_MAX_THREAD_NUM */
  30. void
  31. wasm_cluster_set_max_thread_num(uint32 num)
  32. {
  33. if (num > 0)
  34. cluster_max_thread_num = num;
  35. }
  36. bool
  37. thread_manager_init()
  38. {
  39. if (bh_list_init(cluster_list) != 0)
  40. return false;
  41. if (os_mutex_init(&cluster_list_lock) != 0)
  42. return false;
  43. if (os_mutex_init(&_exception_lock) != 0) {
  44. os_mutex_destroy(&cluster_list_lock);
  45. return false;
  46. }
  47. return true;
  48. }
  49. void
  50. thread_manager_destroy()
  51. {
  52. WASMCluster *cluster = bh_list_first_elem(cluster_list);
  53. WASMCluster *next;
  54. while (cluster) {
  55. next = bh_list_elem_next(cluster);
  56. wasm_cluster_destroy(cluster);
  57. cluster = next;
  58. }
  59. wasm_cluster_cancel_all_callbacks();
  60. os_mutex_destroy(&_exception_lock);
  61. os_mutex_destroy(&cluster_list_lock);
  62. }
  63. static void
  64. traverse_list(bh_list *l, list_visitor visitor, void *user_data)
  65. {
  66. void *next, *node = bh_list_first_elem(l);
  67. while (node) {
  68. next = bh_list_elem_next(node);
  69. visitor(node, user_data);
  70. node = next;
  71. }
  72. }
  73. /* Assumes cluster->lock is locked */
  74. static bool
  75. safe_traverse_exec_env_list(WASMCluster *cluster, list_visitor visitor,
  76. void *user_data)
  77. {
  78. Vector proc_nodes;
  79. void *node;
  80. bool ret = true;
  81. if (!bh_vector_init(&proc_nodes, cluster->exec_env_list.len, sizeof(void *),
  82. false)) {
  83. ret = false;
  84. goto final;
  85. }
  86. node = bh_list_first_elem(&cluster->exec_env_list);
  87. while (node) {
  88. bool already_processed = false;
  89. void *proc_node;
  90. uint32 i;
  91. for (i = 0; i < (uint32)bh_vector_size(&proc_nodes); i++) {
  92. if (!bh_vector_get(&proc_nodes, i, &proc_node)) {
  93. ret = false;
  94. goto final;
  95. }
  96. if (proc_node == node) {
  97. already_processed = true;
  98. break;
  99. }
  100. }
  101. if (already_processed) {
  102. node = bh_list_elem_next(node);
  103. continue;
  104. }
  105. os_mutex_unlock(&cluster->lock);
  106. visitor(node, user_data);
  107. os_mutex_lock(&cluster->lock);
  108. if (!bh_vector_append(&proc_nodes, &node)) {
  109. ret = false;
  110. goto final;
  111. }
  112. node = bh_list_first_elem(&cluster->exec_env_list);
  113. }
  114. final:
  115. bh_vector_destroy(&proc_nodes);
  116. return ret;
  117. }
  118. /* The caller must not have any locks */
  119. bool
  120. wasm_cluster_allocate_aux_stack(WASMExecEnv *exec_env, uint64 *p_start,
  121. uint32 *p_size)
  122. {
  123. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  124. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
  125. WASMModuleInstanceCommon *module_inst =
  126. wasm_exec_env_get_module_inst(exec_env);
  127. uint64 stack_end;
  128. stack_end = wasm_runtime_module_malloc_internal(module_inst, exec_env,
  129. cluster->stack_size, NULL);
  130. *p_start = stack_end + cluster->stack_size;
  131. *p_size = cluster->stack_size;
  132. return stack_end != 0;
  133. #else
  134. uint32 i;
  135. /* If the module doesn't have aux stack info,
  136. it can't create any threads */
  137. os_mutex_lock(&cluster->lock);
  138. if (!cluster->stack_segment_occupied) {
  139. os_mutex_unlock(&cluster->lock);
  140. return false;
  141. }
  142. for (i = 0; i < cluster_max_thread_num; i++) {
  143. if (!cluster->stack_segment_occupied[i]) {
  144. if (p_start)
  145. *p_start = cluster->stack_tops[i];
  146. if (p_size)
  147. *p_size = cluster->stack_size;
  148. cluster->stack_segment_occupied[i] = true;
  149. os_mutex_unlock(&cluster->lock);
  150. return true;
  151. }
  152. }
  153. os_mutex_unlock(&cluster->lock);
  154. return false;
  155. #endif
  156. }
  157. /* The caller must not have any locks */
  158. bool
  159. wasm_cluster_free_aux_stack(WASMExecEnv *exec_env, uint64 start)
  160. {
  161. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  162. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
  163. WASMModuleInstanceCommon *module_inst =
  164. wasm_exec_env_get_module_inst(exec_env);
  165. if (!wasm_exec_env_is_aux_stack_managed_by_runtime(exec_env)) {
  166. return true;
  167. }
  168. bh_assert(start >= cluster->stack_size);
  169. wasm_runtime_module_free_internal(module_inst, exec_env,
  170. start - cluster->stack_size);
  171. return true;
  172. #else
  173. uint32 i;
  174. os_mutex_lock(&cluster->lock);
  175. for (i = 0; i < cluster_max_thread_num; i++) {
  176. if (start == cluster->stack_tops[i]) {
  177. cluster->stack_segment_occupied[i] = false;
  178. os_mutex_unlock(&cluster->lock);
  179. return true;
  180. }
  181. }
  182. os_mutex_unlock(&cluster->lock);
  183. return false;
  184. #endif
  185. }
  186. WASMCluster *
  187. wasm_cluster_create(WASMExecEnv *exec_env)
  188. {
  189. WASMCluster *cluster;
  190. uint32 aux_stack_size;
  191. uint64 aux_stack_start;
  192. bh_assert(exec_env->cluster == NULL);
  193. if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
  194. LOG_ERROR("thread manager error: failed to allocate memory");
  195. return NULL;
  196. }
  197. memset(cluster, 0, sizeof(WASMCluster));
  198. exec_env->cluster = cluster;
  199. bh_list_init(&cluster->exec_env_list);
  200. bh_list_insert(&cluster->exec_env_list, exec_env);
  201. if (os_mutex_init(&cluster->lock) != 0) {
  202. wasm_runtime_free(cluster);
  203. LOG_ERROR("thread manager error: failed to init mutex");
  204. return NULL;
  205. }
  206. /* Prepare the aux stack top and size for every thread */
  207. if (!wasm_exec_env_get_aux_stack(exec_env, &aux_stack_start,
  208. &aux_stack_size)) {
  209. #if WASM_ENABLE_LIB_WASI_THREADS == 0
  210. LOG_VERBOSE("No aux stack info for this module, can't create thread");
  211. #endif
  212. /* If the module don't have aux stack info, don't throw error here,
  213. but remain stack_tops and stack_segment_occupied as NULL */
  214. os_mutex_lock(&cluster_list_lock);
  215. if (bh_list_insert(cluster_list, cluster) != 0) {
  216. os_mutex_unlock(&cluster_list_lock);
  217. goto fail;
  218. }
  219. os_mutex_unlock(&cluster_list_lock);
  220. return cluster;
  221. }
  222. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
  223. cluster->stack_size = aux_stack_size;
  224. #else
  225. cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
  226. if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
  227. goto fail;
  228. }
  229. /* Make stack size 16-byte aligned */
  230. cluster->stack_size = cluster->stack_size & (~15);
  231. #endif
  232. /* Set initial aux stack top to the instance and
  233. aux stack boundary to the main exec_env */
  234. if (!wasm_exec_env_set_aux_stack(exec_env, aux_stack_start,
  235. cluster->stack_size))
  236. goto fail;
  237. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  238. if (cluster_max_thread_num != 0) {
  239. uint64 total_size = cluster_max_thread_num * sizeof(uint64);
  240. uint32 i;
  241. if (total_size >= UINT32_MAX
  242. || !(cluster->stack_tops =
  243. wasm_runtime_malloc((uint32)total_size))) {
  244. goto fail;
  245. }
  246. memset(cluster->stack_tops, 0, (uint32)total_size);
  247. if (!(cluster->stack_segment_occupied =
  248. wasm_runtime_malloc(cluster_max_thread_num * sizeof(bool)))) {
  249. goto fail;
  250. }
  251. memset(cluster->stack_segment_occupied, 0,
  252. cluster_max_thread_num * sizeof(bool));
  253. /* Reserve space for main instance */
  254. aux_stack_start -= cluster->stack_size;
  255. for (i = 0; i < cluster_max_thread_num; i++) {
  256. cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
  257. }
  258. }
  259. #endif
  260. os_mutex_lock(&cluster_list_lock);
  261. if (bh_list_insert(cluster_list, cluster) != 0) {
  262. os_mutex_unlock(&cluster_list_lock);
  263. goto fail;
  264. }
  265. os_mutex_unlock(&cluster_list_lock);
  266. return cluster;
  267. fail:
  268. if (cluster)
  269. wasm_cluster_destroy(cluster);
  270. return NULL;
  271. }
  272. static void
  273. destroy_cluster_visitor(void *node, void *user_data)
  274. {
  275. DestroyCallBackNode *destroy_node = (DestroyCallBackNode *)node;
  276. WASMCluster *cluster = (WASMCluster *)user_data;
  277. destroy_node->destroy_cb(cluster);
  278. }
  279. void
  280. wasm_cluster_destroy(WASMCluster *cluster)
  281. {
  282. traverse_list(destroy_callback_list, destroy_cluster_visitor,
  283. (void *)cluster);
  284. /* Remove the cluster from the cluster list */
  285. os_mutex_lock(&cluster_list_lock);
  286. bh_list_remove(cluster_list, cluster);
  287. os_mutex_unlock(&cluster_list_lock);
  288. os_mutex_destroy(&cluster->lock);
  289. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  290. if (cluster->stack_tops)
  291. wasm_runtime_free(cluster->stack_tops);
  292. if (cluster->stack_segment_occupied)
  293. wasm_runtime_free(cluster->stack_segment_occupied);
  294. #endif
  295. #if WASM_ENABLE_DEBUG_INTERP != 0
  296. wasm_debug_instance_destroy(cluster);
  297. #endif
  298. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  299. bh_vector_destroy(&cluster->exception_frames);
  300. #endif
  301. wasm_runtime_free(cluster);
  302. }
  303. static void
  304. free_node_visitor(void *node, void *user_data)
  305. {
  306. wasm_runtime_free(node);
  307. }
  308. void
  309. wasm_cluster_cancel_all_callbacks()
  310. {
  311. traverse_list(destroy_callback_list, free_node_visitor, NULL);
  312. bh_list_init(destroy_callback_list);
  313. }
  314. WASMCluster *
  315. wasm_exec_env_get_cluster(WASMExecEnv *exec_env)
  316. {
  317. return exec_env->cluster;
  318. }
  319. /* The caller must lock cluster->lock */
  320. static bool
  321. wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  322. {
  323. bool ret = true;
  324. exec_env->cluster = cluster;
  325. if (cluster->exec_env_list.len == cluster_max_thread_num + 1) {
  326. LOG_ERROR("thread manager error: "
  327. "maximum number of threads exceeded");
  328. ret = false;
  329. }
  330. if (ret && bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
  331. ret = false;
  332. return ret;
  333. }
  334. static bool
  335. wasm_cluster_del_exec_env_internal(WASMCluster *cluster, WASMExecEnv *exec_env,
  336. bool can_destroy_cluster)
  337. {
  338. bool ret = true;
  339. bh_assert(exec_env->cluster == cluster);
  340. #if WASM_ENABLE_DEBUG_INTERP != 0
  341. /* Wait for debugger control thread to process the
  342. stop event of this thread */
  343. if (cluster->debug_inst) {
  344. /* lock the debug_inst->wait_lock so
  345. other threads can't fire stop events */
  346. os_mutex_lock(&cluster->debug_inst->wait_lock);
  347. while (cluster->debug_inst->stopped_thread == exec_env) {
  348. /* either wakes up by signal or by 1-second timeout */
  349. os_cond_reltimedwait(&cluster->debug_inst->wait_cond,
  350. &cluster->debug_inst->wait_lock, 1000000);
  351. }
  352. os_mutex_unlock(&cluster->debug_inst->wait_lock);
  353. }
  354. #endif
  355. if (bh_list_remove(&cluster->exec_env_list, exec_env) != 0)
  356. ret = false;
  357. if (can_destroy_cluster) {
  358. if (cluster->exec_env_list.len == 0) {
  359. /* exec_env_list empty, destroy the cluster */
  360. wasm_cluster_destroy(cluster);
  361. }
  362. }
  363. else {
  364. /* Don't destroy cluster as cluster->lock is being used */
  365. }
  366. return ret;
  367. }
  368. /* The caller should lock cluster->lock for thread safety */
  369. bool
  370. wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  371. {
  372. return wasm_cluster_del_exec_env_internal(cluster, exec_env, true);
  373. }
  374. static WASMExecEnv *
  375. wasm_cluster_search_exec_env(WASMCluster *cluster,
  376. WASMModuleInstanceCommon *module_inst)
  377. {
  378. WASMExecEnv *node = NULL;
  379. os_mutex_lock(&cluster->lock);
  380. node = bh_list_first_elem(&cluster->exec_env_list);
  381. while (node) {
  382. if (node->module_inst == module_inst) {
  383. os_mutex_unlock(&cluster->lock);
  384. return node;
  385. }
  386. node = bh_list_elem_next(node);
  387. }
  388. os_mutex_unlock(&cluster->lock);
  389. return NULL;
  390. }
  391. /* search the global cluster list to find if the given
  392. module instance have a corresponding exec_env */
  393. WASMExecEnv *
  394. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst)
  395. {
  396. WASMCluster *cluster = NULL;
  397. WASMExecEnv *exec_env = NULL;
  398. os_mutex_lock(&cluster_list_lock);
  399. cluster = bh_list_first_elem(cluster_list);
  400. while (cluster) {
  401. exec_env = wasm_cluster_search_exec_env(cluster, module_inst);
  402. if (exec_env) {
  403. os_mutex_unlock(&cluster_list_lock);
  404. return exec_env;
  405. }
  406. cluster = bh_list_elem_next(cluster);
  407. }
  408. os_mutex_unlock(&cluster_list_lock);
  409. return NULL;
  410. }
  411. WASMExecEnv *
  412. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
  413. {
  414. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  415. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  416. wasm_module_t module;
  417. wasm_module_inst_t new_module_inst;
  418. WASMExecEnv *new_exec_env;
  419. uint32 aux_stack_size;
  420. uint64 aux_stack_start;
  421. uint32 stack_size = 8192;
  422. if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
  423. return NULL;
  424. }
  425. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  426. module, module_inst, exec_env, stack_size, 0, 0, NULL, 0))) {
  427. return NULL;
  428. }
  429. /* Set custom_data to new module instance */
  430. wasm_runtime_set_custom_data_internal(
  431. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  432. wasm_native_inherit_contexts(new_module_inst, module_inst);
  433. if (!(wasm_cluster_dup_c_api_imports(new_module_inst, module_inst))) {
  434. goto fail1;
  435. }
  436. if (!wasm_cluster_allocate_aux_stack(exec_env, &aux_stack_start,
  437. &aux_stack_size)) {
  438. LOG_ERROR("thread manager error: "
  439. "failed to allocate aux stack space for new thread");
  440. goto fail1;
  441. }
  442. os_mutex_lock(&cluster->lock);
  443. if (cluster->has_exception || cluster->processing) {
  444. goto fail2;
  445. }
  446. #if WASM_ENABLE_INTERP != 0
  447. if (module_inst->module_type == Wasm_Module_Bytecode) {
  448. stack_size =
  449. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  450. }
  451. #endif
  452. #if WASM_ENABLE_AOT != 0
  453. if (module_inst->module_type == Wasm_Module_AoT) {
  454. stack_size =
  455. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  456. }
  457. #endif
  458. new_exec_env = wasm_exec_env_create_internal(new_module_inst,
  459. exec_env->wasm_stack_size);
  460. if (!new_exec_env) {
  461. goto fail2;
  462. }
  463. /* Set aux stack for current thread */
  464. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  465. aux_stack_size)) {
  466. goto fail3;
  467. }
  468. new_exec_env->is_aux_stack_allocated = true;
  469. /* Inherit suspend_flags of parent thread */
  470. new_exec_env->suspend_flags.flags =
  471. (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK);
  472. if (!wasm_cluster_add_exec_env(cluster, new_exec_env)) {
  473. goto fail3;
  474. }
  475. os_mutex_unlock(&cluster->lock);
  476. return new_exec_env;
  477. fail3:
  478. wasm_exec_env_destroy_internal(new_exec_env);
  479. fail2:
  480. os_mutex_unlock(&cluster->lock);
  481. /* free the allocated aux stack space */
  482. wasm_cluster_free_aux_stack(exec_env, aux_stack_start);
  483. fail1:
  484. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  485. return NULL;
  486. }
  487. void
  488. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  489. {
  490. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  491. wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
  492. bh_assert(cluster != NULL);
  493. WASMExecEnv *exec_env_tls = NULL;
  494. #ifdef OS_ENABLE_HW_BOUND_CHECK
  495. /* Note: free_aux_stack can execute the module's "free" function
  496. * using the specified exec_env. In case of OS_ENABLE_HW_BOUND_CHECK,
  497. * it needs to match the TLS exec_env if available. (Consider a native
  498. * function which calls wasm_cluster_destroy_spawned_exec_env.)
  499. */
  500. exec_env_tls = wasm_runtime_get_exec_env_tls();
  501. #endif
  502. if (exec_env_tls == NULL) {
  503. exec_env_tls = exec_env;
  504. }
  505. /* Free aux stack space which was allocated in
  506. wasm_cluster_spawn_exec_env */
  507. bh_assert(exec_env_tls->is_aux_stack_allocated);
  508. wasm_cluster_free_aux_stack(exec_env_tls,
  509. (uint64)exec_env->aux_stack_bottom);
  510. os_mutex_lock(&cluster->lock);
  511. /* Remove exec_env */
  512. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  513. /* Destroy exec_env */
  514. wasm_exec_env_destroy_internal(exec_env);
  515. /* Routine exit, destroy instance */
  516. wasm_runtime_deinstantiate_internal(module_inst, true);
  517. os_mutex_unlock(&cluster->lock);
  518. }
  519. /* start routine of thread manager */
  520. static void *
  521. thread_manager_start_routine(void *arg)
  522. {
  523. void *ret;
  524. WASMExecEnv *exec_env = (WASMExecEnv *)arg;
  525. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  526. WASMModuleInstanceCommon *module_inst =
  527. wasm_exec_env_get_module_inst(exec_env);
  528. bh_assert(cluster != NULL);
  529. bh_assert(module_inst != NULL);
  530. os_mutex_lock(&exec_env->wait_lock);
  531. exec_env->handle = os_self_thread();
  532. /* Notify the parent thread to continue running */
  533. os_cond_signal(&exec_env->wait_cond);
  534. os_mutex_unlock(&exec_env->wait_lock);
  535. ret = exec_env->thread_start_routine(exec_env);
  536. #ifdef OS_ENABLE_HW_BOUND_CHECK
  537. os_mutex_lock(&exec_env->wait_lock);
  538. if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags)
  539. & WASM_SUSPEND_FLAG_EXIT)
  540. ret = exec_env->thread_ret_value;
  541. os_mutex_unlock(&exec_env->wait_lock);
  542. #endif
  543. /* Routine exit */
  544. #if WASM_ENABLE_DEBUG_INTERP != 0
  545. wasm_cluster_thread_exited(exec_env);
  546. #endif
  547. /* Free aux stack space */
  548. if (exec_env->is_aux_stack_allocated)
  549. wasm_cluster_free_aux_stack(exec_env,
  550. (uint64)exec_env->aux_stack_bottom);
  551. os_mutex_lock(&cluster_list_lock);
  552. os_mutex_lock(&cluster->lock);
  553. /* Detach the native thread here to ensure the resources are freed */
  554. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  555. /* Only detach current thread when there is no other thread
  556. joining it, otherwise let the system resources for the
  557. thread be released after joining */
  558. os_thread_detach(exec_env->handle);
  559. /* No need to set exec_env->thread_is_detached to true here
  560. since we will exit soon */
  561. }
  562. #if WASM_ENABLE_PERF_PROFILING != 0
  563. os_printf("============= Spawned thread ===========\n");
  564. wasm_runtime_dump_perf_profiling(module_inst);
  565. os_printf("========================================\n");
  566. #endif
  567. /* Remove exec_env */
  568. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  569. /* Destroy exec_env */
  570. wasm_exec_env_destroy_internal(exec_env);
  571. /* Routine exit, destroy instance */
  572. wasm_runtime_deinstantiate_internal(module_inst, true);
  573. os_mutex_unlock(&cluster->lock);
  574. os_mutex_unlock(&cluster_list_lock);
  575. os_thread_exit(ret);
  576. return ret;
  577. }
  578. int32
  579. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  580. wasm_module_inst_t module_inst,
  581. bool is_aux_stack_allocated, uint64 aux_stack_start,
  582. uint32 aux_stack_size,
  583. void *(*thread_routine)(void *), void *arg)
  584. {
  585. WASMCluster *cluster;
  586. WASMExecEnv *new_exec_env;
  587. korp_tid tid;
  588. cluster = wasm_exec_env_get_cluster(exec_env);
  589. bh_assert(cluster);
  590. os_mutex_lock(&cluster->lock);
  591. if (cluster->has_exception || cluster->processing) {
  592. goto fail1;
  593. }
  594. new_exec_env =
  595. wasm_exec_env_create_internal(module_inst, exec_env->wasm_stack_size);
  596. if (!new_exec_env)
  597. goto fail1;
  598. if (is_aux_stack_allocated) {
  599. /* Set aux stack for current thread */
  600. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  601. aux_stack_size)) {
  602. goto fail2;
  603. }
  604. new_exec_env->is_aux_stack_allocated = true;
  605. }
  606. else {
  607. /* Disable aux stack */
  608. new_exec_env->aux_stack_boundary = 0;
  609. new_exec_env->aux_stack_bottom = UINTPTR_MAX;
  610. new_exec_env->is_aux_stack_allocated = false;
  611. }
  612. /* Inherit suspend_flags of parent thread */
  613. new_exec_env->suspend_flags.flags =
  614. (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK);
  615. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  616. goto fail2;
  617. new_exec_env->thread_start_routine = thread_routine;
  618. new_exec_env->thread_arg = arg;
  619. os_mutex_lock(&new_exec_env->wait_lock);
  620. if (0
  621. != os_thread_create(&tid, thread_manager_start_routine,
  622. (void *)new_exec_env,
  623. APP_THREAD_STACK_SIZE_DEFAULT)) {
  624. os_mutex_unlock(&new_exec_env->wait_lock);
  625. goto fail3;
  626. }
  627. /* Wait until the new_exec_env->handle is set to avoid it is
  628. illegally accessed after unlocking cluster->lock */
  629. os_cond_wait(&new_exec_env->wait_cond, &new_exec_env->wait_lock);
  630. os_mutex_unlock(&new_exec_env->wait_lock);
  631. os_mutex_unlock(&cluster->lock);
  632. return 0;
  633. fail3:
  634. wasm_cluster_del_exec_env_internal(cluster, new_exec_env, false);
  635. fail2:
  636. wasm_exec_env_destroy_internal(new_exec_env);
  637. fail1:
  638. os_mutex_unlock(&cluster->lock);
  639. return -1;
  640. }
  641. bool
  642. wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst,
  643. const WASMModuleInstanceCommon *module_inst_src)
  644. {
  645. /* workaround about passing instantiate-linking information */
  646. CApiFuncImport **new_c_api_func_imports = NULL;
  647. CApiFuncImport *c_api_func_imports = NULL;
  648. uint32 import_func_count = 0;
  649. uint32 size_in_bytes = 0;
  650. #if WASM_ENABLE_INTERP != 0
  651. if (module_inst_src->module_type == Wasm_Module_Bytecode) {
  652. new_c_api_func_imports =
  653. &(((WASMModuleInstance *)module_inst_dst)->c_api_func_imports);
  654. c_api_func_imports =
  655. ((const WASMModuleInstance *)module_inst_src)->c_api_func_imports;
  656. import_func_count =
  657. ((WASMModule *)(((const WASMModuleInstance *)module_inst_src)
  658. ->module))
  659. ->import_function_count;
  660. }
  661. #endif
  662. #if WASM_ENABLE_AOT != 0
  663. if (module_inst_src->module_type == Wasm_Module_AoT) {
  664. new_c_api_func_imports =
  665. &(((AOTModuleInstance *)module_inst_dst)->c_api_func_imports);
  666. c_api_func_imports =
  667. ((const AOTModuleInstance *)module_inst_src)->c_api_func_imports;
  668. import_func_count =
  669. ((AOTModule *)(((AOTModuleInstance *)module_inst_src)->module))
  670. ->import_func_count;
  671. }
  672. #endif
  673. if (import_func_count != 0 && c_api_func_imports) {
  674. size_in_bytes = sizeof(CApiFuncImport) * import_func_count;
  675. *new_c_api_func_imports = wasm_runtime_malloc(size_in_bytes);
  676. if (!(*new_c_api_func_imports))
  677. return false;
  678. bh_memcpy_s(*new_c_api_func_imports, size_in_bytes, c_api_func_imports,
  679. size_in_bytes);
  680. }
  681. return true;
  682. }
  683. #if WASM_ENABLE_DEBUG_INTERP != 0
  684. WASMCurrentEnvStatus *
  685. wasm_cluster_create_exenv_status()
  686. {
  687. WASMCurrentEnvStatus *status;
  688. if (!(status = wasm_runtime_malloc(sizeof(WASMCurrentEnvStatus)))) {
  689. return NULL;
  690. }
  691. status->step_count = 0;
  692. status->signal_flag = 0;
  693. status->running_status = 0;
  694. return status;
  695. }
  696. void
  697. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status)
  698. {
  699. wasm_runtime_free(status);
  700. }
  701. inline static bool
  702. wasm_cluster_thread_is_running(WASMExecEnv *exec_env)
  703. {
  704. return exec_env->current_status->running_status == STATUS_RUNNING
  705. || exec_env->current_status->running_status == STATUS_STEP;
  706. }
  707. void
  708. wasm_cluster_clear_thread_signal(WASMExecEnv *exec_env)
  709. {
  710. exec_env->current_status->signal_flag = 0;
  711. }
  712. void
  713. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo)
  714. {
  715. exec_env->current_status->signal_flag = signo;
  716. }
  717. static void
  718. notify_debug_instance(WASMExecEnv *exec_env)
  719. {
  720. WASMCluster *cluster;
  721. cluster = wasm_exec_env_get_cluster(exec_env);
  722. bh_assert(cluster);
  723. if (!cluster->debug_inst) {
  724. return;
  725. }
  726. on_thread_stop_event(cluster->debug_inst, exec_env);
  727. }
  728. static void
  729. notify_debug_instance_exit(WASMExecEnv *exec_env)
  730. {
  731. WASMCluster *cluster;
  732. cluster = wasm_exec_env_get_cluster(exec_env);
  733. bh_assert(cluster);
  734. if (!cluster->debug_inst) {
  735. return;
  736. }
  737. on_thread_exit_event(cluster->debug_inst, exec_env);
  738. }
  739. void
  740. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env)
  741. {
  742. exec_env->current_status->running_status = STATUS_STOP;
  743. notify_debug_instance(exec_env);
  744. while (!wasm_cluster_thread_is_running(exec_env)) {
  745. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  746. }
  747. }
  748. void
  749. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo)
  750. {
  751. WASMExecEnv *exec_env = bh_list_first_elem(&cluster->exec_env_list);
  752. while (exec_env) {
  753. wasm_cluster_thread_send_signal(exec_env, signo);
  754. exec_env = bh_list_elem_next(exec_env);
  755. }
  756. }
  757. void
  758. wasm_cluster_thread_exited(WASMExecEnv *exec_env)
  759. {
  760. exec_env->current_status->running_status = STATUS_EXIT;
  761. notify_debug_instance_exit(exec_env);
  762. }
  763. void
  764. wasm_cluster_thread_continue(WASMExecEnv *exec_env)
  765. {
  766. os_mutex_lock(&exec_env->wait_lock);
  767. wasm_cluster_clear_thread_signal(exec_env);
  768. exec_env->current_status->running_status = STATUS_RUNNING;
  769. os_cond_signal(&exec_env->wait_cond);
  770. os_mutex_unlock(&exec_env->wait_lock);
  771. }
  772. void
  773. wasm_cluster_thread_step(WASMExecEnv *exec_env)
  774. {
  775. os_mutex_lock(&exec_env->wait_lock);
  776. exec_env->current_status->running_status = STATUS_STEP;
  777. os_cond_signal(&exec_env->wait_cond);
  778. os_mutex_unlock(&exec_env->wait_lock);
  779. }
  780. void
  781. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst)
  782. {
  783. cluster->debug_inst = inst;
  784. }
  785. #endif /* end of WASM_ENABLE_DEBUG_INTERP */
  786. /* Check whether the exec_env is in one of all clusters, the caller
  787. should add lock to the cluster list before calling us */
  788. static bool
  789. clusters_have_exec_env(WASMExecEnv *exec_env)
  790. {
  791. WASMCluster *cluster = bh_list_first_elem(cluster_list);
  792. WASMExecEnv *node;
  793. while (cluster) {
  794. os_mutex_lock(&cluster->lock);
  795. node = bh_list_first_elem(&cluster->exec_env_list);
  796. while (node) {
  797. if (node == exec_env) {
  798. bh_assert(exec_env->cluster == cluster);
  799. os_mutex_unlock(&cluster->lock);
  800. return true;
  801. }
  802. node = bh_list_elem_next(node);
  803. }
  804. os_mutex_unlock(&cluster->lock);
  805. cluster = bh_list_elem_next(cluster);
  806. }
  807. return false;
  808. }
  809. int32
  810. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
  811. {
  812. korp_tid handle;
  813. os_mutex_lock(&cluster_list_lock);
  814. if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
  815. /* Invalid thread, thread has exited or thread has been detached */
  816. if (ret_val)
  817. *ret_val = NULL;
  818. os_mutex_unlock(&cluster_list_lock);
  819. return 0;
  820. }
  821. os_mutex_lock(&exec_env->wait_lock);
  822. exec_env->wait_count++;
  823. handle = exec_env->handle;
  824. os_mutex_unlock(&exec_env->wait_lock);
  825. os_mutex_unlock(&cluster_list_lock);
  826. return os_thread_join(handle, ret_val);
  827. }
  828. int32
  829. wasm_cluster_detach_thread(WASMExecEnv *exec_env)
  830. {
  831. int32 ret = 0;
  832. os_mutex_lock(&cluster_list_lock);
  833. if (!clusters_have_exec_env(exec_env)) {
  834. /* Invalid thread or the thread has exited */
  835. os_mutex_unlock(&cluster_list_lock);
  836. return 0;
  837. }
  838. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  839. /* Only detach current thread when there is no other thread
  840. joining it, otherwise let the system resources for the
  841. thread be released after joining */
  842. ret = os_thread_detach(exec_env->handle);
  843. exec_env->thread_is_detached = true;
  844. }
  845. os_mutex_unlock(&cluster_list_lock);
  846. return ret;
  847. }
  848. void
  849. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
  850. {
  851. WASMCluster *cluster;
  852. WASMModuleInstanceCommon *module_inst;
  853. #ifdef OS_ENABLE_HW_BOUND_CHECK
  854. if (exec_env->jmpbuf_stack_top) {
  855. /* Store the return value in exec_env */
  856. exec_env->thread_ret_value = retval;
  857. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  858. WASM_SUSPEND_FLAG_EXIT);
  859. #ifndef BH_PLATFORM_WINDOWS
  860. /* Pop all jmpbuf_node except the last one */
  861. while (exec_env->jmpbuf_stack_top->prev) {
  862. wasm_exec_env_pop_jmpbuf(exec_env);
  863. }
  864. os_longjmp(exec_env->jmpbuf_stack_top->jmpbuf, 1);
  865. return;
  866. #endif
  867. }
  868. #endif
  869. cluster = wasm_exec_env_get_cluster(exec_env);
  870. bh_assert(cluster);
  871. #if WASM_ENABLE_DEBUG_INTERP != 0
  872. wasm_cluster_clear_thread_signal(exec_env);
  873. wasm_cluster_thread_exited(exec_env);
  874. #endif
  875. /* Free aux stack space */
  876. if (exec_env->is_aux_stack_allocated)
  877. wasm_cluster_free_aux_stack(exec_env,
  878. (uint64)exec_env->aux_stack_bottom);
  879. /* App exit the thread, free the resources before exit native thread */
  880. os_mutex_lock(&cluster_list_lock);
  881. os_mutex_lock(&cluster->lock);
  882. /* Detach the native thread here to ensure the resources are freed */
  883. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  884. /* Only detach current thread when there is no other thread
  885. joining it, otherwise let the system resources for the
  886. thread be released after joining */
  887. os_thread_detach(exec_env->handle);
  888. /* No need to set exec_env->thread_is_detached to true here
  889. since we will exit soon */
  890. }
  891. module_inst = exec_env->module_inst;
  892. /* Remove exec_env */
  893. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  894. /* Destroy exec_env */
  895. wasm_exec_env_destroy_internal(exec_env);
  896. /* Routine exit, destroy instance */
  897. wasm_runtime_deinstantiate_internal(module_inst, true);
  898. os_mutex_unlock(&cluster->lock);
  899. os_mutex_unlock(&cluster_list_lock);
  900. os_thread_exit(retval);
  901. }
  902. static void
  903. set_thread_cancel_flags(WASMExecEnv *exec_env)
  904. {
  905. os_mutex_lock(&exec_env->wait_lock);
  906. #if WASM_ENABLE_DEBUG_INTERP != 0
  907. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  908. #endif
  909. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  910. WASM_SUSPEND_FLAG_TERMINATE);
  911. os_mutex_unlock(&exec_env->wait_lock);
  912. #ifdef OS_ENABLE_WAKEUP_BLOCKING_OP
  913. wasm_runtime_interrupt_blocking_op(exec_env);
  914. #endif
  915. }
  916. static void
  917. clear_thread_cancel_flags(WASMExecEnv *exec_env)
  918. {
  919. os_mutex_lock(&exec_env->wait_lock);
  920. WASM_SUSPEND_FLAGS_FETCH_AND(exec_env->suspend_flags,
  921. ~WASM_SUSPEND_FLAG_TERMINATE);
  922. os_mutex_unlock(&exec_env->wait_lock);
  923. }
  924. int32
  925. wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
  926. {
  927. os_mutex_lock(&cluster_list_lock);
  928. if (!exec_env->cluster) {
  929. os_mutex_unlock(&cluster_list_lock);
  930. return 0;
  931. }
  932. if (!clusters_have_exec_env(exec_env)) {
  933. /* Invalid thread or the thread has exited */
  934. goto final;
  935. }
  936. set_thread_cancel_flags(exec_env);
  937. final:
  938. os_mutex_unlock(&cluster_list_lock);
  939. return 0;
  940. }
  941. static void
  942. terminate_thread_visitor(void *node, void *user_data)
  943. {
  944. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  945. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  946. if (curr_exec_env == exec_env)
  947. return;
  948. wasm_cluster_cancel_thread(curr_exec_env);
  949. wasm_cluster_join_thread(curr_exec_env, NULL);
  950. }
  951. void
  952. wasm_cluster_terminate_all(WASMCluster *cluster)
  953. {
  954. os_mutex_lock(&cluster->lock);
  955. cluster->processing = true;
  956. safe_traverse_exec_env_list(cluster, terminate_thread_visitor, NULL);
  957. cluster->processing = false;
  958. os_mutex_unlock(&cluster->lock);
  959. }
  960. void
  961. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  962. WASMExecEnv *exec_env)
  963. {
  964. os_mutex_lock(&cluster->lock);
  965. cluster->processing = true;
  966. safe_traverse_exec_env_list(cluster, terminate_thread_visitor,
  967. (void *)exec_env);
  968. cluster->processing = false;
  969. os_mutex_unlock(&cluster->lock);
  970. }
  971. static void
  972. wait_for_thread_visitor(void *node, void *user_data)
  973. {
  974. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  975. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  976. if (curr_exec_env == exec_env)
  977. return;
  978. wasm_cluster_join_thread(curr_exec_env, NULL);
  979. }
  980. void
  981. wasm_cluster_wait_for_all(WASMCluster *cluster)
  982. {
  983. os_mutex_lock(&cluster->lock);
  984. cluster->processing = true;
  985. safe_traverse_exec_env_list(cluster, wait_for_thread_visitor, NULL);
  986. cluster->processing = false;
  987. os_mutex_unlock(&cluster->lock);
  988. }
  989. void
  990. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  991. WASMExecEnv *exec_env)
  992. {
  993. os_mutex_lock(&cluster->lock);
  994. cluster->processing = true;
  995. safe_traverse_exec_env_list(cluster, wait_for_thread_visitor,
  996. (void *)exec_env);
  997. cluster->processing = false;
  998. os_mutex_unlock(&cluster->lock);
  999. }
  1000. bool
  1001. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
  1002. {
  1003. DestroyCallBackNode *node;
  1004. if (!(node = wasm_runtime_malloc(sizeof(DestroyCallBackNode)))) {
  1005. LOG_ERROR("thread manager error: failed to allocate memory");
  1006. return false;
  1007. }
  1008. node->destroy_cb = callback;
  1009. bh_list_insert(destroy_callback_list, node);
  1010. return true;
  1011. }
  1012. void
  1013. wasm_cluster_suspend_thread(WASMExecEnv *exec_env)
  1014. {
  1015. /* Set the suspend flag */
  1016. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  1017. WASM_SUSPEND_FLAG_SUSPEND);
  1018. }
  1019. static void
  1020. suspend_thread_visitor(void *node, void *user_data)
  1021. {
  1022. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1023. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  1024. if (curr_exec_env == exec_env)
  1025. return;
  1026. wasm_cluster_suspend_thread(curr_exec_env);
  1027. }
  1028. void
  1029. wasm_cluster_suspend_all(WASMCluster *cluster)
  1030. {
  1031. os_mutex_lock(&cluster->lock);
  1032. traverse_list(&cluster->exec_env_list, suspend_thread_visitor, NULL);
  1033. os_mutex_unlock(&cluster->lock);
  1034. }
  1035. void
  1036. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  1037. WASMExecEnv *exec_env)
  1038. {
  1039. os_mutex_lock(&cluster->lock);
  1040. traverse_list(&cluster->exec_env_list, suspend_thread_visitor,
  1041. (void *)exec_env);
  1042. os_mutex_unlock(&cluster->lock);
  1043. }
  1044. void
  1045. wasm_cluster_resume_thread(WASMExecEnv *exec_env)
  1046. {
  1047. WASM_SUSPEND_FLAGS_FETCH_AND(exec_env->suspend_flags,
  1048. ~WASM_SUSPEND_FLAG_SUSPEND);
  1049. os_cond_signal(&exec_env->wait_cond);
  1050. }
  1051. static void
  1052. resume_thread_visitor(void *node, void *user_data)
  1053. {
  1054. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1055. wasm_cluster_resume_thread(curr_exec_env);
  1056. }
  1057. void
  1058. wasm_cluster_resume_all(WASMCluster *cluster)
  1059. {
  1060. os_mutex_lock(&cluster->lock);
  1061. traverse_list(&cluster->exec_env_list, resume_thread_visitor, NULL);
  1062. os_mutex_unlock(&cluster->lock);
  1063. }
  1064. struct spread_exception_data {
  1065. WASMExecEnv *skip;
  1066. const char *exception;
  1067. };
  1068. static void
  1069. set_exception_visitor(void *node, void *user_data)
  1070. {
  1071. const struct spread_exception_data *data = user_data;
  1072. WASMExecEnv *exec_env = (WASMExecEnv *)node;
  1073. if (exec_env != data->skip) {
  1074. WASMModuleInstance *wasm_inst =
  1075. (WASMModuleInstance *)get_module_inst(exec_env);
  1076. exception_lock(wasm_inst);
  1077. if (data->exception != NULL) {
  1078. snprintf(wasm_inst->cur_exception, sizeof(wasm_inst->cur_exception),
  1079. "Exception: %s", data->exception);
  1080. }
  1081. else {
  1082. wasm_inst->cur_exception[0] = '\0';
  1083. }
  1084. exception_unlock(wasm_inst);
  1085. /* Terminate the thread so it can exit from dead loops */
  1086. if (data->exception != NULL) {
  1087. set_thread_cancel_flags(exec_env);
  1088. }
  1089. else {
  1090. clear_thread_cancel_flags(exec_env);
  1091. }
  1092. }
  1093. }
  1094. void
  1095. wasm_cluster_set_exception(WASMExecEnv *exec_env, const char *exception)
  1096. {
  1097. const bool has_exception = exception != NULL;
  1098. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  1099. bh_assert(cluster);
  1100. struct spread_exception_data data;
  1101. data.skip = NULL;
  1102. data.exception = exception;
  1103. os_mutex_lock(&cluster->lock);
  1104. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1105. if (has_exception) {
  1106. /* Save the stack frames of the crashed thread into the cluster */
  1107. WASMModuleInstance *module_inst =
  1108. (WASMModuleInstance *)get_module_inst(exec_env);
  1109. #if WASM_ENABLE_INTERP != 0
  1110. if (module_inst->module_type == Wasm_Module_Bytecode
  1111. && wasm_interp_create_call_stack(exec_env)) {
  1112. wasm_frame_vec_clone_internal(module_inst->frames,
  1113. &cluster->exception_frames);
  1114. }
  1115. #endif
  1116. #if WASM_ENABLE_AOT != 0
  1117. if (module_inst->module_type == Wasm_Module_AoT
  1118. && aot_create_call_stack(exec_env)) {
  1119. wasm_frame_vec_clone_internal(module_inst->frames,
  1120. &cluster->exception_frames);
  1121. }
  1122. #endif
  1123. }
  1124. #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1125. cluster->has_exception = has_exception;
  1126. traverse_list(&cluster->exec_env_list, set_exception_visitor, &data);
  1127. os_mutex_unlock(&cluster->lock);
  1128. }
  1129. static void
  1130. set_custom_data_visitor(void *node, void *user_data)
  1131. {
  1132. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1133. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1134. wasm_runtime_set_custom_data_internal(module_inst, user_data);
  1135. }
  1136. void
  1137. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  1138. void *custom_data)
  1139. {
  1140. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1141. if (exec_env == NULL) {
  1142. /* Maybe threads have not been started yet. */
  1143. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  1144. }
  1145. else {
  1146. WASMCluster *cluster;
  1147. cluster = wasm_exec_env_get_cluster(exec_env);
  1148. bh_assert(cluster);
  1149. os_mutex_lock(&cluster->lock);
  1150. traverse_list(&cluster->exec_env_list, set_custom_data_visitor,
  1151. custom_data);
  1152. os_mutex_unlock(&cluster->lock);
  1153. }
  1154. }
  1155. #if WASM_ENABLE_SHARED_HEAP != 0
  1156. static void
  1157. attach_shared_heap_visitor(void *node, void *heap)
  1158. {
  1159. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1160. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1161. wasm_runtime_attach_shared_heap_internal(module_inst, heap);
  1162. }
  1163. static void
  1164. detach_shared_heap_visitor(void *node, void *heap)
  1165. {
  1166. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1167. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1168. (void)heap;
  1169. wasm_runtime_detach_shared_heap_internal(module_inst);
  1170. }
  1171. bool
  1172. wasm_cluster_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
  1173. WASMSharedHeap *heap)
  1174. {
  1175. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1176. if (exec_env == NULL) {
  1177. /* Maybe threads have not been started yet. */
  1178. return wasm_runtime_attach_shared_heap_internal(module_inst, heap);
  1179. }
  1180. else {
  1181. WASMCluster *cluster;
  1182. cluster = wasm_exec_env_get_cluster(exec_env);
  1183. bh_assert(cluster);
  1184. os_mutex_lock(&cluster->lock);
  1185. /* Try attaching shared heap to this module instance first
  1186. to ensure that we can attach it to all other instances. */
  1187. if (!wasm_runtime_attach_shared_heap_internal(module_inst, heap)) {
  1188. os_mutex_unlock(&cluster->lock);
  1189. return false;
  1190. }
  1191. /* Detach the shared heap so it can be attached again. */
  1192. wasm_runtime_detach_shared_heap_internal(module_inst);
  1193. traverse_list(&cluster->exec_env_list, attach_shared_heap_visitor,
  1194. heap);
  1195. os_mutex_unlock(&cluster->lock);
  1196. }
  1197. return true;
  1198. }
  1199. void
  1200. wasm_cluster_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
  1201. {
  1202. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1203. if (exec_env == NULL) {
  1204. /* Maybe threads have not been started yet. */
  1205. wasm_runtime_detach_shared_heap_internal(module_inst);
  1206. }
  1207. else {
  1208. WASMCluster *cluster;
  1209. cluster = wasm_exec_env_get_cluster(exec_env);
  1210. bh_assert(cluster);
  1211. os_mutex_lock(&cluster->lock);
  1212. traverse_list(&cluster->exec_env_list, detach_shared_heap_visitor,
  1213. NULL);
  1214. os_mutex_unlock(&cluster->lock);
  1215. }
  1216. }
  1217. #endif
  1218. #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
  1219. struct inst_set_context_data {
  1220. void *key;
  1221. void *ctx;
  1222. };
  1223. static void
  1224. set_context_visitor(void *node, void *user_data)
  1225. {
  1226. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1227. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1228. const struct inst_set_context_data *data = user_data;
  1229. wasm_runtime_set_context(module_inst, data->key, data->ctx);
  1230. }
  1231. void
  1232. wasm_cluster_set_context(WASMModuleInstanceCommon *module_inst, void *key,
  1233. void *ctx)
  1234. {
  1235. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1236. if (exec_env == NULL) {
  1237. /* Maybe threads have not been started yet. */
  1238. wasm_runtime_set_context(module_inst, key, ctx);
  1239. }
  1240. else {
  1241. WASMCluster *cluster;
  1242. struct inst_set_context_data data;
  1243. data.key = key;
  1244. data.ctx = ctx;
  1245. cluster = wasm_exec_env_get_cluster(exec_env);
  1246. bh_assert(cluster);
  1247. os_mutex_lock(&cluster->lock);
  1248. traverse_list(&cluster->exec_env_list, set_context_visitor, &data);
  1249. os_mutex_unlock(&cluster->lock);
  1250. }
  1251. }
  1252. #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
  1253. bool
  1254. wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
  1255. {
  1256. os_mutex_lock(&exec_env->wait_lock);
  1257. bool is_thread_terminated = (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags)
  1258. & WASM_SUSPEND_FLAG_TERMINATE)
  1259. ? true
  1260. : false;
  1261. os_mutex_unlock(&exec_env->wait_lock);
  1262. return is_thread_terminated;
  1263. }
  1264. void
  1265. exception_lock(WASMModuleInstance *module_inst)
  1266. {
  1267. /*
  1268. * Note: this lock could be per module instance if desirable.
  1269. * We can revisit on AOT version bump.
  1270. * It probably doesn't matter though because the exception handling
  1271. * logic should not be executed too frequently anyway.
  1272. */
  1273. os_mutex_lock(&_exception_lock);
  1274. }
  1275. void
  1276. exception_unlock(WASMModuleInstance *module_inst)
  1277. {
  1278. os_mutex_unlock(&_exception_lock);
  1279. }
  1280. void
  1281. wasm_cluster_traverse_lock(WASMExecEnv *exec_env)
  1282. {
  1283. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  1284. bh_assert(cluster);
  1285. os_mutex_lock(&cluster->lock);
  1286. }
  1287. void
  1288. wasm_cluster_traverse_unlock(WASMExecEnv *exec_env)
  1289. {
  1290. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  1291. bh_assert(cluster);
  1292. os_mutex_unlock(&cluster->lock);
  1293. }