thread_manager.c 22 KB

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