thread_manager.c 41 KB

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