thread_manager.c 33 KB

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