thread_manager.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. typedef void (*list_visitor)(void *, void *);
  25. static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM;
  26. /* Set the maximum thread number, if this function is not called,
  27. the max thread num is defined by CLUSTER_MAX_THREAD_NUM */
  28. void
  29. wasm_cluster_set_max_thread_num(uint32 num)
  30. {
  31. if (num > 0)
  32. cluster_max_thread_num = num;
  33. }
  34. bool
  35. thread_manager_init()
  36. {
  37. if (bh_list_init(cluster_list) != 0)
  38. return false;
  39. if (os_mutex_init(&cluster_list_lock) != 0)
  40. return false;
  41. return true;
  42. }
  43. void
  44. thread_manager_destroy()
  45. {
  46. WASMCluster *cluster = bh_list_first_elem(cluster_list);
  47. WASMCluster *next;
  48. while (cluster) {
  49. next = bh_list_elem_next(cluster);
  50. wasm_cluster_destroy(cluster);
  51. cluster = next;
  52. }
  53. wasm_cluster_cancel_all_callbacks();
  54. os_mutex_destroy(&cluster_list_lock);
  55. }
  56. static void
  57. traverse_list(bh_list *l, list_visitor visitor, void *user_data)
  58. {
  59. void *next, *node = bh_list_first_elem(l);
  60. while (node) {
  61. next = bh_list_elem_next(node);
  62. visitor(node, user_data);
  63. node = next;
  64. }
  65. }
  66. static bool
  67. allocate_aux_stack(WASMCluster *cluster, uint32 *start, uint32 *size)
  68. {
  69. uint32 i;
  70. /* If the module doesn't have aux stack info,
  71. it can't create any threads */
  72. if (!cluster->stack_segment_occupied)
  73. return false;
  74. os_mutex_lock(&cluster->lock);
  75. for (i = 0; i < cluster_max_thread_num; i++) {
  76. if (!cluster->stack_segment_occupied[i]) {
  77. if (start)
  78. *start = cluster->stack_tops[i];
  79. if (size)
  80. *size = cluster->stack_size;
  81. cluster->stack_segment_occupied[i] = true;
  82. os_mutex_unlock(&cluster->lock);
  83. return true;
  84. }
  85. }
  86. os_mutex_unlock(&cluster->lock);
  87. return false;
  88. }
  89. static bool
  90. free_aux_stack(WASMCluster *cluster, uint32 start)
  91. {
  92. uint32 i;
  93. for (i = 0; i < cluster_max_thread_num; i++) {
  94. if (start == cluster->stack_tops[i]) {
  95. os_mutex_lock(&cluster->lock);
  96. cluster->stack_segment_occupied[i] = false;
  97. os_mutex_unlock(&cluster->lock);
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. WASMCluster *
  104. wasm_cluster_create(WASMExecEnv *exec_env)
  105. {
  106. WASMCluster *cluster;
  107. uint64 total_size;
  108. uint32 aux_stack_start, aux_stack_size, i;
  109. bh_assert(exec_env->cluster == NULL);
  110. if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
  111. LOG_ERROR("thread manager error: failed to allocate memory");
  112. return NULL;
  113. }
  114. memset(cluster, 0, sizeof(WASMCluster));
  115. exec_env->cluster = cluster;
  116. bh_list_init(&cluster->exec_env_list);
  117. bh_list_insert(&cluster->exec_env_list, exec_env);
  118. if (os_mutex_init(&cluster->lock) != 0) {
  119. wasm_runtime_free(cluster);
  120. LOG_ERROR("thread manager error: failed to init mutex");
  121. return NULL;
  122. }
  123. /* Prepare the aux stack top and size for every thread */
  124. if (!wasm_exec_env_get_aux_stack(exec_env, &aux_stack_start,
  125. &aux_stack_size)) {
  126. LOG_VERBOSE("No aux stack info for this module, can't create thread");
  127. /* If the module don't have aux stack info, don't throw error here,
  128. but remain stack_tops and stack_segment_occupied as NULL */
  129. os_mutex_lock(&cluster_list_lock);
  130. if (bh_list_insert(cluster_list, cluster) != 0) {
  131. os_mutex_unlock(&cluster_list_lock);
  132. goto fail;
  133. }
  134. os_mutex_unlock(&cluster_list_lock);
  135. return cluster;
  136. }
  137. cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
  138. if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
  139. goto fail;
  140. }
  141. /* Make stack size 16-byte aligned */
  142. cluster->stack_size = cluster->stack_size & (~15);
  143. /* Set initial aux stack top to the instance and
  144. aux stack boundary to the main exec_env */
  145. if (!wasm_exec_env_set_aux_stack(exec_env, aux_stack_start,
  146. cluster->stack_size))
  147. goto fail;
  148. if (cluster_max_thread_num != 0) {
  149. total_size = cluster_max_thread_num * sizeof(uint32);
  150. if (total_size >= UINT32_MAX
  151. || !(cluster->stack_tops =
  152. wasm_runtime_malloc((uint32)total_size))) {
  153. goto fail;
  154. }
  155. memset(cluster->stack_tops, 0, (uint32)total_size);
  156. if (!(cluster->stack_segment_occupied =
  157. wasm_runtime_malloc(cluster_max_thread_num * sizeof(bool)))) {
  158. goto fail;
  159. }
  160. memset(cluster->stack_segment_occupied, 0,
  161. cluster_max_thread_num * sizeof(bool));
  162. /* Reserve space for main instance */
  163. aux_stack_start -= cluster->stack_size;
  164. for (i = 0; i < cluster_max_thread_num; i++) {
  165. cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
  166. }
  167. }
  168. os_mutex_lock(&cluster_list_lock);
  169. if (bh_list_insert(cluster_list, cluster) != 0) {
  170. os_mutex_unlock(&cluster_list_lock);
  171. goto fail;
  172. }
  173. os_mutex_unlock(&cluster_list_lock);
  174. return cluster;
  175. fail:
  176. if (cluster)
  177. wasm_cluster_destroy(cluster);
  178. return NULL;
  179. }
  180. static void
  181. destroy_cluster_visitor(void *node, void *user_data)
  182. {
  183. DestroyCallBackNode *destroy_node = (DestroyCallBackNode *)node;
  184. WASMCluster *cluster = (WASMCluster *)user_data;
  185. destroy_node->destroy_cb(cluster);
  186. }
  187. void
  188. wasm_cluster_destroy(WASMCluster *cluster)
  189. {
  190. traverse_list(destroy_callback_list, destroy_cluster_visitor,
  191. (void *)cluster);
  192. /* Remove the cluster from the cluster list */
  193. os_mutex_lock(&cluster_list_lock);
  194. bh_list_remove(cluster_list, cluster);
  195. os_mutex_unlock(&cluster_list_lock);
  196. os_mutex_destroy(&cluster->lock);
  197. if (cluster->stack_tops)
  198. wasm_runtime_free(cluster->stack_tops);
  199. if (cluster->stack_segment_occupied)
  200. wasm_runtime_free(cluster->stack_segment_occupied);
  201. #if WASM_ENABLE_DEBUG_INTERP != 0
  202. wasm_debug_instance_destroy(cluster);
  203. #endif
  204. wasm_runtime_free(cluster);
  205. }
  206. static void
  207. free_node_visitor(void *node, void *user_data)
  208. {
  209. wasm_runtime_free(node);
  210. }
  211. void
  212. wasm_cluster_cancel_all_callbacks()
  213. {
  214. traverse_list(destroy_callback_list, free_node_visitor, NULL);
  215. bh_list_init(destroy_callback_list);
  216. }
  217. WASMCluster *
  218. wasm_exec_env_get_cluster(WASMExecEnv *exec_env)
  219. {
  220. return exec_env->cluster;
  221. }
  222. bool
  223. wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  224. {
  225. bool ret = true;
  226. exec_env->cluster = cluster;
  227. os_mutex_lock(&cluster->lock);
  228. if (bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
  229. ret = false;
  230. os_mutex_unlock(&cluster->lock);
  231. return ret;
  232. }
  233. bool
  234. wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  235. {
  236. bool ret = true;
  237. bh_assert(exec_env->cluster == cluster);
  238. os_mutex_lock(&cluster->lock);
  239. if (bh_list_remove(&cluster->exec_env_list, exec_env) != 0)
  240. ret = false;
  241. os_mutex_unlock(&cluster->lock);
  242. if (cluster->exec_env_list.len == 0) {
  243. /* exec_env_list empty, destroy the cluster */
  244. wasm_cluster_destroy(cluster);
  245. }
  246. return ret;
  247. }
  248. static WASMExecEnv *
  249. wasm_cluster_search_exec_env(WASMCluster *cluster,
  250. WASMModuleInstanceCommon *module_inst)
  251. {
  252. WASMExecEnv *node = NULL;
  253. os_mutex_lock(&cluster->lock);
  254. node = bh_list_first_elem(&cluster->exec_env_list);
  255. while (node) {
  256. if (node->module_inst == module_inst) {
  257. os_mutex_unlock(&cluster->lock);
  258. return node;
  259. }
  260. node = bh_list_elem_next(node);
  261. }
  262. os_mutex_unlock(&cluster->lock);
  263. return NULL;
  264. }
  265. /* search the global cluster list to find if the given
  266. module instance have a corresponding exec_env */
  267. WASMExecEnv *
  268. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst)
  269. {
  270. WASMCluster *cluster = NULL;
  271. WASMExecEnv *exec_env = NULL;
  272. os_mutex_lock(&cluster_list_lock);
  273. cluster = bh_list_first_elem(cluster_list);
  274. while (cluster) {
  275. exec_env = wasm_cluster_search_exec_env(cluster, module_inst);
  276. if (exec_env) {
  277. os_mutex_unlock(&cluster_list_lock);
  278. return exec_env;
  279. }
  280. cluster = bh_list_elem_next(cluster);
  281. }
  282. os_mutex_unlock(&cluster_list_lock);
  283. return NULL;
  284. }
  285. WASMExecEnv *
  286. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
  287. {
  288. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  289. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  290. wasm_module_t module;
  291. wasm_module_inst_t new_module_inst;
  292. WASMExecEnv *new_exec_env;
  293. uint32 aux_stack_start, aux_stack_size;
  294. uint32 stack_size = 8192;
  295. if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
  296. return NULL;
  297. }
  298. #if WASM_ENABLE_INTERP != 0
  299. if (module_inst->module_type == Wasm_Module_Bytecode) {
  300. stack_size =
  301. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  302. }
  303. #endif
  304. #if WASM_ENABLE_AOT != 0
  305. if (module_inst->module_type == Wasm_Module_AoT) {
  306. stack_size =
  307. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  308. }
  309. #endif
  310. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  311. module, true, stack_size, 0, NULL, 0))) {
  312. return NULL;
  313. }
  314. /* Set custom_data to new module instance */
  315. wasm_runtime_set_custom_data_internal(
  316. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  317. new_exec_env = wasm_exec_env_create_internal(new_module_inst,
  318. exec_env->wasm_stack_size);
  319. if (!new_exec_env)
  320. goto fail1;
  321. if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
  322. LOG_ERROR("thread manager error: "
  323. "failed to allocate aux stack space for new thread");
  324. goto fail2;
  325. }
  326. /* Set aux stack for current thread */
  327. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  328. aux_stack_size)) {
  329. goto fail3;
  330. }
  331. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  332. goto fail3;
  333. return new_exec_env;
  334. fail3:
  335. /* free the allocated aux stack space */
  336. free_aux_stack(cluster, aux_stack_start);
  337. fail2:
  338. wasm_exec_env_destroy(new_exec_env);
  339. fail1:
  340. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  341. return NULL;
  342. }
  343. void
  344. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  345. {
  346. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  347. wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
  348. bh_assert(cluster != NULL);
  349. /* Free aux stack space */
  350. free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
  351. wasm_cluster_del_exec_env(cluster, exec_env);
  352. wasm_exec_env_destroy_internal(exec_env);
  353. wasm_runtime_deinstantiate_internal(module_inst, true);
  354. }
  355. /* start routine of thread manager */
  356. static void *
  357. thread_manager_start_routine(void *arg)
  358. {
  359. void *ret;
  360. WASMExecEnv *exec_env = (WASMExecEnv *)arg;
  361. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  362. bh_assert(cluster != NULL);
  363. exec_env->handle = os_self_thread();
  364. ret = exec_env->thread_start_routine(exec_env);
  365. #ifdef OS_ENABLE_HW_BOUND_CHECK
  366. if (exec_env->suspend_flags.flags & 0x08)
  367. ret = exec_env->thread_ret_value;
  368. #endif
  369. /* Routine exit */
  370. /* Free aux stack space */
  371. free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
  372. /* Detach the native thread here to ensure the resources are freed */
  373. wasm_cluster_detach_thread(exec_env);
  374. /* Remove and destroy exec_env */
  375. wasm_cluster_del_exec_env(cluster, exec_env);
  376. wasm_exec_env_destroy_internal(exec_env);
  377. os_thread_exit(ret);
  378. return ret;
  379. }
  380. int32
  381. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  382. wasm_module_inst_t module_inst,
  383. void *(*thread_routine)(void *), void *arg)
  384. {
  385. WASMCluster *cluster;
  386. WASMExecEnv *new_exec_env;
  387. uint32 aux_stack_start, aux_stack_size;
  388. korp_tid tid;
  389. cluster = wasm_exec_env_get_cluster(exec_env);
  390. bh_assert(cluster);
  391. new_exec_env =
  392. wasm_exec_env_create_internal(module_inst, exec_env->wasm_stack_size);
  393. if (!new_exec_env)
  394. return -1;
  395. if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
  396. LOG_ERROR("thread manager error: "
  397. "failed to allocate aux stack space for new thread");
  398. goto fail1;
  399. }
  400. /* Set aux stack for current thread */
  401. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  402. aux_stack_size)) {
  403. goto fail2;
  404. }
  405. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  406. goto fail2;
  407. new_exec_env->thread_start_routine = thread_routine;
  408. new_exec_env->thread_arg = arg;
  409. if (0
  410. != os_thread_create(&tid, thread_manager_start_routine,
  411. (void *)new_exec_env,
  412. APP_THREAD_STACK_SIZE_DEFAULT)) {
  413. goto fail3;
  414. }
  415. return 0;
  416. fail3:
  417. wasm_cluster_del_exec_env(cluster, new_exec_env);
  418. fail2:
  419. /* free the allocated aux stack space */
  420. free_aux_stack(cluster, aux_stack_start);
  421. fail1:
  422. wasm_exec_env_destroy(new_exec_env);
  423. return -1;
  424. }
  425. #if WASM_ENABLE_DEBUG_INTERP != 0
  426. WASMCurrentEnvStatus *
  427. wasm_cluster_create_exenv_status()
  428. {
  429. WASMCurrentEnvStatus *status;
  430. if (!(status = wasm_runtime_malloc(sizeof(WASMCurrentEnvStatus)))) {
  431. return NULL;
  432. }
  433. status->step_count = 0;
  434. status->signal_flag = 0;
  435. status->running_status = 0;
  436. return status;
  437. }
  438. void
  439. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status)
  440. {
  441. wasm_runtime_free(status);
  442. }
  443. inline static bool
  444. wasm_cluster_thread_is_running(WASMExecEnv *exec_env)
  445. {
  446. return exec_env->current_status->running_status == STATUS_RUNNING
  447. || exec_env->current_status->running_status == STATUS_STEP;
  448. }
  449. void
  450. wasm_cluster_clear_thread_signal(WASMExecEnv *exec_env)
  451. {
  452. exec_env->current_status->signal_flag = 0;
  453. }
  454. void
  455. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo)
  456. {
  457. exec_env->current_status->signal_flag = signo;
  458. }
  459. static void
  460. notify_debug_instance(WASMExecEnv *exec_env)
  461. {
  462. WASMCluster *cluster;
  463. cluster = wasm_exec_env_get_cluster(exec_env);
  464. bh_assert(cluster);
  465. if (!cluster->debug_inst) {
  466. return;
  467. }
  468. os_mutex_lock(&cluster->debug_inst->wait_lock);
  469. os_cond_signal(&cluster->debug_inst->wait_cond);
  470. os_mutex_unlock(&cluster->debug_inst->wait_lock);
  471. }
  472. void
  473. wasm_cluster_thread_stopped(WASMExecEnv *exec_env)
  474. {
  475. exec_env->current_status->running_status = STATUS_STOP;
  476. notify_debug_instance(exec_env);
  477. }
  478. void
  479. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env)
  480. {
  481. os_mutex_lock(&exec_env->wait_lock);
  482. while (!wasm_cluster_thread_is_running(exec_env)) {
  483. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  484. }
  485. os_mutex_unlock(&exec_env->wait_lock);
  486. }
  487. void
  488. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo)
  489. {
  490. WASMExecEnv *exec_env = bh_list_first_elem(&cluster->exec_env_list);
  491. while (exec_env) {
  492. wasm_cluster_thread_send_signal(exec_env, signo);
  493. exec_env = bh_list_elem_next(exec_env);
  494. }
  495. }
  496. void
  497. wasm_cluster_thread_exited(WASMExecEnv *exec_env)
  498. {
  499. exec_env->current_status->running_status = STATUS_EXIT;
  500. notify_debug_instance(exec_env);
  501. }
  502. void
  503. wasm_cluster_thread_continue(WASMExecEnv *exec_env)
  504. {
  505. wasm_cluster_clear_thread_signal(exec_env);
  506. exec_env->current_status->running_status = STATUS_RUNNING;
  507. os_cond_signal(&exec_env->wait_cond);
  508. }
  509. void
  510. wasm_cluster_thread_step(WASMExecEnv *exec_env)
  511. {
  512. exec_env->current_status->running_status = STATUS_STEP;
  513. os_cond_signal(&exec_env->wait_cond);
  514. }
  515. void
  516. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst)
  517. {
  518. cluster->debug_inst = inst;
  519. }
  520. #endif /* end of WASM_ENABLE_DEBUG_INTERP */
  521. int32
  522. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
  523. {
  524. return os_thread_join(exec_env->handle, ret_val);
  525. }
  526. int32
  527. wasm_cluster_detach_thread(WASMExecEnv *exec_env)
  528. {
  529. return os_thread_detach(exec_env->handle);
  530. }
  531. void
  532. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
  533. {
  534. WASMCluster *cluster;
  535. #ifdef OS_ENABLE_HW_BOUND_CHECK
  536. if (exec_env->jmpbuf_stack_top) {
  537. /* Store the return value in exec_env */
  538. exec_env->thread_ret_value = retval;
  539. exec_env->suspend_flags.flags |= 0x08;
  540. #ifndef BH_PLATFORM_WINDOWS
  541. /* Pop all jmpbuf_node except the last one */
  542. while (exec_env->jmpbuf_stack_top->prev) {
  543. wasm_exec_env_pop_jmpbuf(exec_env);
  544. }
  545. os_longjmp(exec_env->jmpbuf_stack_top->jmpbuf, 1);
  546. return;
  547. #endif
  548. }
  549. #endif
  550. cluster = wasm_exec_env_get_cluster(exec_env);
  551. bh_assert(cluster);
  552. #if WASM_ENABLE_DEBUG_INTERP != 0
  553. wasm_cluster_clear_thread_signal(exec_env);
  554. wasm_cluster_thread_exited(exec_env);
  555. #endif
  556. /* App exit the thread, free the resources before exit native thread */
  557. /* Free aux stack space */
  558. free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
  559. /* Detach the native thread here to ensure the resources are freed */
  560. wasm_cluster_detach_thread(exec_env);
  561. /* Remove and destroy exec_env */
  562. wasm_cluster_del_exec_env(cluster, exec_env);
  563. wasm_exec_env_destroy_internal(exec_env);
  564. os_thread_exit(retval);
  565. }
  566. int32
  567. wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
  568. {
  569. /* Set the termination flag */
  570. #if WASM_ENABLE_DEBUG_INTERP != 0
  571. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  572. wasm_cluster_thread_exited(exec_env);
  573. #else
  574. exec_env->suspend_flags.flags |= 0x01;
  575. #endif
  576. return 0;
  577. }
  578. static void
  579. terminate_thread_visitor(void *node, void *user_data)
  580. {
  581. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  582. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  583. if (curr_exec_env == exec_env)
  584. return;
  585. wasm_cluster_cancel_thread(curr_exec_env);
  586. wasm_cluster_join_thread(curr_exec_env, NULL);
  587. }
  588. void
  589. wasm_cluster_terminate_all(WASMCluster *cluster)
  590. {
  591. traverse_list(&cluster->exec_env_list, terminate_thread_visitor, NULL);
  592. }
  593. void
  594. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  595. WASMExecEnv *exec_env)
  596. {
  597. traverse_list(&cluster->exec_env_list, terminate_thread_visitor,
  598. (void *)exec_env);
  599. }
  600. bool
  601. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
  602. {
  603. DestroyCallBackNode *node;
  604. if (!(node = wasm_runtime_malloc(sizeof(DestroyCallBackNode)))) {
  605. LOG_ERROR("thread manager error: failed to allocate memory");
  606. return false;
  607. }
  608. node->destroy_cb = callback;
  609. bh_list_insert(destroy_callback_list, node);
  610. return true;
  611. }
  612. void
  613. wasm_cluster_suspend_thread(WASMExecEnv *exec_env)
  614. {
  615. /* Set the suspend flag */
  616. exec_env->suspend_flags.flags |= 0x02;
  617. }
  618. static void
  619. suspend_thread_visitor(void *node, void *user_data)
  620. {
  621. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  622. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  623. if (curr_exec_env == exec_env)
  624. return;
  625. wasm_cluster_suspend_thread(curr_exec_env);
  626. }
  627. void
  628. wasm_cluster_suspend_all(WASMCluster *cluster)
  629. {
  630. traverse_list(&cluster->exec_env_list, suspend_thread_visitor, NULL);
  631. }
  632. void
  633. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  634. WASMExecEnv *exec_env)
  635. {
  636. traverse_list(&cluster->exec_env_list, suspend_thread_visitor,
  637. (void *)exec_env);
  638. }
  639. void
  640. wasm_cluster_resume_thread(WASMExecEnv *exec_env)
  641. {
  642. exec_env->suspend_flags.flags &= ~0x02;
  643. os_cond_signal(&exec_env->wait_cond);
  644. }
  645. static void
  646. resume_thread_visitor(void *node, void *user_data)
  647. {
  648. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  649. wasm_cluster_resume_thread(curr_exec_env);
  650. }
  651. void
  652. wasm_cluster_resume_all(WASMCluster *cluster)
  653. {
  654. traverse_list(&cluster->exec_env_list, resume_thread_visitor, NULL);
  655. }
  656. static void
  657. set_exception_visitor(void *node, void *user_data)
  658. {
  659. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  660. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  661. WASMModuleInstanceCommon *module_inst = get_module_inst(exec_env);
  662. WASMModuleInstanceCommon *curr_module_inst = get_module_inst(curr_exec_env);
  663. const char *exception = wasm_runtime_get_exception(module_inst);
  664. /* skip "Exception: " */
  665. exception += 11;
  666. if (curr_exec_env != exec_env) {
  667. curr_module_inst = get_module_inst(curr_exec_env);
  668. wasm_runtime_set_exception(curr_module_inst, exception);
  669. }
  670. }
  671. void
  672. wasm_cluster_spread_exception(WASMExecEnv *exec_env)
  673. {
  674. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  675. bh_assert(cluster);
  676. traverse_list(&cluster->exec_env_list, set_exception_visitor, exec_env);
  677. }
  678. static void
  679. set_custom_data_visitor(void *node, void *user_data)
  680. {
  681. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  682. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  683. wasm_runtime_set_custom_data_internal(module_inst, user_data);
  684. }
  685. void
  686. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  687. void *custom_data)
  688. {
  689. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  690. WASMCluster *cluster = NULL;
  691. bh_assert(exec_env);
  692. cluster = wasm_exec_env_get_cluster(exec_env);
  693. bh_assert(cluster);
  694. traverse_list(&cluster->exec_env_list, set_custom_data_visitor,
  695. custom_data);
  696. }