thread_manager.c 37 KB

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