thread_manager.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if WASM_ENABLE_SHARED_HEAP != 0
  13. #include "../common/wasm_memory.h"
  14. #endif
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. typedef struct WASMDebugInstance WASMDebugInstance;
  20. #endif
  21. struct WASMCluster {
  22. struct WASMCluster *next;
  23. korp_mutex lock;
  24. bh_list exec_env_list;
  25. #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
  26. /* The aux stack of a module with shared memory will be
  27. divided into several segments. This array store the
  28. stack top of different segments */
  29. uint64 *stack_tops;
  30. /* Record which segments are occupied */
  31. bool *stack_segment_occupied;
  32. #endif
  33. /* Size of every stack segment */
  34. uint32 stack_size;
  35. /* When has_exception == true, this cluster should refuse any spawn thread
  36. * requests, this flag can be cleared by calling
  37. * wasm_runtime_clear_exception on instances of any threads of this cluster
  38. */
  39. bool has_exception;
  40. /* When processing is true, this cluster should refuse any spawn thread
  41. * requests. This is a short-lived state, must be cleared immediately once
  42. * the processing finished.
  43. * This is used to avoid dead lock when one thread waiting another thread
  44. * with lock, see wasm_cluster_wait_for_all and wasm_cluster_terminate_all
  45. */
  46. bool processing;
  47. #if WASM_ENABLE_DEBUG_INTERP != 0
  48. WASMDebugInstance *debug_inst;
  49. #endif
  50. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  51. /* When an exception occurs in a thread, the stack frames of that thread are
  52. * saved into the cluster
  53. */
  54. Vector exception_frames;
  55. #endif
  56. };
  57. void
  58. wasm_cluster_set_max_thread_num(uint32 num);
  59. bool
  60. thread_manager_init(void);
  61. void
  62. thread_manager_destroy(void);
  63. /* Create cluster */
  64. WASMCluster *
  65. wasm_cluster_create(WASMExecEnv *exec_env);
  66. /* Destroy cluster */
  67. void
  68. wasm_cluster_destroy(WASMCluster *cluster);
  69. /* Get the cluster of the current exec_env */
  70. WASMCluster *
  71. wasm_exec_env_get_cluster(WASMExecEnv *exec_env);
  72. /* Forward registered functions to a new thread */
  73. bool
  74. wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst,
  75. const WASMModuleInstanceCommon *module_inst_src);
  76. int32
  77. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  78. wasm_module_inst_t module_inst,
  79. bool is_aux_stack_allocated, uint64 aux_stack_start,
  80. uint32 aux_stack_size,
  81. void *(*thread_routine)(void *), void *arg);
  82. int32
  83. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val);
  84. int32
  85. wasm_cluster_detach_thread(WASMExecEnv *exec_env);
  86. int32
  87. wasm_cluster_cancel_thread(WASMExecEnv *exec_env);
  88. void
  89. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval);
  90. bool
  91. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *));
  92. void
  93. wasm_cluster_cancel_all_callbacks(void);
  94. void
  95. wasm_cluster_suspend_all(WASMCluster *cluster);
  96. void
  97. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  98. WASMExecEnv *exec_env);
  99. void
  100. wasm_cluster_suspend_thread(WASMExecEnv *exec_env);
  101. void
  102. wasm_cluster_resume_thread(WASMExecEnv *exec_env);
  103. void
  104. wasm_cluster_resume_all(WASMCluster *cluster);
  105. void
  106. wasm_cluster_terminate_all(WASMCluster *cluster);
  107. void
  108. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  109. WASMExecEnv *exec_env);
  110. void
  111. wasm_cluster_wait_for_all(WASMCluster *cluster);
  112. void
  113. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  114. WASMExecEnv *exec_env);
  115. bool
  116. wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
  117. WASMExecEnv *
  118. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst);
  119. void
  120. wasm_cluster_set_exception(WASMExecEnv *exec_env, const char *exception);
  121. WASMExecEnv *
  122. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);
  123. void
  124. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env);
  125. void
  126. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  127. void *custom_data);
  128. void
  129. wasm_cluster_set_context(WASMModuleInstanceCommon *module_inst, void *key,
  130. void *ctx);
  131. bool
  132. wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env);
  133. #if WASM_ENABLE_SHARED_HEAP != 0
  134. bool
  135. wasm_cluster_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
  136. WASMSharedHeap *heap);
  137. void
  138. wasm_cluster_detach_shared_heap(WASMModuleInstanceCommon *module_inst);
  139. #endif
  140. #if WASM_ENABLE_DEBUG_INTERP != 0
  141. #define WAMR_SIG_TRAP (5)
  142. #define WAMR_SIG_STOP (19)
  143. #define WAMR_SIG_TERM (15)
  144. #define WAMR_SIG_SINGSTEP (0x1ff)
  145. #define STATUS_RUNNING (0)
  146. #define STATUS_STOP (1)
  147. #define STATUS_EXIT (2)
  148. #define STATUS_STEP (3)
  149. #define IS_WAMR_TERM_SIG(signo) ((signo) == WAMR_SIG_TERM)
  150. #define IS_WAMR_STOP_SIG(signo) \
  151. ((signo) == WAMR_SIG_STOP || (signo) == WAMR_SIG_TRAP)
  152. struct WASMCurrentEnvStatus {
  153. uint32 signal_flag;
  154. uint16 step_count;
  155. uint16 running_status;
  156. };
  157. WASMCurrentEnvStatus *
  158. wasm_cluster_create_exenv_status(void);
  159. void
  160. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status);
  161. void
  162. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo);
  163. /* This function must be called with exec_env->wait_lock locked, otherwise we
  164. * may miss the signal from debugger thread, see
  165. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1860 */
  166. void
  167. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env);
  168. void
  169. wasm_cluster_wait_thread_status(WASMExecEnv *exec_env, uint32 *status);
  170. void
  171. wasm_cluster_thread_exited(WASMExecEnv *exec_env);
  172. void
  173. wasm_cluster_thread_continue(WASMExecEnv *exec_env);
  174. void
  175. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo);
  176. void
  177. wasm_cluster_thread_step(WASMExecEnv *exec_env);
  178. void
  179. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst);
  180. #endif /* end of WASM_ENABLE_DEBUG_INTERP != 0 */
  181. void
  182. wasm_cluster_traverse_lock(WASMExecEnv *exec_env);
  183. void
  184. wasm_cluster_traverse_unlock(WASMExecEnv *exec_env);
  185. bool
  186. wasm_cluster_allocate_aux_stack(WASMExecEnv *exec_env, uint64 *p_start,
  187. uint32 *p_size);
  188. bool
  189. wasm_cluster_free_aux_stack(WASMExecEnv *exec_env, uint64 start);
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. #endif /* end of _THREAD_MANAGER_H */