thread_manager.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. #if WASM_ENABLE_DEBUG_INTERP != 0
  239. /* Wait for debugger control thread to process the
  240. stop event of this thread */
  241. if (cluster->debug_inst) {
  242. /* lock the debug_inst->wait_lock so
  243. other threads can't fire stop events */
  244. os_mutex_lock(&cluster->debug_inst->wait_lock);
  245. while (cluster->debug_inst->stopped_thread == exec_env) {
  246. os_cond_wait(&cluster->debug_inst->wait_cond,
  247. &cluster->debug_inst->wait_lock);
  248. }
  249. os_mutex_unlock(&cluster->debug_inst->wait_lock);
  250. }
  251. #endif
  252. os_mutex_lock(&cluster->lock);
  253. if (bh_list_remove(&cluster->exec_env_list, exec_env) != 0)
  254. ret = false;
  255. os_mutex_unlock(&cluster->lock);
  256. if (cluster->exec_env_list.len == 0) {
  257. /* exec_env_list empty, destroy the cluster */
  258. wasm_cluster_destroy(cluster);
  259. }
  260. return ret;
  261. }
  262. static WASMExecEnv *
  263. wasm_cluster_search_exec_env(WASMCluster *cluster,
  264. WASMModuleInstanceCommon *module_inst)
  265. {
  266. WASMExecEnv *node = NULL;
  267. os_mutex_lock(&cluster->lock);
  268. node = bh_list_first_elem(&cluster->exec_env_list);
  269. while (node) {
  270. if (node->module_inst == module_inst) {
  271. os_mutex_unlock(&cluster->lock);
  272. return node;
  273. }
  274. node = bh_list_elem_next(node);
  275. }
  276. os_mutex_unlock(&cluster->lock);
  277. return NULL;
  278. }
  279. /* search the global cluster list to find if the given
  280. module instance have a corresponding exec_env */
  281. WASMExecEnv *
  282. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst)
  283. {
  284. WASMCluster *cluster = NULL;
  285. WASMExecEnv *exec_env = NULL;
  286. os_mutex_lock(&cluster_list_lock);
  287. cluster = bh_list_first_elem(cluster_list);
  288. while (cluster) {
  289. exec_env = wasm_cluster_search_exec_env(cluster, module_inst);
  290. if (exec_env) {
  291. os_mutex_unlock(&cluster_list_lock);
  292. return exec_env;
  293. }
  294. cluster = bh_list_elem_next(cluster);
  295. }
  296. os_mutex_unlock(&cluster_list_lock);
  297. return NULL;
  298. }
  299. WASMExecEnv *
  300. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
  301. {
  302. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  303. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  304. wasm_module_t module;
  305. wasm_module_inst_t new_module_inst;
  306. #if WASM_ENABLE_LIBC_WASI != 0
  307. WASIContext *wasi_ctx;
  308. #endif
  309. WASMExecEnv *new_exec_env;
  310. uint32 aux_stack_start, aux_stack_size;
  311. uint32 stack_size = 8192;
  312. if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
  313. return NULL;
  314. }
  315. #if WASM_ENABLE_INTERP != 0
  316. if (module_inst->module_type == Wasm_Module_Bytecode) {
  317. stack_size =
  318. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  319. }
  320. #endif
  321. #if WASM_ENABLE_AOT != 0
  322. if (module_inst->module_type == Wasm_Module_AoT) {
  323. stack_size =
  324. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  325. }
  326. #endif
  327. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  328. module, true, stack_size, 0, NULL, 0))) {
  329. return NULL;
  330. }
  331. /* Set custom_data to new module instance */
  332. wasm_runtime_set_custom_data_internal(
  333. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  334. #if WASM_ENABLE_LIBC_WASI != 0
  335. wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  336. wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
  337. #endif
  338. new_exec_env = wasm_exec_env_create_internal(new_module_inst,
  339. exec_env->wasm_stack_size);
  340. if (!new_exec_env)
  341. goto fail1;
  342. if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
  343. LOG_ERROR("thread manager error: "
  344. "failed to allocate aux stack space for new thread");
  345. goto fail2;
  346. }
  347. /* Set aux stack for current thread */
  348. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  349. aux_stack_size)) {
  350. goto fail3;
  351. }
  352. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  353. goto fail3;
  354. return new_exec_env;
  355. fail3:
  356. /* free the allocated aux stack space */
  357. free_aux_stack(cluster, aux_stack_start);
  358. fail2:
  359. wasm_exec_env_destroy(new_exec_env);
  360. fail1:
  361. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  362. return NULL;
  363. }
  364. void
  365. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  366. {
  367. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  368. wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
  369. bh_assert(cluster != NULL);
  370. /* Free aux stack space */
  371. free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
  372. wasm_cluster_del_exec_env(cluster, exec_env);
  373. wasm_exec_env_destroy_internal(exec_env);
  374. wasm_runtime_deinstantiate_internal(module_inst, true);
  375. }
  376. /* start routine of thread manager */
  377. static void *
  378. thread_manager_start_routine(void *arg)
  379. {
  380. void *ret;
  381. WASMExecEnv *exec_env = (WASMExecEnv *)arg;
  382. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  383. bh_assert(cluster != NULL);
  384. exec_env->handle = os_self_thread();
  385. ret = exec_env->thread_start_routine(exec_env);
  386. #ifdef OS_ENABLE_HW_BOUND_CHECK
  387. if (exec_env->suspend_flags.flags & 0x08)
  388. ret = exec_env->thread_ret_value;
  389. #endif
  390. /* Routine exit */
  391. /* Free aux stack space */
  392. free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
  393. /* Detach the native thread here to ensure the resources are freed */
  394. wasm_cluster_detach_thread(exec_env);
  395. #if WASM_ENABLE_DEBUG_INTERP != 0
  396. wasm_cluster_thread_exited(exec_env);
  397. #endif
  398. /* Remove and destroy exec_env */
  399. wasm_cluster_del_exec_env(cluster, exec_env);
  400. wasm_exec_env_destroy_internal(exec_env);
  401. os_thread_exit(ret);
  402. return ret;
  403. }
  404. int32
  405. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  406. wasm_module_inst_t module_inst,
  407. void *(*thread_routine)(void *), void *arg)
  408. {
  409. WASMCluster *cluster;
  410. WASMExecEnv *new_exec_env;
  411. uint32 aux_stack_start, aux_stack_size;
  412. korp_tid tid;
  413. cluster = wasm_exec_env_get_cluster(exec_env);
  414. bh_assert(cluster);
  415. new_exec_env =
  416. wasm_exec_env_create_internal(module_inst, exec_env->wasm_stack_size);
  417. if (!new_exec_env)
  418. return -1;
  419. if (!allocate_aux_stack(cluster, &aux_stack_start, &aux_stack_size)) {
  420. LOG_ERROR("thread manager error: "
  421. "failed to allocate aux stack space for new thread");
  422. goto fail1;
  423. }
  424. /* Set aux stack for current thread */
  425. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  426. aux_stack_size)) {
  427. goto fail2;
  428. }
  429. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  430. goto fail2;
  431. new_exec_env->thread_start_routine = thread_routine;
  432. new_exec_env->thread_arg = arg;
  433. if (0
  434. != os_thread_create(&tid, thread_manager_start_routine,
  435. (void *)new_exec_env,
  436. APP_THREAD_STACK_SIZE_DEFAULT)) {
  437. goto fail3;
  438. }
  439. return 0;
  440. fail3:
  441. wasm_cluster_del_exec_env(cluster, new_exec_env);
  442. fail2:
  443. /* free the allocated aux stack space */
  444. free_aux_stack(cluster, aux_stack_start);
  445. fail1:
  446. wasm_exec_env_destroy(new_exec_env);
  447. return -1;
  448. }
  449. #if WASM_ENABLE_DEBUG_INTERP != 0
  450. WASMCurrentEnvStatus *
  451. wasm_cluster_create_exenv_status()
  452. {
  453. WASMCurrentEnvStatus *status;
  454. if (!(status = wasm_runtime_malloc(sizeof(WASMCurrentEnvStatus)))) {
  455. return NULL;
  456. }
  457. status->step_count = 0;
  458. status->signal_flag = 0;
  459. status->running_status = 0;
  460. return status;
  461. }
  462. void
  463. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status)
  464. {
  465. wasm_runtime_free(status);
  466. }
  467. inline static bool
  468. wasm_cluster_thread_is_running(WASMExecEnv *exec_env)
  469. {
  470. return exec_env->current_status->running_status == STATUS_RUNNING
  471. || exec_env->current_status->running_status == STATUS_STEP;
  472. }
  473. void
  474. wasm_cluster_clear_thread_signal(WASMExecEnv *exec_env)
  475. {
  476. exec_env->current_status->signal_flag = 0;
  477. }
  478. void
  479. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo)
  480. {
  481. exec_env->current_status->signal_flag = signo;
  482. }
  483. static void
  484. notify_debug_instance(WASMExecEnv *exec_env)
  485. {
  486. WASMCluster *cluster;
  487. cluster = wasm_exec_env_get_cluster(exec_env);
  488. bh_assert(cluster);
  489. if (!cluster->debug_inst) {
  490. return;
  491. }
  492. on_thread_stop_event(cluster->debug_inst, exec_env);
  493. }
  494. void
  495. wasm_cluster_thread_stopped(WASMExecEnv *exec_env)
  496. {
  497. exec_env->current_status->running_status = STATUS_STOP;
  498. notify_debug_instance(exec_env);
  499. }
  500. void
  501. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env)
  502. {
  503. os_mutex_lock(&exec_env->wait_lock);
  504. while (!wasm_cluster_thread_is_running(exec_env)) {
  505. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  506. }
  507. os_mutex_unlock(&exec_env->wait_lock);
  508. }
  509. void
  510. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo)
  511. {
  512. WASMExecEnv *exec_env = bh_list_first_elem(&cluster->exec_env_list);
  513. while (exec_env) {
  514. wasm_cluster_thread_send_signal(exec_env, signo);
  515. exec_env = bh_list_elem_next(exec_env);
  516. }
  517. }
  518. void
  519. wasm_cluster_thread_exited(WASMExecEnv *exec_env)
  520. {
  521. exec_env->current_status->running_status = STATUS_EXIT;
  522. notify_debug_instance(exec_env);
  523. }
  524. void
  525. wasm_cluster_thread_continue(WASMExecEnv *exec_env)
  526. {
  527. wasm_cluster_clear_thread_signal(exec_env);
  528. exec_env->current_status->running_status = STATUS_RUNNING;
  529. os_cond_signal(&exec_env->wait_cond);
  530. }
  531. void
  532. wasm_cluster_thread_step(WASMExecEnv *exec_env)
  533. {
  534. exec_env->current_status->running_status = STATUS_STEP;
  535. os_cond_signal(&exec_env->wait_cond);
  536. }
  537. void
  538. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst)
  539. {
  540. cluster->debug_inst = inst;
  541. }
  542. #endif /* end of WASM_ENABLE_DEBUG_INTERP */
  543. /* Check whether the exec_env is in one of all clusters, the caller
  544. should add lock to the cluster list before calling us */
  545. static bool
  546. clusters_have_exec_env(WASMExecEnv *exec_env)
  547. {
  548. WASMCluster *cluster = bh_list_first_elem(cluster_list);
  549. WASMExecEnv *node;
  550. while (cluster) {
  551. node = bh_list_first_elem(&cluster->exec_env_list);
  552. while (node) {
  553. if (node == exec_env) {
  554. bh_assert(exec_env->cluster == cluster);
  555. return true;
  556. }
  557. node = bh_list_elem_next(node);
  558. }
  559. cluster = bh_list_elem_next(cluster);
  560. }
  561. return false;
  562. }
  563. int32
  564. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
  565. {
  566. korp_tid handle;
  567. os_mutex_lock(&cluster_list_lock);
  568. if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
  569. /* Invalid thread, thread has exited or thread has been detached */
  570. if (ret_val)
  571. *ret_val = NULL;
  572. os_mutex_unlock(&cluster_list_lock);
  573. return 0;
  574. }
  575. exec_env->wait_count++;
  576. handle = exec_env->handle;
  577. os_mutex_unlock(&cluster_list_lock);
  578. return os_thread_join(handle, ret_val);
  579. }
  580. int32
  581. wasm_cluster_detach_thread(WASMExecEnv *exec_env)
  582. {
  583. int32 ret = 0;
  584. os_mutex_lock(&cluster_list_lock);
  585. if (!clusters_have_exec_env(exec_env)) {
  586. /* Invalid thread or the thread has exited */
  587. os_mutex_unlock(&cluster_list_lock);
  588. return 0;
  589. }
  590. if (exec_env->wait_count == 0) {
  591. /* Only detach current thread when there is no other thread
  592. joining it, otherwise let the system resources for the
  593. thread be released after joining */
  594. ret = os_thread_detach(exec_env->handle);
  595. exec_env->thread_is_detached = true;
  596. }
  597. os_mutex_unlock(&cluster_list_lock);
  598. return ret;
  599. }
  600. void
  601. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
  602. {
  603. WASMCluster *cluster;
  604. #ifdef OS_ENABLE_HW_BOUND_CHECK
  605. if (exec_env->jmpbuf_stack_top) {
  606. /* Store the return value in exec_env */
  607. exec_env->thread_ret_value = retval;
  608. exec_env->suspend_flags.flags |= 0x08;
  609. #ifndef BH_PLATFORM_WINDOWS
  610. /* Pop all jmpbuf_node except the last one */
  611. while (exec_env->jmpbuf_stack_top->prev) {
  612. wasm_exec_env_pop_jmpbuf(exec_env);
  613. }
  614. os_longjmp(exec_env->jmpbuf_stack_top->jmpbuf, 1);
  615. return;
  616. #endif
  617. }
  618. #endif
  619. cluster = wasm_exec_env_get_cluster(exec_env);
  620. bh_assert(cluster);
  621. #if WASM_ENABLE_DEBUG_INTERP != 0
  622. wasm_cluster_clear_thread_signal(exec_env);
  623. wasm_cluster_thread_exited(exec_env);
  624. #endif
  625. /* App exit the thread, free the resources before exit native thread */
  626. /* Free aux stack space */
  627. free_aux_stack(cluster, exec_env->aux_stack_bottom.bottom);
  628. /* Detach the native thread here to ensure the resources are freed */
  629. wasm_cluster_detach_thread(exec_env);
  630. /* Remove and destroy exec_env */
  631. wasm_cluster_del_exec_env(cluster, exec_env);
  632. wasm_exec_env_destroy_internal(exec_env);
  633. os_thread_exit(retval);
  634. }
  635. int32
  636. wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
  637. {
  638. os_mutex_lock(&cluster_list_lock);
  639. if (!clusters_have_exec_env(exec_env)) {
  640. /* Invalid thread or the thread has exited */
  641. os_mutex_unlock(&cluster_list_lock);
  642. return 0;
  643. }
  644. os_mutex_unlock(&cluster_list_lock);
  645. /* Set the termination flag */
  646. #if WASM_ENABLE_DEBUG_INTERP != 0
  647. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  648. #else
  649. exec_env->suspend_flags.flags |= 0x01;
  650. #endif
  651. return 0;
  652. }
  653. static void
  654. terminate_thread_visitor(void *node, void *user_data)
  655. {
  656. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  657. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  658. if (curr_exec_env == exec_env)
  659. return;
  660. wasm_cluster_cancel_thread(curr_exec_env);
  661. wasm_cluster_join_thread(curr_exec_env, NULL);
  662. }
  663. void
  664. wasm_cluster_terminate_all(WASMCluster *cluster)
  665. {
  666. traverse_list(&cluster->exec_env_list, terminate_thread_visitor, NULL);
  667. }
  668. void
  669. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  670. WASMExecEnv *exec_env)
  671. {
  672. traverse_list(&cluster->exec_env_list, terminate_thread_visitor,
  673. (void *)exec_env);
  674. }
  675. static void
  676. wait_for_thread_visitor(void *node, void *user_data)
  677. {
  678. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  679. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  680. if (curr_exec_env == exec_env)
  681. return;
  682. wasm_cluster_join_thread(curr_exec_env, NULL);
  683. }
  684. void
  685. wams_cluster_wait_for_all(WASMCluster *cluster)
  686. {
  687. traverse_list(&cluster->exec_env_list, wait_for_thread_visitor, NULL);
  688. }
  689. void
  690. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  691. WASMExecEnv *exec_env)
  692. {
  693. traverse_list(&cluster->exec_env_list, wait_for_thread_visitor,
  694. (void *)exec_env);
  695. }
  696. bool
  697. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
  698. {
  699. DestroyCallBackNode *node;
  700. if (!(node = wasm_runtime_malloc(sizeof(DestroyCallBackNode)))) {
  701. LOG_ERROR("thread manager error: failed to allocate memory");
  702. return false;
  703. }
  704. node->destroy_cb = callback;
  705. bh_list_insert(destroy_callback_list, node);
  706. return true;
  707. }
  708. void
  709. wasm_cluster_suspend_thread(WASMExecEnv *exec_env)
  710. {
  711. /* Set the suspend flag */
  712. exec_env->suspend_flags.flags |= 0x02;
  713. }
  714. static void
  715. suspend_thread_visitor(void *node, void *user_data)
  716. {
  717. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  718. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  719. if (curr_exec_env == exec_env)
  720. return;
  721. wasm_cluster_suspend_thread(curr_exec_env);
  722. }
  723. void
  724. wasm_cluster_suspend_all(WASMCluster *cluster)
  725. {
  726. traverse_list(&cluster->exec_env_list, suspend_thread_visitor, NULL);
  727. }
  728. void
  729. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  730. WASMExecEnv *exec_env)
  731. {
  732. traverse_list(&cluster->exec_env_list, suspend_thread_visitor,
  733. (void *)exec_env);
  734. }
  735. void
  736. wasm_cluster_resume_thread(WASMExecEnv *exec_env)
  737. {
  738. exec_env->suspend_flags.flags &= ~0x02;
  739. os_cond_signal(&exec_env->wait_cond);
  740. }
  741. static void
  742. resume_thread_visitor(void *node, void *user_data)
  743. {
  744. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  745. wasm_cluster_resume_thread(curr_exec_env);
  746. }
  747. void
  748. wasm_cluster_resume_all(WASMCluster *cluster)
  749. {
  750. traverse_list(&cluster->exec_env_list, resume_thread_visitor, NULL);
  751. }
  752. static void
  753. set_exception_visitor(void *node, void *user_data)
  754. {
  755. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  756. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  757. WASMModuleInstanceCommon *module_inst = get_module_inst(exec_env);
  758. WASMModuleInstanceCommon *curr_module_inst = get_module_inst(curr_exec_env);
  759. const char *exception = wasm_runtime_get_exception(module_inst);
  760. /* skip "Exception: " */
  761. exception += 11;
  762. if (curr_exec_env != exec_env) {
  763. curr_module_inst = get_module_inst(curr_exec_env);
  764. wasm_runtime_set_exception(curr_module_inst, exception);
  765. }
  766. }
  767. void
  768. wasm_cluster_spread_exception(WASMExecEnv *exec_env)
  769. {
  770. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  771. bh_assert(cluster);
  772. traverse_list(&cluster->exec_env_list, set_exception_visitor, exec_env);
  773. }
  774. static void
  775. set_custom_data_visitor(void *node, void *user_data)
  776. {
  777. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  778. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  779. wasm_runtime_set_custom_data_internal(module_inst, user_data);
  780. }
  781. void
  782. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  783. void *custom_data)
  784. {
  785. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  786. if (exec_env == NULL) {
  787. /* Maybe threads have not been started yet. */
  788. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  789. }
  790. else {
  791. WASMCluster *cluster;
  792. cluster = wasm_exec_env_get_cluster(exec_env);
  793. bh_assert(cluster);
  794. traverse_list(&cluster->exec_env_list, set_custom_data_visitor,
  795. custom_data);
  796. }
  797. }