thread_manager.c 37 KB

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