thread_manager.c 41 KB

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