thread_manager.c 38 KB

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