thread_manager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #if WASM_ENABLE_DEBUG_INTERP != 0
  33. WASMDebugInstance *debug_inst;
  34. #endif
  35. };
  36. void
  37. wasm_cluster_set_max_thread_num(uint32 num);
  38. bool
  39. thread_manager_init();
  40. void
  41. thread_manager_destroy();
  42. /* Create cluster */
  43. WASMCluster *
  44. wasm_cluster_create(WASMExecEnv *exec_env);
  45. /* Destroy cluster */
  46. void
  47. wasm_cluster_destroy(WASMCluster *cluster);
  48. /* Get the cluster of the current exec_env */
  49. WASMCluster *
  50. wasm_exec_env_get_cluster(WASMExecEnv *exec_env);
  51. int32
  52. wasm_cluster_create_thread(WASMExecEnv *exec_env,
  53. wasm_module_inst_t module_inst,
  54. void *(*thread_routine)(void *), void *arg);
  55. int32
  56. wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val);
  57. int32
  58. wasm_cluster_detach_thread(WASMExecEnv *exec_env);
  59. int32
  60. wasm_cluster_cancel_thread(WASMExecEnv *exec_env);
  61. void
  62. wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval);
  63. bool
  64. wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *));
  65. void
  66. wasm_cluster_cancel_all_callbacks();
  67. void
  68. wasm_cluster_suspend_all(WASMCluster *cluster);
  69. void
  70. wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
  71. WASMExecEnv *exec_env);
  72. void
  73. wasm_cluster_suspend_thread(WASMExecEnv *exec_env);
  74. void
  75. wasm_cluster_resume_thread(WASMExecEnv *exec_env);
  76. void
  77. wasm_cluster_resume_all(WASMCluster *cluster);
  78. void
  79. wasm_cluster_terminate_all(WASMCluster *cluster);
  80. void
  81. wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
  82. WASMExecEnv *exec_env);
  83. void
  84. wams_cluster_wait_for_all(WASMCluster *cluster);
  85. void
  86. wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
  87. WASMExecEnv *exec_env);
  88. bool
  89. wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
  90. bool
  91. wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env);
  92. WASMExecEnv *
  93. wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst);
  94. void
  95. wasm_cluster_spread_exception(WASMExecEnv *exec_env);
  96. WASMExecEnv *
  97. wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env);
  98. void
  99. wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env);
  100. void
  101. wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
  102. void *custom_data);
  103. #if WASM_ENABLE_DEBUG_INTERP != 0
  104. #define WAMR_SIG_TRAP (5)
  105. #define WAMR_SIG_STOP (19)
  106. #define WAMR_SIG_TERM (15)
  107. #define WAMR_SIG_SINGSTEP (0x1ff)
  108. #define STATUS_RUNNING (0)
  109. #define STATUS_STOP (1)
  110. #define STATUS_EXIT (2)
  111. #define STATUS_STEP (3)
  112. #define IS_WAMR_TERM_SIG(signo) ((signo) == WAMR_SIG_TERM)
  113. #define IS_WAMR_STOP_SIG(signo) \
  114. ((signo) == WAMR_SIG_STOP || (signo) == WAMR_SIG_TRAP)
  115. struct WASMCurrentEnvStatus {
  116. uint64 signal_flag : 32;
  117. uint64 step_count : 16;
  118. uint64 running_status : 16;
  119. };
  120. WASMCurrentEnvStatus *
  121. wasm_cluster_create_exenv_status();
  122. void
  123. wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status);
  124. void
  125. wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo);
  126. void
  127. wasm_cluster_thread_stopped(WASMExecEnv *exec_env);
  128. void
  129. wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env);
  130. void
  131. wasm_cluster_wait_thread_status(WASMExecEnv *exec_env, uint32 *status);
  132. void
  133. wasm_cluster_thread_exited(WASMExecEnv *exec_env);
  134. void
  135. wasm_cluster_thread_continue(WASMExecEnv *exec_env);
  136. void
  137. wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo);
  138. void
  139. wasm_cluster_thread_step(WASMExecEnv *exec_env);
  140. void
  141. wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst);
  142. #endif /* end of WASM_ENABLE_DEBUG_INTERP != 0 */
  143. #ifdef __cplusplus
  144. }
  145. #endif
  146. #endif /* end of _THREAD_MANAGER_H */