thread_manager.c 39 KB

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