thread_manager.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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(WASMExecEnv *exec_env, uint32 *start, uint32 *size)
  68. {
  69. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  70. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
  71. WASMModuleInstanceCommon *module_inst =
  72. wasm_exec_env_get_module_inst(exec_env);
  73. uint32 stack_end;
  74. stack_end =
  75. wasm_runtime_module_malloc(module_inst, cluster->stack_size, NULL);
  76. *start = stack_end + cluster->stack_size;
  77. *size = cluster->stack_size;
  78. return stack_end != 0;
  79. #else
  80. uint32 i;
  81. /* If the module doesn't have aux stack info,
  82. it can't create any threads */
  83. if (!cluster->stack_segment_occupied)
  84. return false;
  85. os_mutex_lock(&cluster->lock);
  86. for (i = 0; i < cluster_max_thread_num; i++) {
  87. if (!cluster->stack_segment_occupied[i]) {
  88. if (start)
  89. *start = cluster->stack_tops[i];
  90. if (size)
  91. *size = cluster->stack_size;
  92. cluster->stack_segment_occupied[i] = true;
  93. os_mutex_unlock(&cluster->lock);
  94. return true;
  95. }
  96. }
  97. os_mutex_unlock(&cluster->lock);
  98. return false;
  99. #endif
  100. }
  101. static bool
  102. free_aux_stack(WASMExecEnv *exec_env, uint32 start)
  103. {
  104. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  105. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
  106. WASMModuleInstanceCommon *module_inst =
  107. wasm_exec_env_get_module_inst(exec_env);
  108. if (!wasm_exec_env_is_aux_stack_managed_by_runtime(exec_env)) {
  109. return true;
  110. }
  111. bh_assert(start >= cluster->stack_size);
  112. wasm_runtime_module_free(module_inst, start - cluster->stack_size);
  113. return true;
  114. #else
  115. uint32 i;
  116. for (i = 0; i < cluster_max_thread_num; i++) {
  117. if (start == cluster->stack_tops[i]) {
  118. os_mutex_lock(&cluster->lock);
  119. cluster->stack_segment_occupied[i] = false;
  120. os_mutex_unlock(&cluster->lock);
  121. return true;
  122. }
  123. }
  124. return false;
  125. #endif
  126. }
  127. WASMCluster *
  128. wasm_cluster_create(WASMExecEnv *exec_env)
  129. {
  130. WASMCluster *cluster;
  131. uint32 aux_stack_start, aux_stack_size;
  132. bh_assert(exec_env->cluster == NULL);
  133. if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
  134. LOG_ERROR("thread manager error: failed to allocate memory");
  135. return NULL;
  136. }
  137. memset(cluster, 0, sizeof(WASMCluster));
  138. exec_env->cluster = cluster;
  139. bh_list_init(&cluster->exec_env_list);
  140. bh_list_insert(&cluster->exec_env_list, exec_env);
  141. if (os_mutex_init(&cluster->lock) != 0) {
  142. wasm_runtime_free(cluster);
  143. LOG_ERROR("thread manager error: failed to init mutex");
  144. return NULL;
  145. }
  146. /* Prepare the aux stack top and size for every thread */
  147. if (!wasm_exec_env_get_aux_stack(exec_env, &aux_stack_start,
  148. &aux_stack_size)) {
  149. LOG_VERBOSE("No aux stack info for this module, can't create thread");
  150. /* If the module don't have aux stack info, don't throw error here,
  151. but remain stack_tops and stack_segment_occupied as NULL */
  152. os_mutex_lock(&cluster_list_lock);
  153. if (bh_list_insert(cluster_list, cluster) != 0) {
  154. os_mutex_unlock(&cluster_list_lock);
  155. goto fail;
  156. }
  157. os_mutex_unlock(&cluster_list_lock);
  158. return cluster;
  159. }
  160. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
  161. cluster->stack_size = aux_stack_size;
  162. #else
  163. cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
  164. if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
  165. goto fail;
  166. }
  167. /* Make stack size 16-byte aligned */
  168. cluster->stack_size = cluster->stack_size & (~15);
  169. #endif
  170. /* Set initial aux stack top to the instance and
  171. aux stack boundary to the main exec_env */
  172. if (!wasm_exec_env_set_aux_stack(exec_env, aux_stack_start,
  173. cluster->stack_size))
  174. goto fail;
  175. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  176. if (cluster_max_thread_num != 0) {
  177. uint64 total_size = cluster_max_thread_num * sizeof(uint32);
  178. uint32 i;
  179. if (total_size >= UINT32_MAX
  180. || !(cluster->stack_tops =
  181. wasm_runtime_malloc((uint32)total_size))) {
  182. goto fail;
  183. }
  184. memset(cluster->stack_tops, 0, (uint32)total_size);
  185. if (!(cluster->stack_segment_occupied =
  186. wasm_runtime_malloc(cluster_max_thread_num * sizeof(bool)))) {
  187. goto fail;
  188. }
  189. memset(cluster->stack_segment_occupied, 0,
  190. cluster_max_thread_num * sizeof(bool));
  191. /* Reserve space for main instance */
  192. aux_stack_start -= cluster->stack_size;
  193. for (i = 0; i < cluster_max_thread_num; i++) {
  194. cluster->stack_tops[i] = aux_stack_start - cluster->stack_size * i;
  195. }
  196. }
  197. #endif
  198. os_mutex_lock(&cluster_list_lock);
  199. if (bh_list_insert(cluster_list, cluster) != 0) {
  200. os_mutex_unlock(&cluster_list_lock);
  201. goto fail;
  202. }
  203. os_mutex_unlock(&cluster_list_lock);
  204. return cluster;
  205. fail:
  206. if (cluster)
  207. wasm_cluster_destroy(cluster);
  208. return NULL;
  209. }
  210. static void
  211. destroy_cluster_visitor(void *node, void *user_data)
  212. {
  213. DestroyCallBackNode *destroy_node = (DestroyCallBackNode *)node;
  214. WASMCluster *cluster = (WASMCluster *)user_data;
  215. destroy_node->destroy_cb(cluster);
  216. }
  217. void
  218. wasm_cluster_destroy(WASMCluster *cluster)
  219. {
  220. traverse_list(destroy_callback_list, destroy_cluster_visitor,
  221. (void *)cluster);
  222. /* Remove the cluster from the cluster list */
  223. os_mutex_lock(&cluster_list_lock);
  224. bh_list_remove(cluster_list, cluster);
  225. os_mutex_unlock(&cluster_list_lock);
  226. os_mutex_destroy(&cluster->lock);
  227. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  228. if (cluster->stack_tops)
  229. wasm_runtime_free(cluster->stack_tops);
  230. if (cluster->stack_segment_occupied)
  231. wasm_runtime_free(cluster->stack_segment_occupied);
  232. #endif
  233. #if WASM_ENABLE_DEBUG_INTERP != 0
  234. wasm_debug_instance_destroy(cluster);
  235. #endif
  236. wasm_runtime_free(cluster);
  237. }
  238. static void
  239. free_node_visitor(void *node, void *user_data)
  240. {
  241. wasm_runtime_free(node);
  242. }
  243. void
  244. wasm_cluster_cancel_all_callbacks()
  245. {
  246. traverse_list(destroy_callback_list, free_node_visitor, NULL);
  247. bh_list_init(destroy_callback_list);
  248. }
  249. WASMCluster *
  250. wasm_exec_env_get_cluster(WASMExecEnv *exec_env)
  251. {
  252. return exec_env->cluster;
  253. }
  254. bool
  255. wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  256. {
  257. bool ret = true;
  258. exec_env->cluster = cluster;
  259. os_mutex_lock(&cluster->lock);
  260. if (cluster->exec_env_list.len == cluster_max_thread_num + 1) {
  261. LOG_ERROR("thread manager error: "
  262. "maximum number of threads exceeded");
  263. ret = false;
  264. }
  265. if (ret && bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
  266. ret = false;
  267. os_mutex_unlock(&cluster->lock);
  268. return ret;
  269. }
  270. bool
  271. wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
  272. {
  273. bool ret = true;
  274. bh_assert(exec_env->cluster == cluster);
  275. #if WASM_ENABLE_DEBUG_INTERP != 0
  276. /* Wait for debugger control thread to process the
  277. stop event of this thread */
  278. if (cluster->debug_inst) {
  279. /* lock the debug_inst->wait_lock so
  280. other threads can't fire stop events */
  281. os_mutex_lock(&cluster->debug_inst->wait_lock);
  282. while (cluster->debug_inst->stopped_thread == exec_env) {
  283. /* either wakes up by signal or by 1-second timeout */
  284. os_cond_reltimedwait(&cluster->debug_inst->wait_cond,
  285. &cluster->debug_inst->wait_lock, 1000000);
  286. }
  287. os_mutex_unlock(&cluster->debug_inst->wait_lock);
  288. }
  289. #endif
  290. os_mutex_lock(&cluster->lock);
  291. if (bh_list_remove(&cluster->exec_env_list, exec_env) != 0)
  292. ret = false;
  293. os_mutex_unlock(&cluster->lock);
  294. if (cluster->exec_env_list.len == 0) {
  295. /* exec_env_list empty, destroy the cluster */
  296. wasm_cluster_destroy(cluster);
  297. }
  298. return ret;
  299. }
  300. static WASMExecEnv *
  301. wasm_cluster_search_exec_env(WASMCluster *cluster,
  302. WASMModuleInstanceCommon *module_inst)
  303. {
  304. WASMExecEnv *node = NULL;
  305. os_mutex_lock(&cluster->lock);
  306. node = bh_list_first_elem(&cluster->exec_env_list);
  307. while (node) {
  308. if (node->module_inst == module_inst) {
  309. os_mutex_unlock(&cluster->lock);
  310. return node;
  311. }
  312. node = bh_list_elem_next(node);
  313. }
  314. os_mutex_unlock(&cluster->lock);
  315. return NULL;
  316. }
  317. /* search the global cluster list to find if the given
  318. module instance have a corresponding exec_env */
  319. WASMExecEnv *
  320. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst)
  321. {
  322. WASMCluster *cluster = NULL;
  323. WASMExecEnv *exec_env = NULL;
  324. os_mutex_lock(&cluster_list_lock);
  325. cluster = bh_list_first_elem(cluster_list);
  326. while (cluster) {
  327. exec_env = wasm_cluster_search_exec_env(cluster, module_inst);
  328. if (exec_env) {
  329. os_mutex_unlock(&cluster_list_lock);
  330. return exec_env;
  331. }
  332. cluster = bh_list_elem_next(cluster);
  333. }
  334. os_mutex_unlock(&cluster_list_lock);
  335. return NULL;
  336. }
  337. WASMExecEnv *
  338. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
  339. {
  340. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  341. wasm_module_inst_t module_inst = get_module_inst(exec_env);
  342. wasm_module_t module;
  343. wasm_module_inst_t new_module_inst;
  344. #if WASM_ENABLE_LIBC_WASI != 0
  345. WASIContext *wasi_ctx;
  346. #endif
  347. WASMExecEnv *new_exec_env;
  348. uint32 aux_stack_start, aux_stack_size;
  349. uint32 stack_size = 8192;
  350. if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
  351. return NULL;
  352. }
  353. #if WASM_ENABLE_INTERP != 0
  354. if (module_inst->module_type == Wasm_Module_Bytecode) {
  355. stack_size =
  356. ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
  357. }
  358. #endif
  359. #if WASM_ENABLE_AOT != 0
  360. if (module_inst->module_type == Wasm_Module_AoT) {
  361. stack_size =
  362. ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
  363. }
  364. #endif
  365. if (!(new_module_inst = wasm_runtime_instantiate_internal(
  366. module, true, stack_size, 0, NULL, 0))) {
  367. return NULL;
  368. }
  369. /* Set custom_data to new module instance */
  370. wasm_runtime_set_custom_data_internal(
  371. new_module_inst, wasm_runtime_get_custom_data(module_inst));
  372. #if WASM_ENABLE_LIBC_WASI != 0
  373. wasi_ctx = wasm_runtime_get_wasi_ctx(module_inst);
  374. wasm_runtime_set_wasi_ctx(new_module_inst, wasi_ctx);
  375. #endif
  376. new_exec_env = wasm_exec_env_create_internal(new_module_inst,
  377. exec_env->wasm_stack_size);
  378. if (!new_exec_env)
  379. goto fail1;
  380. if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
  381. LOG_ERROR("thread manager error: "
  382. "failed to allocate aux stack space for new thread");
  383. goto fail2;
  384. }
  385. /* Set aux stack for current thread */
  386. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  387. aux_stack_size)) {
  388. goto fail3;
  389. }
  390. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  391. goto fail3;
  392. return new_exec_env;
  393. fail3:
  394. /* free the allocated aux stack space */
  395. free_aux_stack(exec_env, aux_stack_start);
  396. fail2:
  397. wasm_exec_env_destroy(new_exec_env);
  398. fail1:
  399. wasm_runtime_deinstantiate_internal(new_module_inst, true);
  400. return NULL;
  401. }
  402. void
  403. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
  404. {
  405. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  406. wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
  407. bh_assert(cluster != NULL);
  408. /* Free aux stack space */
  409. free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
  410. wasm_cluster_del_exec_env(cluster, exec_env);
  411. wasm_exec_env_destroy_internal(exec_env);
  412. wasm_runtime_deinstantiate_internal(module_inst, true);
  413. }
  414. /* start routine of thread manager */
  415. static void *
  416. thread_manager_start_routine(void *arg)
  417. {
  418. void *ret;
  419. WASMExecEnv *exec_env = (WASMExecEnv *)arg;
  420. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  421. WASMModuleInstanceCommon *module_inst =
  422. wasm_exec_env_get_module_inst(exec_env);
  423. bh_assert(cluster != NULL);
  424. bh_assert(module_inst != NULL);
  425. exec_env->handle = os_self_thread();
  426. ret = exec_env->thread_start_routine(exec_env);
  427. #ifdef OS_ENABLE_HW_BOUND_CHECK
  428. if (exec_env->suspend_flags.flags & 0x08)
  429. ret = exec_env->thread_ret_value;
  430. #endif
  431. /* Routine exit */
  432. /* Free aux stack space */
  433. free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
  434. /* routine exit, destroy instance */
  435. wasm_runtime_deinstantiate_internal(module_inst, true);
  436. /* Detach the native thread here to ensure the resources are freed */
  437. wasm_cluster_detach_thread(exec_env);
  438. #if WASM_ENABLE_DEBUG_INTERP != 0
  439. wasm_cluster_thread_exited(exec_env);
  440. #endif
  441. /* Remove and destroy exec_env */
  442. wasm_cluster_del_exec_env(cluster, exec_env);
  443. wasm_exec_env_destroy_internal(exec_env);
  444. os_thread_exit(ret);
  445. return ret;
  446. }
  447. int32
  448. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  449. wasm_module_inst_t module_inst, bool alloc_aux_stack,
  450. void *(*thread_routine)(void *), void *arg)
  451. {
  452. WASMCluster *cluster;
  453. WASMExecEnv *new_exec_env;
  454. uint32 aux_stack_start, aux_stack_size;
  455. korp_tid tid;
  456. cluster = wasm_exec_env_get_cluster(exec_env);
  457. bh_assert(cluster);
  458. new_exec_env =
  459. wasm_exec_env_create_internal(module_inst, exec_env->wasm_stack_size);
  460. if (!new_exec_env)
  461. return -1;
  462. if (alloc_aux_stack) {
  463. if (!allocate_aux_stack(exec_env, &aux_stack_start, &aux_stack_size)) {
  464. LOG_ERROR("thread manager error: "
  465. "failed to allocate aux stack space for new thread");
  466. goto fail1;
  467. }
  468. /* Set aux stack for current thread */
  469. if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
  470. aux_stack_size)) {
  471. goto fail2;
  472. }
  473. }
  474. if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
  475. goto fail2;
  476. new_exec_env->thread_start_routine = thread_routine;
  477. new_exec_env->thread_arg = arg;
  478. if (0
  479. != os_thread_create(&tid, thread_manager_start_routine,
  480. (void *)new_exec_env,
  481. APP_THREAD_STACK_SIZE_DEFAULT)) {
  482. goto fail3;
  483. }
  484. return 0;
  485. fail3:
  486. wasm_cluster_del_exec_env(cluster, new_exec_env);
  487. fail2:
  488. /* free the allocated aux stack space */
  489. if (alloc_aux_stack)
  490. free_aux_stack(exec_env, aux_stack_start);
  491. fail1:
  492. wasm_exec_env_destroy(new_exec_env);
  493. return -1;
  494. }
  495. #if WASM_ENABLE_DEBUG_INTERP != 0
  496. WASMCurrentEnvStatus *
  497. wasm_cluster_create_exenv_status()
  498. {
  499. WASMCurrentEnvStatus *status;
  500. if (!(status = wasm_runtime_malloc(sizeof(WASMCurrentEnvStatus)))) {
  501. return NULL;
  502. }
  503. status->step_count = 0;
  504. status->signal_flag = 0;
  505. status->running_status = 0;
  506. return status;
  507. }
  508. void
  509. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status)
  510. {
  511. wasm_runtime_free(status);
  512. }
  513. inline static bool
  514. wasm_cluster_thread_is_running(WASMExecEnv *exec_env)
  515. {
  516. return exec_env->current_status->running_status == STATUS_RUNNING
  517. || exec_env->current_status->running_status == STATUS_STEP;
  518. }
  519. void
  520. wasm_cluster_clear_thread_signal(WASMExecEnv *exec_env)
  521. {
  522. exec_env->current_status->signal_flag = 0;
  523. }
  524. void
  525. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo)
  526. {
  527. exec_env->current_status->signal_flag = signo;
  528. }
  529. static void
  530. notify_debug_instance(WASMExecEnv *exec_env)
  531. {
  532. WASMCluster *cluster;
  533. cluster = wasm_exec_env_get_cluster(exec_env);
  534. bh_assert(cluster);
  535. if (!cluster->debug_inst) {
  536. return;
  537. }
  538. on_thread_stop_event(cluster->debug_inst, exec_env);
  539. }
  540. static void
  541. notify_debug_instance_exit(WASMExecEnv *exec_env)
  542. {
  543. WASMCluster *cluster;
  544. cluster = wasm_exec_env_get_cluster(exec_env);
  545. bh_assert(cluster);
  546. if (!cluster->debug_inst) {
  547. return;
  548. }
  549. on_thread_exit_event(cluster->debug_inst, exec_env);
  550. }
  551. void
  552. wasm_cluster_thread_stopped(WASMExecEnv *exec_env)
  553. {
  554. exec_env->current_status->running_status = STATUS_STOP;
  555. notify_debug_instance(exec_env);
  556. }
  557. void
  558. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env)
  559. {
  560. os_mutex_lock(&exec_env->wait_lock);
  561. while (!wasm_cluster_thread_is_running(exec_env)) {
  562. os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
  563. }
  564. os_mutex_unlock(&exec_env->wait_lock);
  565. }
  566. void
  567. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo)
  568. {
  569. WASMExecEnv *exec_env = bh_list_first_elem(&cluster->exec_env_list);
  570. while (exec_env) {
  571. wasm_cluster_thread_send_signal(exec_env, signo);
  572. exec_env = bh_list_elem_next(exec_env);
  573. }
  574. }
  575. void
  576. wasm_cluster_thread_exited(WASMExecEnv *exec_env)
  577. {
  578. exec_env->current_status->running_status = STATUS_EXIT;
  579. notify_debug_instance_exit(exec_env);
  580. }
  581. void
  582. wasm_cluster_thread_continue(WASMExecEnv *exec_env)
  583. {
  584. wasm_cluster_clear_thread_signal(exec_env);
  585. exec_env->current_status->running_status = STATUS_RUNNING;
  586. os_cond_signal(&exec_env->wait_cond);
  587. }
  588. void
  589. wasm_cluster_thread_step(WASMExecEnv *exec_env)
  590. {
  591. exec_env->current_status->running_status = STATUS_STEP;
  592. os_cond_signal(&exec_env->wait_cond);
  593. }
  594. void
  595. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst)
  596. {
  597. cluster->debug_inst = inst;
  598. }
  599. #endif /* end of WASM_ENABLE_DEBUG_INTERP */
  600. /* Check whether the exec_env is in one of all clusters, the caller
  601. should add lock to the cluster list before calling us */
  602. static bool
  603. clusters_have_exec_env(WASMExecEnv *exec_env)
  604. {
  605. WASMCluster *cluster = bh_list_first_elem(cluster_list);
  606. WASMExecEnv *node;
  607. while (cluster) {
  608. node = bh_list_first_elem(&cluster->exec_env_list);
  609. while (node) {
  610. if (node == exec_env) {
  611. bh_assert(exec_env->cluster == cluster);
  612. return true;
  613. }
  614. node = bh_list_elem_next(node);
  615. }
  616. cluster = bh_list_elem_next(cluster);
  617. }
  618. return false;
  619. }
  620. int32
  621. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
  622. {
  623. korp_tid handle;
  624. os_mutex_lock(&cluster_list_lock);
  625. if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
  626. /* Invalid thread, thread has exited or thread has been detached */
  627. if (ret_val)
  628. *ret_val = NULL;
  629. os_mutex_unlock(&cluster_list_lock);
  630. return 0;
  631. }
  632. exec_env->wait_count++;
  633. handle = exec_env->handle;
  634. os_mutex_unlock(&cluster_list_lock);
  635. return os_thread_join(handle, ret_val);
  636. }
  637. int32
  638. wasm_cluster_detach_thread(WASMExecEnv *exec_env)
  639. {
  640. int32 ret = 0;
  641. os_mutex_lock(&cluster_list_lock);
  642. if (!clusters_have_exec_env(exec_env)) {
  643. /* Invalid thread or the thread has exited */
  644. os_mutex_unlock(&cluster_list_lock);
  645. return 0;
  646. }
  647. if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
  648. /* Only detach current thread when there is no other thread
  649. joining it, otherwise let the system resources for the
  650. thread be released after joining */
  651. ret = os_thread_detach(exec_env->handle);
  652. exec_env->thread_is_detached = true;
  653. }
  654. os_mutex_unlock(&cluster_list_lock);
  655. return ret;
  656. }
  657. void
  658. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
  659. {
  660. WASMCluster *cluster;
  661. #ifdef OS_ENABLE_HW_BOUND_CHECK
  662. if (exec_env->jmpbuf_stack_top) {
  663. /* Store the return value in exec_env */
  664. exec_env->thread_ret_value = retval;
  665. exec_env->suspend_flags.flags |= 0x08;
  666. #ifndef BH_PLATFORM_WINDOWS
  667. /* Pop all jmpbuf_node except the last one */
  668. while (exec_env->jmpbuf_stack_top->prev) {
  669. wasm_exec_env_pop_jmpbuf(exec_env);
  670. }
  671. os_longjmp(exec_env->jmpbuf_stack_top->jmpbuf, 1);
  672. return;
  673. #endif
  674. }
  675. #endif
  676. cluster = wasm_exec_env_get_cluster(exec_env);
  677. bh_assert(cluster);
  678. #if WASM_ENABLE_DEBUG_INTERP != 0
  679. wasm_cluster_clear_thread_signal(exec_env);
  680. wasm_cluster_thread_exited(exec_env);
  681. #endif
  682. /* App exit the thread, free the resources before exit native thread */
  683. /* Free aux stack space */
  684. free_aux_stack(exec_env, exec_env->aux_stack_bottom.bottom);
  685. /* Detach the native thread here to ensure the resources are freed */
  686. wasm_cluster_detach_thread(exec_env);
  687. /* Remove and destroy exec_env */
  688. wasm_cluster_del_exec_env(cluster, exec_env);
  689. wasm_exec_env_destroy_internal(exec_env);
  690. os_thread_exit(retval);
  691. }
  692. int32
  693. wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
  694. {
  695. os_mutex_lock(&cluster_list_lock);
  696. if (!clusters_have_exec_env(exec_env)) {
  697. /* Invalid thread or the thread has exited */
  698. os_mutex_unlock(&cluster_list_lock);
  699. return 0;
  700. }
  701. os_mutex_unlock(&cluster_list_lock);
  702. /* Set the termination flag */
  703. #if WASM_ENABLE_DEBUG_INTERP != 0
  704. wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
  705. #else
  706. exec_env->suspend_flags.flags |= 0x01;
  707. #endif
  708. return 0;
  709. }
  710. static void
  711. terminate_thread_visitor(void *node, void *user_data)
  712. {
  713. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  714. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  715. if (curr_exec_env == exec_env)
  716. return;
  717. wasm_cluster_cancel_thread(curr_exec_env);
  718. wasm_cluster_join_thread(curr_exec_env, NULL);
  719. }
  720. void
  721. wasm_cluster_terminate_all(WASMCluster *cluster)
  722. {
  723. traverse_list(&cluster->exec_env_list, terminate_thread_visitor, NULL);
  724. }
  725. void
  726. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  727. WASMExecEnv *exec_env)
  728. {
  729. traverse_list(&cluster->exec_env_list, terminate_thread_visitor,
  730. (void *)exec_env);
  731. }
  732. static void
  733. wait_for_thread_visitor(void *node, void *user_data)
  734. {
  735. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  736. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  737. if (curr_exec_env == exec_env)
  738. return;
  739. wasm_cluster_join_thread(curr_exec_env, NULL);
  740. }
  741. void
  742. wams_cluster_wait_for_all(WASMCluster *cluster)
  743. {
  744. traverse_list(&cluster->exec_env_list, wait_for_thread_visitor, NULL);
  745. }
  746. void
  747. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  748. WASMExecEnv *exec_env)
  749. {
  750. traverse_list(&cluster->exec_env_list, wait_for_thread_visitor,
  751. (void *)exec_env);
  752. }
  753. bool
  754. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
  755. {
  756. DestroyCallBackNode *node;
  757. if (!(node = wasm_runtime_malloc(sizeof(DestroyCallBackNode)))) {
  758. LOG_ERROR("thread manager error: failed to allocate memory");
  759. return false;
  760. }
  761. node->destroy_cb = callback;
  762. bh_list_insert(destroy_callback_list, node);
  763. return true;
  764. }
  765. void
  766. wasm_cluster_suspend_thread(WASMExecEnv *exec_env)
  767. {
  768. /* Set the suspend flag */
  769. exec_env->suspend_flags.flags |= 0x02;
  770. }
  771. static void
  772. suspend_thread_visitor(void *node, void *user_data)
  773. {
  774. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  775. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  776. if (curr_exec_env == exec_env)
  777. return;
  778. wasm_cluster_suspend_thread(curr_exec_env);
  779. }
  780. void
  781. wasm_cluster_suspend_all(WASMCluster *cluster)
  782. {
  783. traverse_list(&cluster->exec_env_list, suspend_thread_visitor, NULL);
  784. }
  785. void
  786. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  787. WASMExecEnv *exec_env)
  788. {
  789. traverse_list(&cluster->exec_env_list, suspend_thread_visitor,
  790. (void *)exec_env);
  791. }
  792. void
  793. wasm_cluster_resume_thread(WASMExecEnv *exec_env)
  794. {
  795. exec_env->suspend_flags.flags &= ~0x02;
  796. os_cond_signal(&exec_env->wait_cond);
  797. }
  798. static void
  799. resume_thread_visitor(void *node, void *user_data)
  800. {
  801. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  802. wasm_cluster_resume_thread(curr_exec_env);
  803. }
  804. void
  805. wasm_cluster_resume_all(WASMCluster *cluster)
  806. {
  807. traverse_list(&cluster->exec_env_list, resume_thread_visitor, NULL);
  808. }
  809. static void
  810. set_exception_visitor(void *node, void *user_data)
  811. {
  812. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  813. WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
  814. WASMModuleInstanceCommon *module_inst = get_module_inst(exec_env);
  815. WASMModuleInstanceCommon *curr_module_inst = get_module_inst(curr_exec_env);
  816. const char *exception = wasm_runtime_get_exception(module_inst);
  817. /* skip "Exception: " */
  818. exception += 11;
  819. if (curr_exec_env != exec_env) {
  820. curr_module_inst = get_module_inst(curr_exec_env);
  821. wasm_runtime_set_exception(curr_module_inst, exception);
  822. }
  823. }
  824. void
  825. wasm_cluster_spread_exception(WASMExecEnv *exec_env)
  826. {
  827. WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
  828. bh_assert(cluster);
  829. traverse_list(&cluster->exec_env_list, set_exception_visitor, exec_env);
  830. }
  831. static void
  832. set_custom_data_visitor(void *node, void *user_data)
  833. {
  834. WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
  835. WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
  836. wasm_runtime_set_custom_data_internal(module_inst, user_data);
  837. }
  838. void
  839. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  840. void *custom_data)
  841. {
  842. WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
  843. if (exec_env == NULL) {
  844. /* Maybe threads have not been started yet. */
  845. wasm_runtime_set_custom_data_internal(module_inst, custom_data);
  846. }
  847. else {
  848. WASMCluster *cluster;
  849. cluster = wasm_exec_env_get_cluster(exec_env);
  850. bh_assert(cluster);
  851. traverse_list(&cluster->exec_env_list, set_custom_data_visitor,
  852. custom_data);
  853. }
  854. }