thread_manager.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _THREAD_MANAGER_H
  6. #define _THREAD_MANAGER_H
  7. #include "bh_common.h"
  8. #include "bh_log.h"
  9. #include "wasm_export.h"
  10. #include "../interpreter/wasm.h"
  11. #include "../common/wasm_runtime_common.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #if WASM_ENABLE_DEBUG_INTERP != 0
  16. typedef struct WASMDebugInstance WASMDebugInstance;
  17. #endif
  18. struct WASMCluster {
  19. struct WASMCluster *next;
  20. korp_mutex lock;
  21. bh_list exec_env_list;
  22. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  23. /* The aux stack of a module with shared memory will be
  24. divided into several segments. This array store the
  25. stack top of different segments */
  26. uint32 *stack_tops;
  27. /* Record which segments are occupied */
  28. bool *stack_segment_occupied;
  29. #endif
  30. /* Size of every stack segment */
  31. uint32 stack_size;
  32. /* When has_exception == true, this cluster should refuse any spawn thread
  33. * requests, this flag can be cleared by calling
  34. * wasm_runtime_clear_exception on instances of any threads of this cluster
  35. */
  36. bool has_exception;
  37. /* When processing is true, this cluster should refuse any spawn thread
  38. * requests. This is a short-lived state, must be cleared immediately once
  39. * the processing finished.
  40. * This is used to avoid dead lock when one thread waiting another thread
  41. * with lock, see wams_cluster_wait_for_all and wasm_cluster_terminate_all
  42. */
  43. bool processing;
  44. #if WASM_ENABLE_DEBUG_INTERP != 0
  45. WASMDebugInstance *debug_inst;
  46. #endif
  47. };
  48. void
  49. wasm_cluster_set_max_thread_num(uint32 num);
  50. bool
  51. thread_manager_init();
  52. void
  53. thread_manager_destroy();
  54. /* Create cluster */
  55. WASMCluster *
  56. wasm_cluster_create(WASMExecEnv *exec_env);
  57. /* Destroy cluster */
  58. void
  59. wasm_cluster_destroy(WASMCluster *cluster);
  60. /* Get the cluster of the current exec_env */
  61. WASMCluster *
  62. wasm_exec_env_get_cluster(WASMExecEnv *exec_env);
  63. /* Forward registered functions to a new thread */
  64. bool
  65. wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst,
  66. const WASMModuleInstanceCommon *module_inst_src);
  67. int32
  68. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  69. wasm_module_inst_t module_inst, bool alloc_aux_stack,
  70. void *(*thread_routine)(void *), void *arg);
  71. int32
  72. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val);
  73. int32
  74. wasm_cluster_detach_thread(WASMExecEnv *exec_env);
  75. int32
  76. wasm_cluster_cancel_thread(WASMExecEnv *exec_env);
  77. void
  78. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval);
  79. bool
  80. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *));
  81. void
  82. wasm_cluster_cancel_all_callbacks();
  83. void
  84. wasm_cluster_suspend_all(WASMCluster *cluster);
  85. void
  86. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  87. WASMExecEnv *exec_env);
  88. void
  89. wasm_cluster_suspend_thread(WASMExecEnv *exec_env);
  90. void
  91. wasm_cluster_resume_thread(WASMExecEnv *exec_env);
  92. void
  93. wasm_cluster_resume_all(WASMCluster *cluster);
  94. void
  95. wasm_cluster_terminate_all(WASMCluster *cluster);
  96. void
  97. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  98. WASMExecEnv *exec_env);
  99. void
  100. wams_cluster_wait_for_all(WASMCluster *cluster);
  101. void
  102. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  103. WASMExecEnv *exec_env);
  104. bool
  105. wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
  106. WASMExecEnv *
  107. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst);
  108. void
  109. wasm_cluster_spread_exception(WASMExecEnv *exec_env, bool clear);
  110. WASMExecEnv *
  111. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);
  112. void
  113. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env);
  114. void
  115. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  116. void *custom_data);
  117. bool
  118. wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env);
  119. #if WASM_ENABLE_DEBUG_INTERP != 0
  120. #define WAMR_SIG_TRAP (5)
  121. #define WAMR_SIG_STOP (19)
  122. #define WAMR_SIG_TERM (15)
  123. #define WAMR_SIG_SINGSTEP (0x1ff)
  124. #define STATUS_RUNNING (0)
  125. #define STATUS_STOP (1)
  126. #define STATUS_EXIT (2)
  127. #define STATUS_STEP (3)
  128. #define IS_WAMR_TERM_SIG(signo) ((signo) == WAMR_SIG_TERM)
  129. #define IS_WAMR_STOP_SIG(signo) \
  130. ((signo) == WAMR_SIG_STOP || (signo) == WAMR_SIG_TRAP)
  131. struct WASMCurrentEnvStatus {
  132. uint64 signal_flag : 32;
  133. uint64 step_count : 16;
  134. uint64 running_status : 16;
  135. };
  136. WASMCurrentEnvStatus *
  137. wasm_cluster_create_exenv_status();
  138. void
  139. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status);
  140. void
  141. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo);
  142. /* This function must be called with exec_env->wait_lock locked, otherwise we
  143. * may miss the signal from debugger thread, see
  144. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1860 */
  145. void
  146. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env);
  147. void
  148. wasm_cluster_wait_thread_status(WASMExecEnv *exec_env, uint32 *status);
  149. void
  150. wasm_cluster_thread_exited(WASMExecEnv *exec_env);
  151. void
  152. wasm_cluster_thread_continue(WASMExecEnv *exec_env);
  153. void
  154. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo);
  155. void
  156. wasm_cluster_thread_step(WASMExecEnv *exec_env);
  157. void
  158. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst);
  159. #endif /* end of WASM_ENABLE_DEBUG_INTERP != 0 */
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* end of _THREAD_MANAGER_H */