thread_manager.c 30 KB

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