thread_manager.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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, true, 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 (exec_env->suspend_flags.flags & 0x08)
  505. ret = exec_env->thread_ret_value;
  506. os_mutex_unlock(&exec_env->wait_lock);
  507. #endif
  508. /* Routine exit */
  509. #if WASM_ENABLE_DEBUG_INTERP != 0
  510. wasm_cluster_thread_exited(exec_env);
  511. #endif
  512. os_mutex_lock(&cluster_list_lock);
  513. os_mutex_lock(&cluster->lock);
  514. /* Detach the native thread here to ensure the resources are freed */
  515. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  516. /* Only detach current thread when there is no other thread
  517. joining it, otherwise let the system resources for the
  518. thread be released after joining */
  519. os_thread_detach(exec_env->handle);
  520. /* No need to set exec_env->thread_is_detached to true here
  521. since we will exit soon */
  522. }
  523. /* Free aux stack space */
  524. free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
  525. /* Remove exec_env */
  526. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  527. /* Destroy exec_env */
  528. wasm_exec_env_destroy_internal(exec_env);
  529. /* Routine exit, destroy instance */
  530. wasm_runtime_deinstantiate_internal(module_inst, true);
  531. os_mutex_unlock(&cluster->lock);
  532. os_mutex_unlock(&cluster_list_lock);
  533. os_thread_exit(ret);
  534. return ret;
  535. }
  536. int32
  537. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  538. wasm_module_inst_t module_inst, bool alloc_aux_stack,
  539. void *(*thread_routine)(void *), void *arg)
  540. {
  541. WASMCluster *cluster;
  542. WASMExecEnv *new_exec_env;
  543. uint32 aux_stack_start = 0, aux_stack_size;
  544. korp_tid tid;
  545. cluster = wasm_exec_env_get_cluster(exec_env);
  546. bh_assert(cluster);
  547. os_mutex_lock(&cluster->lock);
  548. if (cluster->has_exception || cluster->processing) {
  549. goto fail1;
  550. }
  551. new_exec_env =
  552. wasm_exec_env_create_internal(module_inst, exec_env->wasm_stack_size);
  553. if (!new_exec_env)
  554. goto fail1;
  555. if (alloc_aux_stack) {
  556. if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
  557. LOG_ERROR("thread manager error: "
  558. "failed to allocate aux stack space for new thread");
  559. goto fail2;
  560. }
  561. /* Set aux stack for current thread */
  562. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  563. aux_stack_size)) {
  564. goto fail3;
  565. }
  566. }
  567. else {
  568. /* Disable aux stack */
  569. new_exec_env->aux_stack_boundary.boundary = 0;
  570. new_exec_env->aux_stack_bottom.bottom = UINT32_MAX;
  571. }
  572. /* Inherit suspend_flags of parent thread */
  573. new_exec_env->suspend_flags.flags = exec_env->suspend_flags.flags;
  574. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  575. goto fail3;
  576. new_exec_env->thread_start_routine = thread_routine;
  577. new_exec_env->thread_arg = arg;
  578. os_mutex_lock(&new_exec_env->wait_lock);
  579. if (0
  580. != os_thread_create(&tid, thread_manager_start_routine,
  581. (void *)new_exec_env,
  582. APP_THREAD_STACK_SIZE_DEFAULT)) {
  583. os_mutex_unlock(&new_exec_env->wait_lock);
  584. goto fail4;
  585. }
  586. /* Wait until the new_exec_env->handle is set to avoid it is
  587. illegally accessed after unlocking cluster->lock */
  588. os_cond_wait(&new_exec_env->wait_cond, &new_exec_env->wait_lock);
  589. os_mutex_unlock(&new_exec_env->wait_lock);
  590. os_mutex_unlock(&cluster->lock);
  591. return 0;
  592. fail4:
  593. wasm_cluster_del_exec_env_internal(cluster, new_exec_env, false);
  594. fail3:
  595. /* free the allocated aux stack space */
  596. if (alloc_aux_stack)
  597. free_aux_stack(exec_env, aux_stack_start);
  598. fail2:
  599. wasm_exec_env_destroy_internal(new_exec_env);
  600. fail1:
  601. os_mutex_unlock(&cluster->lock);
  602. return -1;
  603. }
  604. bool
  605. wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst,
  606. const WASMModuleInstanceCommon *module_inst_src)
  607. {
  608. /* workaround about passing instantiate-linking information */
  609. CApiFuncImport **new_c_api_func_imports = NULL;
  610. CApiFuncImport *c_api_func_imports;
  611. uint32 import_func_count = 0;
  612. uint32 size_in_bytes = 0;
  613. #if WASM_ENABLE_INTERP != 0
  614. if (module_inst_src->module_type == Wasm_Module_Bytecode) {
  615. new_c_api_func_imports =
  616. &(((WASMModuleInstance *)module_inst_dst)->e->c_api_func_imports);
  617. c_api_func_imports = ((const WASMModuleInstance *)module_inst_src)
  618. ->e->c_api_func_imports;
  619. import_func_count =
  620. ((WASMModule *)(((const WASMModuleInstance *)module_inst_src)
  621. ->module))
  622. ->import_function_count;
  623. }
  624. #endif
  625. #if WASM_ENABLE_AOT != 0
  626. if (module_inst_src->module_type == Wasm_Module_AoT) {
  627. AOTModuleInstanceExtra *e =
  628. (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_dst)->e;
  629. new_c_api_func_imports = &(e->c_api_func_imports);
  630. e = (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_src)->e;
  631. c_api_func_imports = e->c_api_func_imports;
  632. import_func_count =
  633. ((AOTModule *)(((AOTModuleInstance *)module_inst_src)->module))
  634. ->import_func_count;
  635. }
  636. #endif
  637. if (import_func_count != 0 && c_api_func_imports) {
  638. size_in_bytes = sizeof(CApiFuncImport) * import_func_count;
  639. *new_c_api_func_imports = wasm_runtime_malloc(size_in_bytes);
  640. if (!(*new_c_api_func_imports))
  641. return false;
  642. bh_memcpy_s(*new_c_api_func_imports, size_in_bytes, c_api_func_imports,
  643. size_in_bytes);
  644. }
  645. return true;
  646. }
  647. #if WASM_ENABLE_DEBUG_INTERP != 0
  648. WASMCurrentEnvStatus *
  649. wasm_cluster_create_exenv_status()
  650. {
  651. WASMCurrentEnvStatus *status;
  652. if (!(status = wasm_runtime_malloc(sizeof(WASMCurrentEnvStatus)))) {
  653. return NULL;
  654. }
  655. status->step_count = 0;
  656. status->signal_flag = 0;
  657. status->running_status = 0;
  658. return status;
  659. }
  660. void
  661. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status)
  662. {
  663. wasm_runtime_free(status);
  664. }
  665. inline static bool
  666. wasm_cluster_thread_is_running(WASMExecEnv *exec_env)
  667. {
  668. return exec_env->current_status->running_status == STATUS_RUNNING
  669. || exec_env->current_status->running_status == STATUS_STEP;
  670. }
  671. void
  672. wasm_cluster_clear_thread_signal(WASMExecEnv *exec_env)
  673. {
  674. exec_env->current_status->signal_flag = 0;
  675. }
  676. void
  677. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo)
  678. {
  679. exec_env->current_status->signal_flag = signo;
  680. }
  681. static void
  682. notify_debug_instance(WASMExecEnv *exec_env)
  683. {
  684. WASMCluster *cluster;
  685. cluster = wasm_exec_env_get_cluster(exec_env);
  686. bh_assert(cluster);
  687. if (!cluster->debug_inst) {
  688. return;
  689. }
  690. on_thread_stop_event(cluster->debug_inst, exec_env);
  691. }
  692. static void
  693. notify_debug_instance_exit(WASMExecEnv *exec_env)
  694. {
  695. WASMCluster *cluster;
  696. cluster = wasm_exec_env_get_cluster(exec_env);
  697. bh_assert(cluster);
  698. if (!cluster->debug_inst) {
  699. return;
  700. }
  701. on_thread_exit_event(cluster->debug_inst, exec_env);
  702. }
  703. void
  704. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env)
  705. {
  706. exec_env->current_status->running_status = STATUS_STOP;
  707. notify_debug_instance(exec_env);
  708. while (!wasm_cluster_thread_is_running(exec_env)) {
  709. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  710. }
  711. }
  712. void
  713. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo)
  714. {
  715. WASMExecEnv *exec_env = bh_list_first_elem(&cluster->exec_env_list);
  716. while (exec_env) {
  717. wasm_cluster_thread_send_signal(exec_env, signo);
  718. exec_env = bh_list_elem_next(exec_env);
  719. }
  720. }
  721. void
  722. wasm_cluster_thread_exited(WASMExecEnv *exec_env)
  723. {
  724. exec_env->current_status->running_status = STATUS_EXIT;
  725. notify_debug_instance_exit(exec_env);
  726. }
  727. void
  728. wasm_cluster_thread_continue(WASMExecEnv *exec_env)
  729. {
  730. os_mutex_lock(&exec_env->wait_lock);
  731. wasm_cluster_clear_thread_signal(exec_env);
  732. exec_env->current_status->running_status = STATUS_RUNNING;
  733. os_cond_signal(&exec_env->wait_cond);
  734. os_mutex_unlock(&exec_env->wait_lock);
  735. }
  736. void
  737. wasm_cluster_thread_step(WASMExecEnv *exec_env)
  738. {
  739. os_mutex_lock(&exec_env->wait_lock);
  740. exec_env->current_status->running_status = STATUS_STEP;
  741. os_cond_signal(&exec_env->wait_cond);
  742. os_mutex_unlock(&exec_env->wait_lock);
  743. }
  744. void
  745. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst)
  746. {
  747. cluster->debug_inst = inst;
  748. }
  749. #endif /* end of WASM_ENABLE_DEBUG_INTERP */
  750. /* Check whether the exec_env is in one of all clusters, the caller
  751. should add lock to the cluster list before calling us */
  752. static bool
  753. clusters_have_exec_env(WASMExecEnv *exec_env)
  754. {
  755. WASMCluster *cluster = bh_list_first_elem(cluster_list);
  756. WASMExecEnv *node;
  757. while (cluster) {
  758. os_mutex_lock(&cluster->lock);
  759. node = bh_list_first_elem(&cluster->exec_env_list);
  760. while (node) {
  761. if (node == exec_env) {
  762. bh_assert(exec_env->cluster == cluster);
  763. os_mutex_unlock(&cluster->lock);
  764. return true;
  765. }
  766. node = bh_list_elem_next(node);
  767. }
  768. os_mutex_unlock(&cluster->lock);
  769. cluster = bh_list_elem_next(cluster);
  770. }
  771. return false;
  772. }
  773. int32
  774. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
  775. {
  776. korp_tid handle;
  777. os_mutex_lock(&cluster_list_lock);
  778. if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
  779. /* Invalid thread, thread has exited or thread has been detached */
  780. if (ret_val)
  781. *ret_val = NULL;
  782. os_mutex_unlock(&cluster_list_lock);
  783. return 0;
  784. }
  785. os_mutex_lock(&exec_env->wait_lock);
  786. exec_env->wait_count++;
  787. handle = exec_env->handle;
  788. os_mutex_unlock(&exec_env->wait_lock);
  789. os_mutex_unlock(&cluster_list_lock);
  790. return os_thread_join(handle, ret_val);
  791. }
  792. int32
  793. wasm_cluster_detach_thread(WASMExecEnv *exec_env)
  794. {
  795. int32 ret = 0;
  796. os_mutex_lock(&cluster_list_lock);
  797. if (!clusters_have_exec_env(exec_env)) {
  798. /* Invalid thread or the thread has exited */
  799. os_mutex_unlock(&cluster_list_lock);
  800. return 0;
  801. }
  802. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  803. /* Only detach current thread when there is no other thread
  804. joining it, otherwise let the system resources for the
  805. thread be released after joining */
  806. ret = os_thread_detach(exec_env->handle);
  807. exec_env->thread_is_detached = true;
  808. }
  809. os_mutex_unlock(&cluster_list_lock);
  810. return ret;
  811. }
  812. void
  813. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
  814. {
  815. WASMCluster *cluster;
  816. WASMModuleInstanceCommon *module_inst;
  817. #ifdef OS_ENABLE_HW_BOUND_CHECK
  818. if (exec_env->jmpbuf_stack_top) {
  819. /* Store the return value in exec_env */
  820. exec_env->thread_ret_value = retval;
  821. exec_env->suspend_flags.flags |= 0x08;
  822. #ifndef BH_PLATFORM_WINDOWS
  823. /* Pop all jmpbuf_node except the last one */
  824. while (exec_env->jmpbuf_stack_top->prev) {
  825. wasm_exec_env_pop_jmpbuf(exec_env);
  826. }
  827. os_longjmp(exec_env->jmpbuf_stack_top->jmpbuf, 1);
  828. return;
  829. #endif
  830. }
  831. #endif
  832. cluster = wasm_exec_env_get_cluster(exec_env);
  833. bh_assert(cluster);
  834. #if WASM_ENABLE_DEBUG_INTERP != 0
  835. wasm_cluster_clear_thread_signal(exec_env);
  836. wasm_cluster_thread_exited(exec_env);
  837. #endif
  838. /* App exit the thread, free the resources before exit native thread */
  839. os_mutex_lock(&cluster_list_lock);
  840. os_mutex_lock(&cluster->lock);
  841. /* Detach the native thread here to ensure the resources are freed */
  842. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  843. /* Only detach current thread when there is no other thread
  844. joining it, otherwise let the system resources for the
  845. thread be released after joining */
  846. os_thread_detach(exec_env->handle);
  847. /* No need to set exec_env->thread_is_detached to true here
  848. since we will exit soon */
  849. }
  850. module_inst = exec_env->module_inst;
  851. /* Free aux stack space */
  852. free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
  853. /* Remove exec_env */
  854. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  855. /* Destroy exec_env */
  856. wasm_exec_env_destroy_internal(exec_env);
  857. /* Routine exit, destroy instance */
  858. wasm_runtime_deinstantiate_internal(module_inst, true);
  859. os_mutex_unlock(&cluster->lock);
  860. os_mutex_unlock(&cluster_list_lock);
  861. os_thread_exit(retval);
  862. }
  863. static void
  864. set_thread_cancel_flags(WASMExecEnv *exec_env)
  865. {
  866. os_mutex_lock(&exec_env->wait_lock);
  867. #if WASM_ENABLE_DEBUG_INTERP != 0
  868. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  869. #endif
  870. exec_env->suspend_flags.flags |= 0x01;
  871. os_mutex_unlock(&exec_env->wait_lock);
  872. }
  873. int32
  874. wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
  875. {
  876. os_mutex_lock(&cluster_list_lock);
  877. if (!exec_env->cluster) {
  878. os_mutex_unlock(&cluster_list_lock);
  879. return 0;
  880. }
  881. if (!clusters_have_exec_env(exec_env)) {
  882. /* Invalid thread or the thread has exited */
  883. goto final;
  884. }
  885. set_thread_cancel_flags(exec_env);
  886. final:
  887. os_mutex_unlock(&cluster_list_lock);
  888. return 0;
  889. }
  890. static void
  891. terminate_thread_visitor(void *node, void *user_data)
  892. {
  893. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  894. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  895. if (curr_exec_env == exec_env)
  896. return;
  897. wasm_cluster_cancel_thread(curr_exec_env);
  898. wasm_cluster_join_thread(curr_exec_env, NULL);
  899. }
  900. void
  901. wasm_cluster_terminate_all(WASMCluster *cluster)
  902. {
  903. os_mutex_lock(&cluster->lock);
  904. cluster->processing = true;
  905. safe_traverse_exec_env_list(cluster, terminate_thread_visitor, NULL);
  906. cluster->processing = false;
  907. os_mutex_unlock(&cluster->lock);
  908. }
  909. void
  910. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  911. WASMExecEnv *exec_env)
  912. {
  913. os_mutex_lock(&cluster->lock);
  914. cluster->processing = true;
  915. safe_traverse_exec_env_list(cluster, terminate_thread_visitor,
  916. (void *)exec_env);
  917. cluster->processing = false;
  918. os_mutex_unlock(&cluster->lock);
  919. }
  920. static void
  921. wait_for_thread_visitor(void *node, void *user_data)
  922. {
  923. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  924. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  925. if (curr_exec_env == exec_env)
  926. return;
  927. wasm_cluster_join_thread(curr_exec_env, NULL);
  928. }
  929. void
  930. wams_cluster_wait_for_all(WASMCluster *cluster)
  931. {
  932. os_mutex_lock(&cluster->lock);
  933. cluster->processing = true;
  934. safe_traverse_exec_env_list(cluster, wait_for_thread_visitor, NULL);
  935. cluster->processing = false;
  936. os_mutex_unlock(&cluster->lock);
  937. }
  938. void
  939. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  940. WASMExecEnv *exec_env)
  941. {
  942. os_mutex_lock(&cluster->lock);
  943. cluster->processing = true;
  944. safe_traverse_exec_env_list(cluster, wait_for_thread_visitor,
  945. (void *)exec_env);
  946. cluster->processing = false;
  947. os_mutex_unlock(&cluster->lock);
  948. }
  949. bool
  950. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
  951. {
  952. DestroyCallBackNode *node;
  953. if (!(node = wasm_runtime_malloc(sizeof(DestroyCallBackNode)))) {
  954. LOG_ERROR("thread manager error: failed to allocate memory");
  955. return false;
  956. }
  957. node->destroy_cb = callback;
  958. bh_list_insert(destroy_callback_list, node);
  959. return true;
  960. }
  961. void
  962. wasm_cluster_suspend_thread(WASMExecEnv *exec_env)
  963. {
  964. /* Set the suspend flag */
  965. exec_env->suspend_flags.flags |= 0x02;
  966. }
  967. static void
  968. suspend_thread_visitor(void *node, void *user_data)
  969. {
  970. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  971. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  972. if (curr_exec_env == exec_env)
  973. return;
  974. wasm_cluster_suspend_thread(curr_exec_env);
  975. }
  976. void
  977. wasm_cluster_suspend_all(WASMCluster *cluster)
  978. {
  979. os_mutex_lock(&cluster->lock);
  980. traverse_list(&cluster->exec_env_list, suspend_thread_visitor, NULL);
  981. os_mutex_unlock(&cluster->lock);
  982. }
  983. void
  984. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  985. WASMExecEnv *exec_env)
  986. {
  987. os_mutex_lock(&cluster->lock);
  988. traverse_list(&cluster->exec_env_list, suspend_thread_visitor,
  989. (void *)exec_env);
  990. os_mutex_unlock(&cluster->lock);
  991. }
  992. void
  993. wasm_cluster_resume_thread(WASMExecEnv *exec_env)
  994. {
  995. exec_env->suspend_flags.flags &= ~0x02;
  996. os_cond_signal(&exec_env->wait_cond);
  997. }
  998. static void
  999. resume_thread_visitor(void *node, void *user_data)
  1000. {
  1001. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1002. wasm_cluster_resume_thread(curr_exec_env);
  1003. }
  1004. void
  1005. wasm_cluster_resume_all(WASMCluster *cluster)
  1006. {
  1007. os_mutex_lock(&cluster->lock);
  1008. traverse_list(&cluster->exec_env_list, resume_thread_visitor, NULL);
  1009. os_mutex_unlock(&cluster->lock);
  1010. }
  1011. static void
  1012. set_exception_visitor(void *node, void *user_data)
  1013. {
  1014. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1015. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  1016. WASMModuleInstanceCommon *module_inst = get_module_inst(exec_env);
  1017. WASMModuleInstance *wasm_inst = (WASMModuleInstance *)module_inst;
  1018. if (curr_exec_env != exec_env) {
  1019. WASMModuleInstance *curr_wasm_inst =
  1020. (WASMModuleInstance *)get_module_inst(curr_exec_env);
  1021. /* Only spread non "wasi proc exit" exception */
  1022. #if WASM_ENABLE_SHARED_MEMORY != 0
  1023. WASMSharedMemNode *shared_mem_node = wasm_module_get_shared_memory(
  1024. (WASMModuleCommon *)curr_wasm_inst->module);
  1025. if (shared_mem_node)
  1026. os_mutex_lock(&shared_mem_node->shared_mem_lock);
  1027. #endif
  1028. if (!strstr(wasm_inst->cur_exception, "wasi proc exit")) {
  1029. bh_memcpy_s(curr_wasm_inst->cur_exception,
  1030. sizeof(curr_wasm_inst->cur_exception),
  1031. wasm_inst->cur_exception,
  1032. sizeof(wasm_inst->cur_exception));
  1033. }
  1034. #if WASM_ENABLE_SHARED_MEMORY != 0
  1035. if (shared_mem_node)
  1036. os_mutex_unlock(&shared_mem_node->shared_mem_lock);
  1037. #endif
  1038. /* Terminate the thread so it can exit from dead loops */
  1039. set_thread_cancel_flags(curr_exec_env);
  1040. }
  1041. }
  1042. static void
  1043. clear_exception_visitor(void *node, void *user_data)
  1044. {
  1045. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  1046. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1047. if (curr_exec_env != exec_env) {
  1048. WASMModuleInstance *curr_wasm_inst =
  1049. (WASMModuleInstance *)get_module_inst(curr_exec_env);
  1050. #if WASM_ENABLE_SHARED_MEMORY != 0
  1051. WASMSharedMemNode *shared_mem_node = wasm_module_get_shared_memory(
  1052. (WASMModuleCommon *)curr_wasm_inst->module);
  1053. if (shared_mem_node)
  1054. os_mutex_lock(&shared_mem_node->shared_mem_lock);
  1055. #endif
  1056. curr_wasm_inst->cur_exception[0] = '\0';
  1057. #if WASM_ENABLE_SHARED_MEMORY != 0
  1058. if (shared_mem_node)
  1059. os_mutex_unlock(&shared_mem_node->shared_mem_lock);
  1060. #endif
  1061. }
  1062. }
  1063. void
  1064. wasm_cluster_spread_exception(WASMExecEnv *exec_env, bool clear)
  1065. {
  1066. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  1067. bh_assert(cluster);
  1068. os_mutex_lock(&cluster->lock);
  1069. cluster->has_exception = !clear;
  1070. traverse_list(&cluster->exec_env_list,
  1071. clear ? clear_exception_visitor : set_exception_visitor,
  1072. exec_env);
  1073. os_mutex_unlock(&cluster->lock);
  1074. }
  1075. static void
  1076. set_custom_data_visitor(void *node, void *user_data)
  1077. {
  1078. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1079. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1080. wasm_runtime_set_custom_data_internal(module_inst, user_data);
  1081. }
  1082. void
  1083. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  1084. void *custom_data)
  1085. {
  1086. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1087. if (exec_env == NULL) {
  1088. /* Maybe threads have not been started yet. */
  1089. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  1090. }
  1091. else {
  1092. WASMCluster *cluster;
  1093. cluster = wasm_exec_env_get_cluster(exec_env);
  1094. bh_assert(cluster);
  1095. os_mutex_lock(&cluster->lock);
  1096. traverse_list(&cluster->exec_env_list, set_custom_data_visitor,
  1097. custom_data);
  1098. os_mutex_unlock(&cluster->lock);
  1099. }
  1100. }
  1101. bool
  1102. wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
  1103. {
  1104. os_mutex_lock(&exec_env->wait_lock);
  1105. bool is_thread_terminated =
  1106. (exec_env->suspend_flags.flags & 0x01) ? true : false;
  1107. os_mutex_unlock(&exec_env->wait_lock);
  1108. return is_thread_terminated;
  1109. }