thread_manager.c 35 KB

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