thread_manager.c 41 KB

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