thread_manager.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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, uint64 *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. uint64 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, uint64 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_size;
  191. uint64 aux_stack_start;
  192. bh_assert(exec_env->cluster == NULL);
  193. if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
  194. LOG_ERROR("thread manager error: failed to allocate memory");
  195. return NULL;
  196. }
  197. memset(cluster, 0, sizeof(WASMCluster));
  198. exec_env->cluster = cluster;
  199. bh_list_init(&cluster->exec_env_list);
  200. bh_list_insert(&cluster->exec_env_list, exec_env);
  201. if (os_mutex_init(&cluster->lock) != 0) {
  202. wasm_runtime_free(cluster);
  203. LOG_ERROR("thread manager error: failed to init mutex");
  204. return NULL;
  205. }
  206. /* Prepare the aux stack top and size for every thread */
  207. if (!wasm_exec_env_get_aux_stack(exec_env, &aux_stack_start,
  208. &aux_stack_size)) {
  209. #if WASM_ENABLE_LIB_WASI_THREADS == 0
  210. LOG_VERBOSE("No aux stack info for this module, can't create thread");
  211. #endif
  212. /* If the module don't have aux stack info, don't throw error here,
  213. but remain stack_tops and stack_segment_occupied as NULL */
  214. os_mutex_lock(&cluster_list_lock);
  215. if (bh_list_insert(cluster_list, cluster) != 0) {
  216. os_mutex_unlock(&cluster_list_lock);
  217. goto fail;
  218. }
  219. os_mutex_unlock(&cluster_list_lock);
  220. return cluster;
  221. }
  222. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
  223. cluster->stack_size = aux_stack_size;
  224. #else
  225. cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
  226. if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
  227. goto fail;
  228. }
  229. /* Make stack size 16-byte aligned */
  230. cluster->stack_size = cluster->stack_size & (~15);
  231. #endif
  232. /* Set initial aux stack top to the instance and
  233. aux stack boundary to the main exec_env */
  234. if (!wasm_exec_env_set_aux_stack(exec_env, aux_stack_start,
  235. cluster->stack_size))
  236. goto fail;
  237. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  238. if (cluster_max_thread_num != 0) {
  239. uint64 total_size = cluster_max_thread_num * sizeof(uint64);
  240. uint32 i;
  241. if (total_size >= UINT32_MAX
  242. || !(cluster->stack_tops =
  243. wasm_runtime_malloc((uint32)total_size))) {
  244. goto fail;
  245. }
  246. memset(cluster->stack_tops, 0, (uint32)total_size);
  247. if (!(cluster->stack_segment_occupied =
  248. wasm_runtime_malloc(cluster_max_thread_num * sizeof(bool)))) {
  249. goto fail;
  250. }
  251. memset(cluster->stack_segment_occupied, 0,
  252. cluster_max_thread_num * sizeof(bool));
  253. /* Reserve space for main instance */
  254. aux_stack_start -= cluster->stack_size;
  255. for (i = 0; i < cluster_max_thread_num; i++) {
  256. cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
  257. }
  258. }
  259. #endif
  260. os_mutex_lock(&cluster_list_lock);
  261. if (bh_list_insert(cluster_list, cluster) != 0) {
  262. os_mutex_unlock(&cluster_list_lock);
  263. goto fail;
  264. }
  265. os_mutex_unlock(&cluster_list_lock);
  266. return cluster;
  267. fail:
  268. if (cluster)
  269. wasm_cluster_destroy(cluster);
  270. return NULL;
  271. }
  272. static void
  273. destroy_cluster_visitor(void *node, void *user_data)
  274. {
  275. DestroyCallBackNode *destroy_node = (DestroyCallBackNode *)node;
  276. WASMCluster *cluster = (WASMCluster *)user_data;
  277. destroy_node->destroy_cb(cluster);
  278. }
  279. void
  280. wasm_cluster_destroy(WASMCluster *cluster)
  281. {
  282. traverse_list(destroy_callback_list, destroy_cluster_visitor,
  283. (void *)cluster);
  284. /* Remove the cluster from the cluster list */
  285. os_mutex_lock(&cluster_list_lock);
  286. bh_list_remove(cluster_list, cluster);
  287. os_mutex_unlock(&cluster_list_lock);
  288. os_mutex_destroy(&cluster->lock);
  289. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  290. if (cluster->stack_tops)
  291. wasm_runtime_free(cluster->stack_tops);
  292. if (cluster->stack_segment_occupied)
  293. wasm_runtime_free(cluster->stack_segment_occupied);
  294. #endif
  295. #if WASM_ENABLE_DEBUG_INTERP != 0
  296. wasm_debug_instance_destroy(cluster);
  297. #endif
  298. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  299. bh_vector_destroy(&cluster->exception_frames);
  300. #endif
  301. wasm_runtime_free(cluster);
  302. }
  303. static void
  304. free_node_visitor(void *node, void *user_data)
  305. {
  306. wasm_runtime_free(node);
  307. }
  308. void
  309. wasm_cluster_cancel_all_callbacks()
  310. {
  311. traverse_list(destroy_callback_list, free_node_visitor, NULL);
  312. bh_list_init(destroy_callback_list);
  313. }
  314. WASMCluster *
  315. wasm_exec_env_get_cluster(WASMExecEnv *exec_env)
  316. {
  317. return exec_env->cluster;
  318. }
  319. /* The caller must lock cluster->lock */
  320. static bool
  321. wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  322. {
  323. bool ret = true;
  324. exec_env->cluster = cluster;
  325. if (cluster->exec_env_list.len == cluster_max_thread_num + 1) {
  326. LOG_ERROR("thread manager error: "
  327. "maximum number of threads exceeded");
  328. ret = false;
  329. }
  330. if (ret && bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
  331. ret = false;
  332. return ret;
  333. }
  334. static bool
  335. wasm_cluster_del_exec_env_internal(WASMCluster *cluster, WASMExecEnv *exec_env,
  336. bool can_destroy_cluster)
  337. {
  338. bool ret = true;
  339. bh_assert(exec_env->cluster == cluster);
  340. #if WASM_ENABLE_DEBUG_INTERP != 0
  341. /* Wait for debugger control thread to process the
  342. stop event of this thread */
  343. if (cluster->debug_inst) {
  344. /* lock the debug_inst->wait_lock so
  345. other threads can't fire stop events */
  346. os_mutex_lock(&cluster->debug_inst->wait_lock);
  347. while (cluster->debug_inst->stopped_thread == exec_env) {
  348. /* either wakes up by signal or by 1-second timeout */
  349. os_cond_reltimedwait(&cluster->debug_inst->wait_cond,
  350. &cluster->debug_inst->wait_lock, 1000000);
  351. }
  352. os_mutex_unlock(&cluster->debug_inst->wait_lock);
  353. }
  354. #endif
  355. if (bh_list_remove(&cluster->exec_env_list, exec_env) != 0)
  356. ret = false;
  357. if (can_destroy_cluster) {
  358. if (cluster->exec_env_list.len == 0) {
  359. /* exec_env_list empty, destroy the cluster */
  360. wasm_cluster_destroy(cluster);
  361. }
  362. }
  363. else {
  364. /* Don't destroy cluster as cluster->lock is being used */
  365. }
  366. return ret;
  367. }
  368. /* The caller should lock cluster->lock for thread safety */
  369. bool
  370. wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  371. {
  372. return wasm_cluster_del_exec_env_internal(cluster, exec_env, true);
  373. }
  374. static WASMExecEnv *
  375. wasm_cluster_search_exec_env(WASMCluster *cluster,
  376. WASMModuleInstanceCommon *module_inst)
  377. {
  378. WASMExecEnv *node = NULL;
  379. os_mutex_lock(&cluster->lock);
  380. node = bh_list_first_elem(&cluster->exec_env_list);
  381. while (node) {
  382. if (node->module_inst == module_inst) {
  383. os_mutex_unlock(&cluster->lock);
  384. return node;
  385. }
  386. node = bh_list_elem_next(node);
  387. }
  388. os_mutex_unlock(&cluster->lock);
  389. return NULL;
  390. }
  391. /* search the global cluster list to find if the given
  392. module instance have a corresponding exec_env */
  393. WASMExecEnv *
  394. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst)
  395. {
  396. WASMCluster *cluster = NULL;
  397. WASMExecEnv *exec_env = NULL;
  398. os_mutex_lock(&cluster_list_lock);
  399. cluster = bh_list_first_elem(cluster_list);
  400. while (cluster) {
  401. exec_env = wasm_cluster_search_exec_env(cluster, module_inst);
  402. if (exec_env) {
  403. os_mutex_unlock(&cluster_list_lock);
  404. return exec_env;
  405. }
  406. cluster = bh_list_elem_next(cluster);
  407. }
  408. os_mutex_unlock(&cluster_list_lock);
  409. return NULL;
  410. }
  411. WASMExecEnv *
  412. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
  413. {
  414. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  415. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  416. wasm_module_t module;
  417. wasm_module_inst_t new_module_inst;
  418. WASMExecEnv *new_exec_env;
  419. uint32 aux_stack_size;
  420. uint64 aux_stack_start;
  421. uint32 stack_size = 8192;
  422. int32 ret = -1;
  423. int32 spawned_import_count = 0;
  424. WASMExternInstance *spawned_imports = NULL;
  425. if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
  426. return NULL;
  427. }
  428. /*
  429. * build a imports list(WASMExternInstance[]) from parent's imports
  430. */
  431. spawned_import_count = ((WASMModule *)module)->import_count;
  432. spawned_imports =
  433. wasm_runtime_malloc(sizeof(WASMExternInstance) * spawned_import_count);
  434. if (spawned_imports == NULL) {
  435. LOG_ERROR("Failed to allocate memory for imports");
  436. return NULL;
  437. }
  438. ret = wasm_runtime_inherit_imports(module, module_inst, spawned_imports,
  439. spawned_import_count);
  440. if (ret != 0) {
  441. LOG_ERROR("Failed to inherit imports");
  442. goto release_imports;
  443. }
  444. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  445. module, module_inst, exec_env, stack_size,
  446. 0, // heap_size
  447. 0, // max_memory_pages
  448. spawned_imports, // imports
  449. spawned_import_count, // import_count
  450. NULL, 0))) {
  451. goto disinherit_imports;
  452. }
  453. /* Set custom_data to new module instance */
  454. wasm_runtime_set_custom_data_internal(
  455. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  456. wasm_native_inherit_contexts(new_module_inst, module_inst);
  457. if (!(wasm_cluster_dup_c_api_imports(new_module_inst, module_inst))) {
  458. goto fail1;
  459. }
  460. if (!wasm_cluster_allocate_aux_stack(exec_env, &aux_stack_start,
  461. &aux_stack_size)) {
  462. LOG_ERROR("thread manager error: "
  463. "failed to allocate aux stack space for new thread");
  464. goto fail1;
  465. }
  466. os_mutex_lock(&cluster->lock);
  467. if (cluster->has_exception || cluster->processing) {
  468. goto fail2;
  469. }
  470. #if WASM_ENABLE_INTERP != 0
  471. if (module_inst->module_type == Wasm_Module_Bytecode) {
  472. stack_size =
  473. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  474. }
  475. #endif
  476. #if WASM_ENABLE_AOT != 0
  477. if (module_inst->module_type == Wasm_Module_AoT) {
  478. stack_size =
  479. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  480. }
  481. #endif
  482. new_exec_env = wasm_exec_env_create_internal(new_module_inst,
  483. exec_env->wasm_stack_size);
  484. if (!new_exec_env) {
  485. goto fail2;
  486. }
  487. /* Set aux stack for current thread */
  488. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  489. aux_stack_size)) {
  490. goto fail3;
  491. }
  492. new_exec_env->is_aux_stack_allocated = true;
  493. /* Inherit suspend_flags of parent thread */
  494. new_exec_env->suspend_flags.flags =
  495. (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK);
  496. if (!wasm_cluster_add_exec_env(cluster, new_exec_env)) {
  497. goto fail3;
  498. }
  499. os_mutex_unlock(&cluster->lock);
  500. wasm_runtime_disinherit_imports(module, spawned_imports,
  501. spawned_import_count);
  502. wasm_runtime_free(spawned_imports);
  503. return new_exec_env;
  504. fail3:
  505. wasm_exec_env_destroy_internal(new_exec_env);
  506. fail2:
  507. os_mutex_unlock(&cluster->lock);
  508. /* free the allocated aux stack space */
  509. wasm_cluster_free_aux_stack(exec_env, aux_stack_start);
  510. fail1:
  511. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  512. disinherit_imports:
  513. wasm_runtime_disinherit_imports(module, spawned_imports,
  514. spawned_import_count);
  515. release_imports:
  516. wasm_runtime_free(spawned_imports);
  517. return NULL;
  518. }
  519. void
  520. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  521. {
  522. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  523. wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
  524. bh_assert(cluster != NULL);
  525. WASMExecEnv *exec_env_tls = NULL;
  526. #ifdef OS_ENABLE_HW_BOUND_CHECK
  527. /* Note: free_aux_stack can execute the module's "free" function
  528. * using the specified exec_env. In case of OS_ENABLE_HW_BOUND_CHECK,
  529. * it needs to match the TLS exec_env if available. (Consider a native
  530. * function which calls wasm_cluster_destroy_spawned_exec_env.)
  531. */
  532. exec_env_tls = wasm_runtime_get_exec_env_tls();
  533. #endif
  534. if (exec_env_tls == NULL) {
  535. exec_env_tls = exec_env;
  536. }
  537. /* Free aux stack space which was allocated in
  538. wasm_cluster_spawn_exec_env */
  539. bh_assert(exec_env_tls->is_aux_stack_allocated);
  540. wasm_cluster_free_aux_stack(exec_env_tls,
  541. (uint64)exec_env->aux_stack_bottom);
  542. os_mutex_lock(&cluster->lock);
  543. /* Remove exec_env */
  544. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  545. /* Destroy exec_env */
  546. wasm_exec_env_destroy_internal(exec_env);
  547. /* Routine exit, destroy instance */
  548. wasm_runtime_deinstantiate_internal(module_inst, true);
  549. os_mutex_unlock(&cluster->lock);
  550. }
  551. /* start routine of thread manager */
  552. static void *
  553. thread_manager_start_routine(void *arg)
  554. {
  555. void *ret;
  556. WASMExecEnv *exec_env = (WASMExecEnv *)arg;
  557. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  558. WASMModuleInstanceCommon *module_inst =
  559. wasm_exec_env_get_module_inst(exec_env);
  560. bh_assert(cluster != NULL);
  561. bh_assert(module_inst != NULL);
  562. os_mutex_lock(&exec_env->wait_lock);
  563. exec_env->handle = os_self_thread();
  564. /* Notify the parent thread to continue running */
  565. os_cond_signal(&exec_env->wait_cond);
  566. os_mutex_unlock(&exec_env->wait_lock);
  567. ret = exec_env->thread_start_routine(exec_env);
  568. #ifdef OS_ENABLE_HW_BOUND_CHECK
  569. os_mutex_lock(&exec_env->wait_lock);
  570. if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags)
  571. & WASM_SUSPEND_FLAG_EXIT)
  572. ret = exec_env->thread_ret_value;
  573. os_mutex_unlock(&exec_env->wait_lock);
  574. #endif
  575. /* Routine exit */
  576. #if WASM_ENABLE_DEBUG_INTERP != 0
  577. wasm_cluster_thread_exited(exec_env);
  578. #endif
  579. /* Free aux stack space */
  580. if (exec_env->is_aux_stack_allocated)
  581. wasm_cluster_free_aux_stack(exec_env,
  582. (uint64)exec_env->aux_stack_bottom);
  583. os_mutex_lock(&cluster_list_lock);
  584. os_mutex_lock(&cluster->lock);
  585. /* Detach the native thread here to ensure the resources are freed */
  586. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  587. /* Only detach current thread when there is no other thread
  588. joining it, otherwise let the system resources for the
  589. thread be released after joining */
  590. os_thread_detach(exec_env->handle);
  591. /* No need to set exec_env->thread_is_detached to true here
  592. since we will exit soon */
  593. }
  594. #if WASM_ENABLE_PERF_PROFILING != 0
  595. os_printf("============= Spawned thread ===========\n");
  596. wasm_runtime_dump_perf_profiling(module_inst);
  597. os_printf("========================================\n");
  598. #endif
  599. /* Remove exec_env */
  600. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  601. /* Destroy exec_env */
  602. wasm_exec_env_destroy_internal(exec_env);
  603. /* Routine exit, destroy instance */
  604. wasm_runtime_deinstantiate_internal(module_inst, true);
  605. os_mutex_unlock(&cluster->lock);
  606. os_mutex_unlock(&cluster_list_lock);
  607. os_thread_exit(ret);
  608. return ret;
  609. }
  610. int32
  611. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  612. wasm_module_inst_t module_inst,
  613. bool is_aux_stack_allocated, uint64 aux_stack_start,
  614. uint32 aux_stack_size,
  615. void *(*thread_routine)(void *), void *arg)
  616. {
  617. WASMCluster *cluster;
  618. WASMExecEnv *new_exec_env;
  619. korp_tid tid;
  620. cluster = wasm_exec_env_get_cluster(exec_env);
  621. bh_assert(cluster);
  622. os_mutex_lock(&cluster->lock);
  623. if (cluster->has_exception || cluster->processing) {
  624. goto fail1;
  625. }
  626. new_exec_env =
  627. wasm_exec_env_create_internal(module_inst, exec_env->wasm_stack_size);
  628. if (!new_exec_env)
  629. goto fail1;
  630. if (is_aux_stack_allocated) {
  631. /* Set aux stack for current thread */
  632. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  633. aux_stack_size)) {
  634. goto fail2;
  635. }
  636. new_exec_env->is_aux_stack_allocated = true;
  637. }
  638. else {
  639. /* Disable aux stack */
  640. new_exec_env->aux_stack_boundary = 0;
  641. new_exec_env->aux_stack_bottom = UINTPTR_MAX;
  642. new_exec_env->is_aux_stack_allocated = false;
  643. }
  644. /* Inherit suspend_flags of parent thread */
  645. new_exec_env->suspend_flags.flags =
  646. (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK);
  647. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  648. goto fail2;
  649. new_exec_env->thread_start_routine = thread_routine;
  650. new_exec_env->thread_arg = arg;
  651. os_mutex_lock(&new_exec_env->wait_lock);
  652. if (0
  653. != os_thread_create(&tid, thread_manager_start_routine,
  654. (void *)new_exec_env,
  655. APP_THREAD_STACK_SIZE_DEFAULT)) {
  656. os_mutex_unlock(&new_exec_env->wait_lock);
  657. goto fail3;
  658. }
  659. /* Wait until the new_exec_env->handle is set to avoid it is
  660. illegally accessed after unlocking cluster->lock */
  661. os_cond_wait(&new_exec_env->wait_cond, &new_exec_env->wait_lock);
  662. os_mutex_unlock(&new_exec_env->wait_lock);
  663. os_mutex_unlock(&cluster->lock);
  664. return 0;
  665. fail3:
  666. wasm_cluster_del_exec_env_internal(cluster, new_exec_env, false);
  667. fail2:
  668. wasm_exec_env_destroy_internal(new_exec_env);
  669. fail1:
  670. os_mutex_unlock(&cluster->lock);
  671. return -1;
  672. }
  673. bool
  674. wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst,
  675. const WASMModuleInstanceCommon *module_inst_src)
  676. {
  677. /* workaround about passing instantiate-linking information */
  678. CApiFuncImport **new_c_api_func_imports = NULL;
  679. CApiFuncImport *c_api_func_imports = NULL;
  680. uint32 import_func_count = 0;
  681. uint32 size_in_bytes = 0;
  682. #if WASM_ENABLE_INTERP != 0
  683. if (module_inst_src->module_type == Wasm_Module_Bytecode) {
  684. new_c_api_func_imports =
  685. &(((WASMModuleInstance *)module_inst_dst)->c_api_func_imports);
  686. c_api_func_imports =
  687. ((const WASMModuleInstance *)module_inst_src)->c_api_func_imports;
  688. import_func_count =
  689. ((WASMModule *)(((const WASMModuleInstance *)module_inst_src)
  690. ->module))
  691. ->import_function_count;
  692. }
  693. #endif
  694. #if WASM_ENABLE_AOT != 0
  695. if (module_inst_src->module_type == Wasm_Module_AoT) {
  696. new_c_api_func_imports =
  697. &(((AOTModuleInstance *)module_inst_dst)->c_api_func_imports);
  698. c_api_func_imports =
  699. ((const AOTModuleInstance *)module_inst_src)->c_api_func_imports;
  700. import_func_count =
  701. ((AOTModule *)(((AOTModuleInstance *)module_inst_src)->module))
  702. ->import_func_count;
  703. }
  704. #endif
  705. if (import_func_count != 0 && c_api_func_imports) {
  706. size_in_bytes = sizeof(CApiFuncImport) * import_func_count;
  707. *new_c_api_func_imports = wasm_runtime_malloc(size_in_bytes);
  708. if (!(*new_c_api_func_imports))
  709. return false;
  710. bh_memcpy_s(*new_c_api_func_imports, size_in_bytes, c_api_func_imports,
  711. size_in_bytes);
  712. }
  713. return true;
  714. }
  715. #if WASM_ENABLE_DEBUG_INTERP != 0
  716. WASMCurrentEnvStatus *
  717. wasm_cluster_create_exenv_status()
  718. {
  719. WASMCurrentEnvStatus *status;
  720. if (!(status = wasm_runtime_malloc(sizeof(WASMCurrentEnvStatus)))) {
  721. return NULL;
  722. }
  723. status->step_count = 0;
  724. status->signal_flag = 0;
  725. status->running_status = 0;
  726. return status;
  727. }
  728. void
  729. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status)
  730. {
  731. wasm_runtime_free(status);
  732. }
  733. inline static bool
  734. wasm_cluster_thread_is_running(WASMExecEnv *exec_env)
  735. {
  736. return exec_env->current_status->running_status == STATUS_RUNNING
  737. || exec_env->current_status->running_status == STATUS_STEP;
  738. }
  739. void
  740. wasm_cluster_clear_thread_signal(WASMExecEnv *exec_env)
  741. {
  742. exec_env->current_status->signal_flag = 0;
  743. }
  744. void
  745. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo)
  746. {
  747. exec_env->current_status->signal_flag = signo;
  748. }
  749. static void
  750. notify_debug_instance(WASMExecEnv *exec_env)
  751. {
  752. WASMCluster *cluster;
  753. cluster = wasm_exec_env_get_cluster(exec_env);
  754. bh_assert(cluster);
  755. if (!cluster->debug_inst) {
  756. return;
  757. }
  758. on_thread_stop_event(cluster->debug_inst, exec_env);
  759. }
  760. static void
  761. notify_debug_instance_exit(WASMExecEnv *exec_env)
  762. {
  763. WASMCluster *cluster;
  764. cluster = wasm_exec_env_get_cluster(exec_env);
  765. bh_assert(cluster);
  766. if (!cluster->debug_inst) {
  767. return;
  768. }
  769. on_thread_exit_event(cluster->debug_inst, exec_env);
  770. }
  771. void
  772. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env)
  773. {
  774. exec_env->current_status->running_status = STATUS_STOP;
  775. notify_debug_instance(exec_env);
  776. while (!wasm_cluster_thread_is_running(exec_env)) {
  777. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  778. }
  779. }
  780. void
  781. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo)
  782. {
  783. WASMExecEnv *exec_env = bh_list_first_elem(&cluster->exec_env_list);
  784. while (exec_env) {
  785. wasm_cluster_thread_send_signal(exec_env, signo);
  786. exec_env = bh_list_elem_next(exec_env);
  787. }
  788. }
  789. void
  790. wasm_cluster_thread_exited(WASMExecEnv *exec_env)
  791. {
  792. exec_env->current_status->running_status = STATUS_EXIT;
  793. notify_debug_instance_exit(exec_env);
  794. }
  795. void
  796. wasm_cluster_thread_continue(WASMExecEnv *exec_env)
  797. {
  798. os_mutex_lock(&exec_env->wait_lock);
  799. wasm_cluster_clear_thread_signal(exec_env);
  800. exec_env->current_status->running_status = STATUS_RUNNING;
  801. os_cond_signal(&exec_env->wait_cond);
  802. os_mutex_unlock(&exec_env->wait_lock);
  803. }
  804. void
  805. wasm_cluster_thread_step(WASMExecEnv *exec_env)
  806. {
  807. os_mutex_lock(&exec_env->wait_lock);
  808. exec_env->current_status->running_status = STATUS_STEP;
  809. os_cond_signal(&exec_env->wait_cond);
  810. os_mutex_unlock(&exec_env->wait_lock);
  811. }
  812. void
  813. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst)
  814. {
  815. cluster->debug_inst = inst;
  816. }
  817. #endif /* end of WASM_ENABLE_DEBUG_INTERP */
  818. /* Check whether the exec_env is in one of all clusters, the caller
  819. should add lock to the cluster list before calling us */
  820. static bool
  821. clusters_have_exec_env(WASMExecEnv *exec_env)
  822. {
  823. WASMCluster *cluster = bh_list_first_elem(cluster_list);
  824. WASMExecEnv *node;
  825. while (cluster) {
  826. os_mutex_lock(&cluster->lock);
  827. node = bh_list_first_elem(&cluster->exec_env_list);
  828. while (node) {
  829. if (node == exec_env) {
  830. bh_assert(exec_env->cluster == cluster);
  831. os_mutex_unlock(&cluster->lock);
  832. return true;
  833. }
  834. node = bh_list_elem_next(node);
  835. }
  836. os_mutex_unlock(&cluster->lock);
  837. cluster = bh_list_elem_next(cluster);
  838. }
  839. return false;
  840. }
  841. int32
  842. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
  843. {
  844. korp_tid handle;
  845. os_mutex_lock(&cluster_list_lock);
  846. if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
  847. /* Invalid thread, thread has exited or thread has been detached */
  848. if (ret_val)
  849. *ret_val = NULL;
  850. os_mutex_unlock(&cluster_list_lock);
  851. return 0;
  852. }
  853. os_mutex_lock(&exec_env->wait_lock);
  854. exec_env->wait_count++;
  855. handle = exec_env->handle;
  856. os_mutex_unlock(&exec_env->wait_lock);
  857. os_mutex_unlock(&cluster_list_lock);
  858. return os_thread_join(handle, ret_val);
  859. }
  860. int32
  861. wasm_cluster_detach_thread(WASMExecEnv *exec_env)
  862. {
  863. int32 ret = 0;
  864. os_mutex_lock(&cluster_list_lock);
  865. if (!clusters_have_exec_env(exec_env)) {
  866. /* Invalid thread or the thread has exited */
  867. os_mutex_unlock(&cluster_list_lock);
  868. return 0;
  869. }
  870. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  871. /* Only detach current thread when there is no other thread
  872. joining it, otherwise let the system resources for the
  873. thread be released after joining */
  874. ret = os_thread_detach(exec_env->handle);
  875. exec_env->thread_is_detached = true;
  876. }
  877. os_mutex_unlock(&cluster_list_lock);
  878. return ret;
  879. }
  880. void
  881. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
  882. {
  883. WASMCluster *cluster;
  884. WASMModuleInstanceCommon *module_inst;
  885. #ifdef OS_ENABLE_HW_BOUND_CHECK
  886. if (exec_env->jmpbuf_stack_top) {
  887. /* Store the return value in exec_env */
  888. exec_env->thread_ret_value = retval;
  889. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  890. WASM_SUSPEND_FLAG_EXIT);
  891. #ifndef BH_PLATFORM_WINDOWS
  892. /* Pop all jmpbuf_node except the last one */
  893. while (exec_env->jmpbuf_stack_top->prev) {
  894. wasm_exec_env_pop_jmpbuf(exec_env);
  895. }
  896. os_longjmp(exec_env->jmpbuf_stack_top->jmpbuf, 1);
  897. return;
  898. #endif
  899. }
  900. #endif
  901. cluster = wasm_exec_env_get_cluster(exec_env);
  902. bh_assert(cluster);
  903. #if WASM_ENABLE_DEBUG_INTERP != 0
  904. wasm_cluster_clear_thread_signal(exec_env);
  905. wasm_cluster_thread_exited(exec_env);
  906. #endif
  907. /* Free aux stack space */
  908. if (exec_env->is_aux_stack_allocated)
  909. wasm_cluster_free_aux_stack(exec_env,
  910. (uint64)exec_env->aux_stack_bottom);
  911. /* App exit the thread, free the resources before exit native thread */
  912. os_mutex_lock(&cluster_list_lock);
  913. os_mutex_lock(&cluster->lock);
  914. /* Detach the native thread here to ensure the resources are freed */
  915. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  916. /* Only detach current thread when there is no other thread
  917. joining it, otherwise let the system resources for the
  918. thread be released after joining */
  919. os_thread_detach(exec_env->handle);
  920. /* No need to set exec_env->thread_is_detached to true here
  921. since we will exit soon */
  922. }
  923. module_inst = exec_env->module_inst;
  924. /* Remove exec_env */
  925. wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
  926. /* Destroy exec_env */
  927. wasm_exec_env_destroy_internal(exec_env);
  928. /* Routine exit, destroy instance */
  929. wasm_runtime_deinstantiate_internal(module_inst, true);
  930. os_mutex_unlock(&cluster->lock);
  931. os_mutex_unlock(&cluster_list_lock);
  932. os_thread_exit(retval);
  933. }
  934. static void
  935. set_thread_cancel_flags(WASMExecEnv *exec_env)
  936. {
  937. os_mutex_lock(&exec_env->wait_lock);
  938. #if WASM_ENABLE_DEBUG_INTERP != 0
  939. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  940. #endif
  941. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  942. WASM_SUSPEND_FLAG_TERMINATE);
  943. os_mutex_unlock(&exec_env->wait_lock);
  944. #ifdef OS_ENABLE_WAKEUP_BLOCKING_OP
  945. wasm_runtime_interrupt_blocking_op(exec_env);
  946. #endif
  947. }
  948. static void
  949. clear_thread_cancel_flags(WASMExecEnv *exec_env)
  950. {
  951. os_mutex_lock(&exec_env->wait_lock);
  952. WASM_SUSPEND_FLAGS_FETCH_AND(exec_env->suspend_flags,
  953. ~WASM_SUSPEND_FLAG_TERMINATE);
  954. os_mutex_unlock(&exec_env->wait_lock);
  955. }
  956. int32
  957. wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
  958. {
  959. os_mutex_lock(&cluster_list_lock);
  960. if (!exec_env->cluster) {
  961. os_mutex_unlock(&cluster_list_lock);
  962. return 0;
  963. }
  964. if (!clusters_have_exec_env(exec_env)) {
  965. /* Invalid thread or the thread has exited */
  966. goto final;
  967. }
  968. set_thread_cancel_flags(exec_env);
  969. final:
  970. os_mutex_unlock(&cluster_list_lock);
  971. return 0;
  972. }
  973. static void
  974. terminate_thread_visitor(void *node, void *user_data)
  975. {
  976. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  977. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  978. if (curr_exec_env == exec_env)
  979. return;
  980. wasm_cluster_cancel_thread(curr_exec_env);
  981. wasm_cluster_join_thread(curr_exec_env, NULL);
  982. }
  983. void
  984. wasm_cluster_terminate_all(WASMCluster *cluster)
  985. {
  986. os_mutex_lock(&cluster->lock);
  987. cluster->processing = true;
  988. safe_traverse_exec_env_list(cluster, terminate_thread_visitor, NULL);
  989. cluster->processing = false;
  990. os_mutex_unlock(&cluster->lock);
  991. }
  992. void
  993. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  994. WASMExecEnv *exec_env)
  995. {
  996. os_mutex_lock(&cluster->lock);
  997. cluster->processing = true;
  998. safe_traverse_exec_env_list(cluster, terminate_thread_visitor,
  999. (void *)exec_env);
  1000. cluster->processing = false;
  1001. os_mutex_unlock(&cluster->lock);
  1002. }
  1003. static void
  1004. wait_for_thread_visitor(void *node, void *user_data)
  1005. {
  1006. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1007. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  1008. if (curr_exec_env == exec_env)
  1009. return;
  1010. wasm_cluster_join_thread(curr_exec_env, NULL);
  1011. }
  1012. void
  1013. wasm_cluster_wait_for_all(WASMCluster *cluster)
  1014. {
  1015. os_mutex_lock(&cluster->lock);
  1016. cluster->processing = true;
  1017. safe_traverse_exec_env_list(cluster, wait_for_thread_visitor, NULL);
  1018. cluster->processing = false;
  1019. os_mutex_unlock(&cluster->lock);
  1020. }
  1021. void
  1022. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  1023. WASMExecEnv *exec_env)
  1024. {
  1025. os_mutex_lock(&cluster->lock);
  1026. cluster->processing = true;
  1027. safe_traverse_exec_env_list(cluster, wait_for_thread_visitor,
  1028. (void *)exec_env);
  1029. cluster->processing = false;
  1030. os_mutex_unlock(&cluster->lock);
  1031. }
  1032. bool
  1033. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
  1034. {
  1035. DestroyCallBackNode *node;
  1036. if (!(node = wasm_runtime_malloc(sizeof(DestroyCallBackNode)))) {
  1037. LOG_ERROR("thread manager error: failed to allocate memory");
  1038. return false;
  1039. }
  1040. node->destroy_cb = callback;
  1041. bh_list_insert(destroy_callback_list, node);
  1042. return true;
  1043. }
  1044. void
  1045. wasm_cluster_suspend_thread(WASMExecEnv *exec_env)
  1046. {
  1047. /* Set the suspend flag */
  1048. WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
  1049. WASM_SUSPEND_FLAG_SUSPEND);
  1050. }
  1051. static void
  1052. suspend_thread_visitor(void *node, void *user_data)
  1053. {
  1054. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1055. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  1056. if (curr_exec_env == exec_env)
  1057. return;
  1058. wasm_cluster_suspend_thread(curr_exec_env);
  1059. }
  1060. void
  1061. wasm_cluster_suspend_all(WASMCluster *cluster)
  1062. {
  1063. os_mutex_lock(&cluster->lock);
  1064. traverse_list(&cluster->exec_env_list, suspend_thread_visitor, NULL);
  1065. os_mutex_unlock(&cluster->lock);
  1066. }
  1067. void
  1068. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  1069. WASMExecEnv *exec_env)
  1070. {
  1071. os_mutex_lock(&cluster->lock);
  1072. traverse_list(&cluster->exec_env_list, suspend_thread_visitor,
  1073. (void *)exec_env);
  1074. os_mutex_unlock(&cluster->lock);
  1075. }
  1076. void
  1077. wasm_cluster_resume_thread(WASMExecEnv *exec_env)
  1078. {
  1079. WASM_SUSPEND_FLAGS_FETCH_AND(exec_env->suspend_flags,
  1080. ~WASM_SUSPEND_FLAG_SUSPEND);
  1081. os_cond_signal(&exec_env->wait_cond);
  1082. }
  1083. static void
  1084. resume_thread_visitor(void *node, void *user_data)
  1085. {
  1086. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1087. wasm_cluster_resume_thread(curr_exec_env);
  1088. }
  1089. void
  1090. wasm_cluster_resume_all(WASMCluster *cluster)
  1091. {
  1092. os_mutex_lock(&cluster->lock);
  1093. traverse_list(&cluster->exec_env_list, resume_thread_visitor, NULL);
  1094. os_mutex_unlock(&cluster->lock);
  1095. }
  1096. struct spread_exception_data {
  1097. WASMExecEnv *skip;
  1098. const char *exception;
  1099. };
  1100. static void
  1101. set_exception_visitor(void *node, void *user_data)
  1102. {
  1103. const struct spread_exception_data *data = user_data;
  1104. WASMExecEnv *exec_env = (WASMExecEnv *)node;
  1105. if (exec_env != data->skip) {
  1106. WASMModuleInstance *wasm_inst =
  1107. (WASMModuleInstance *)get_module_inst(exec_env);
  1108. exception_lock(wasm_inst);
  1109. if (data->exception != NULL) {
  1110. snprintf(wasm_inst->cur_exception, sizeof(wasm_inst->cur_exception),
  1111. "Exception: %s", data->exception);
  1112. }
  1113. else {
  1114. wasm_inst->cur_exception[0] = '\0';
  1115. }
  1116. exception_unlock(wasm_inst);
  1117. /* Terminate the thread so it can exit from dead loops */
  1118. if (data->exception != NULL) {
  1119. set_thread_cancel_flags(exec_env);
  1120. }
  1121. else {
  1122. clear_thread_cancel_flags(exec_env);
  1123. }
  1124. }
  1125. }
  1126. void
  1127. wasm_cluster_set_exception(WASMExecEnv *exec_env, const char *exception)
  1128. {
  1129. const bool has_exception = exception != NULL;
  1130. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  1131. bh_assert(cluster);
  1132. struct spread_exception_data data;
  1133. data.skip = NULL;
  1134. data.exception = exception;
  1135. os_mutex_lock(&cluster->lock);
  1136. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  1137. if (has_exception) {
  1138. /* Save the stack frames of the crashed thread into the cluster */
  1139. WASMModuleInstance *module_inst =
  1140. (WASMModuleInstance *)get_module_inst(exec_env);
  1141. #if WASM_ENABLE_INTERP != 0
  1142. if (module_inst->module_type == Wasm_Module_Bytecode
  1143. && wasm_interp_create_call_stack(exec_env)) {
  1144. wasm_frame_vec_clone_internal(module_inst->frames,
  1145. &cluster->exception_frames);
  1146. }
  1147. #endif
  1148. #if WASM_ENABLE_AOT != 0
  1149. if (module_inst->module_type == Wasm_Module_AoT
  1150. && aot_create_call_stack(exec_env)) {
  1151. wasm_frame_vec_clone_internal(module_inst->frames,
  1152. &cluster->exception_frames);
  1153. }
  1154. #endif
  1155. }
  1156. #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
  1157. cluster->has_exception = has_exception;
  1158. traverse_list(&cluster->exec_env_list, set_exception_visitor, &data);
  1159. os_mutex_unlock(&cluster->lock);
  1160. }
  1161. static void
  1162. set_custom_data_visitor(void *node, void *user_data)
  1163. {
  1164. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1165. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1166. wasm_runtime_set_custom_data_internal(module_inst, user_data);
  1167. }
  1168. void
  1169. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  1170. void *custom_data)
  1171. {
  1172. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1173. if (exec_env == NULL) {
  1174. /* Maybe threads have not been started yet. */
  1175. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  1176. }
  1177. else {
  1178. WASMCluster *cluster;
  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_custom_data_visitor,
  1183. custom_data);
  1184. os_mutex_unlock(&cluster->lock);
  1185. }
  1186. }
  1187. #if WASM_ENABLE_SHARED_HEAP != 0
  1188. static void
  1189. attach_shared_heap_visitor(void *node, void *heap)
  1190. {
  1191. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1192. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1193. wasm_runtime_attach_shared_heap_internal(module_inst, heap);
  1194. }
  1195. static void
  1196. detach_shared_heap_visitor(void *node, void *heap)
  1197. {
  1198. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1199. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1200. (void)heap;
  1201. wasm_runtime_detach_shared_heap_internal(module_inst);
  1202. }
  1203. bool
  1204. wasm_cluster_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
  1205. WASMSharedHeap *heap)
  1206. {
  1207. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1208. if (exec_env == NULL) {
  1209. /* Maybe threads have not been started yet. */
  1210. return wasm_runtime_attach_shared_heap_internal(module_inst, heap);
  1211. }
  1212. else {
  1213. WASMCluster *cluster;
  1214. cluster = wasm_exec_env_get_cluster(exec_env);
  1215. bh_assert(cluster);
  1216. os_mutex_lock(&cluster->lock);
  1217. /* Try attaching shared heap to this module instance first
  1218. to ensure that we can attach it to all other instances. */
  1219. if (!wasm_runtime_attach_shared_heap_internal(module_inst, heap)) {
  1220. os_mutex_unlock(&cluster->lock);
  1221. return false;
  1222. }
  1223. /* Detach the shared heap so it can be attached again. */
  1224. wasm_runtime_detach_shared_heap_internal(module_inst);
  1225. traverse_list(&cluster->exec_env_list, attach_shared_heap_visitor,
  1226. heap);
  1227. os_mutex_unlock(&cluster->lock);
  1228. }
  1229. return true;
  1230. }
  1231. void
  1232. wasm_cluster_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
  1233. {
  1234. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1235. if (exec_env == NULL) {
  1236. /* Maybe threads have not been started yet. */
  1237. wasm_runtime_detach_shared_heap_internal(module_inst);
  1238. }
  1239. else {
  1240. WASMCluster *cluster;
  1241. cluster = wasm_exec_env_get_cluster(exec_env);
  1242. bh_assert(cluster);
  1243. os_mutex_lock(&cluster->lock);
  1244. traverse_list(&cluster->exec_env_list, detach_shared_heap_visitor,
  1245. NULL);
  1246. os_mutex_unlock(&cluster->lock);
  1247. }
  1248. }
  1249. #endif
  1250. #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
  1251. struct inst_set_context_data {
  1252. void *key;
  1253. void *ctx;
  1254. };
  1255. static void
  1256. set_context_visitor(void *node, void *user_data)
  1257. {
  1258. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  1259. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  1260. const struct inst_set_context_data *data = user_data;
  1261. wasm_runtime_set_context(module_inst, data->key, data->ctx);
  1262. }
  1263. void
  1264. wasm_cluster_set_context(WASMModuleInstanceCommon *module_inst, void *key,
  1265. void *ctx)
  1266. {
  1267. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  1268. if (exec_env == NULL) {
  1269. /* Maybe threads have not been started yet. */
  1270. wasm_runtime_set_context(module_inst, key, ctx);
  1271. }
  1272. else {
  1273. WASMCluster *cluster;
  1274. struct inst_set_context_data data;
  1275. data.key = key;
  1276. data.ctx = ctx;
  1277. cluster = wasm_exec_env_get_cluster(exec_env);
  1278. bh_assert(cluster);
  1279. os_mutex_lock(&cluster->lock);
  1280. traverse_list(&cluster->exec_env_list, set_context_visitor, &data);
  1281. os_mutex_unlock(&cluster->lock);
  1282. }
  1283. }
  1284. #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
  1285. bool
  1286. wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
  1287. {
  1288. os_mutex_lock(&exec_env->wait_lock);
  1289. bool is_thread_terminated = (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags)
  1290. & WASM_SUSPEND_FLAG_TERMINATE)
  1291. ? true
  1292. : false;
  1293. os_mutex_unlock(&exec_env->wait_lock);
  1294. return is_thread_terminated;
  1295. }
  1296. void
  1297. exception_lock(WASMModuleInstance *module_inst)
  1298. {
  1299. /*
  1300. * Note: this lock could be per module instance if desirable.
  1301. * We can revisit on AOT version bump.
  1302. * It probably doesn't matter though because the exception handling
  1303. * logic should not be executed too frequently anyway.
  1304. */
  1305. os_mutex_lock(&_exception_lock);
  1306. }
  1307. void
  1308. exception_unlock(WASMModuleInstance *module_inst)
  1309. {
  1310. os_mutex_unlock(&_exception_lock);
  1311. }
  1312. void
  1313. wasm_cluster_traverse_lock(WASMExecEnv *exec_env)
  1314. {
  1315. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  1316. bh_assert(cluster);
  1317. os_mutex_lock(&cluster->lock);
  1318. }
  1319. void
  1320. wasm_cluster_traverse_unlock(WASMExecEnv *exec_env)
  1321. {
  1322. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  1323. bh_assert(cluster);
  1324. os_mutex_unlock(&cluster->lock);
  1325. }