thread_manager.c 26 KB

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