thread_manager.c 43 KB

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