thread_manager.c 39 KB

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