krillplayer-plugin 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/LICENSE +2 -1
  2. package/README.md +2 -0
  3. package/dist/cli.js +397 -80
  4. package/dist/crypto.js +38 -11
  5. package/dist/engine/engine.c +6 -3
  6. package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
  7. package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
  8. package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
  9. package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
  10. package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
  11. package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
  12. package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
  13. package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
  14. package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
  15. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
  16. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
  17. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
  18. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
  19. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
  20. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
  21. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
  22. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
  23. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
  24. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
  25. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
  26. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
  27. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
  28. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
  29. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
  30. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
  31. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
  32. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
  33. package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
  34. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
  35. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
  36. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
  37. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
  38. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
  39. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
  40. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
  41. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
  42. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
  43. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
  44. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
  45. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
  46. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
  47. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
  48. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
  49. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
  50. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
  51. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
  52. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
  53. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
  54. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
  55. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
  56. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
  57. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
  58. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
  59. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
  60. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
  61. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
  62. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
  63. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
  64. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
  65. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
  66. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
  67. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
  68. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
  69. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
  70. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
  71. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
  72. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
  73. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
  74. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
  75. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
  76. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
  77. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
  78. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
  79. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
  80. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
  81. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
  82. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
  83. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
  84. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
  85. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
  86. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
  87. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
  88. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
  89. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
  90. package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
  91. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
  92. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
  93. package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
  94. package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
  95. package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
  96. package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
  97. package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
  98. package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
  99. package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
  100. package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
  101. package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
  102. package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
  103. package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
  104. package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
  105. package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
  106. package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
  107. package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
  108. package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
  109. package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
  110. package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
  111. package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
  112. package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
  113. package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
  114. package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
  115. package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
  116. package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
  117. package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
  118. package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
  119. package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
  120. package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
  121. package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
  122. package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
  123. package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
  124. package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
  125. package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
  126. package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
  127. package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
  128. package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
  129. package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
  130. package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
  131. package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
  132. package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
  133. package/kernel/Tests/KernelTests/CasePins.js +50 -0
  134. package/package.json +2 -2
  135. package/skill/krillplayer-plugin/SKILL.md +2 -0
  136. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
  137. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
  138. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
  139. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
  140. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
  141. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
  142. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
  143. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +46 -0
  144. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +46 -0
  145. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +21 -1
  146. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +39 -4
  147. package/skill/krillplayer-plugin/references/cases.md +20 -2
  148. package/skill/krillplayer-plugin/references/contract.d.ts +114 -12
  149. package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
  150. package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
@@ -0,0 +1,1569 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "thread_manager.h"
7
+ #include "../common/wasm_c_api_internal.h"
8
+
9
+ #if WASM_ENABLE_INTERP != 0
10
+ #include "../interpreter/wasm_runtime.h"
11
+ #endif
12
+ #if WASM_ENABLE_AOT != 0
13
+ #include "../aot/aot_runtime.h"
14
+ #endif
15
+
16
+ #if WASM_ENABLE_DEBUG_INTERP != 0
17
+ #include "debug_engine.h"
18
+ #endif
19
+
20
+ typedef struct {
21
+ bh_list_link l;
22
+ void (*destroy_cb)(WASMCluster *);
23
+ } DestroyCallBackNode;
24
+
25
+ static bh_list destroy_callback_list_head;
26
+ static bh_list *const destroy_callback_list = &destroy_callback_list_head;
27
+
28
+ static bh_list cluster_list_head;
29
+ static bh_list *const cluster_list = &cluster_list_head;
30
+ static korp_mutex cluster_list_lock;
31
+
32
+ static korp_mutex _exception_lock;
33
+
34
+ typedef void (*list_visitor)(void *, void *);
35
+
36
+ static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM;
37
+
38
+ /* Set the maximum thread number, if this function is not called,
39
+ the max thread num is defined by CLUSTER_MAX_THREAD_NUM */
40
+ void
41
+ wasm_cluster_set_max_thread_num(uint32 num)
42
+ {
43
+ if (num > 0)
44
+ cluster_max_thread_num = num;
45
+ }
46
+
47
+ bool
48
+ thread_manager_init()
49
+ {
50
+ if (bh_list_init(cluster_list) != 0)
51
+ return false;
52
+ if (os_mutex_init(&cluster_list_lock) != 0)
53
+ return false;
54
+ if (os_mutex_init(&_exception_lock) != 0) {
55
+ os_mutex_destroy(&cluster_list_lock);
56
+ return false;
57
+ }
58
+ return true;
59
+ }
60
+
61
+ void
62
+ thread_manager_destroy()
63
+ {
64
+ WASMCluster *cluster = bh_list_first_elem(cluster_list);
65
+ WASMCluster *next;
66
+ while (cluster) {
67
+ next = bh_list_elem_next(cluster);
68
+ wasm_cluster_destroy(cluster);
69
+ cluster = next;
70
+ }
71
+ wasm_cluster_cancel_all_callbacks();
72
+ os_mutex_destroy(&_exception_lock);
73
+ os_mutex_destroy(&cluster_list_lock);
74
+ }
75
+
76
+ static void
77
+ traverse_list(bh_list *l, list_visitor visitor, void *user_data)
78
+ {
79
+ void *next, *node = bh_list_first_elem(l);
80
+ while (node) {
81
+ next = bh_list_elem_next(node);
82
+ visitor(node, user_data);
83
+ node = next;
84
+ }
85
+ }
86
+
87
+ /* Assumes cluster->lock is locked */
88
+ static bool
89
+ safe_traverse_exec_env_list(WASMCluster *cluster, list_visitor visitor,
90
+ void *user_data)
91
+ {
92
+ Vector proc_nodes;
93
+ void *node;
94
+ bool ret = true;
95
+
96
+ if (!bh_vector_init(&proc_nodes, cluster->exec_env_list.len, sizeof(void *),
97
+ false)) {
98
+ ret = false;
99
+ goto final;
100
+ }
101
+
102
+ node = bh_list_first_elem(&cluster->exec_env_list);
103
+
104
+ while (node) {
105
+ bool already_processed = false;
106
+ void *proc_node;
107
+ uint32 i;
108
+ for (i = 0; i < (uint32)bh_vector_size(&proc_nodes); i++) {
109
+ if (!bh_vector_get(&proc_nodes, i, &proc_node)) {
110
+ ret = false;
111
+ goto final;
112
+ }
113
+ if (proc_node == node) {
114
+ already_processed = true;
115
+ break;
116
+ }
117
+ }
118
+ if (already_processed) {
119
+ node = bh_list_elem_next(node);
120
+ continue;
121
+ }
122
+
123
+ os_mutex_unlock(&cluster->lock);
124
+ visitor(node, user_data);
125
+ os_mutex_lock(&cluster->lock);
126
+ if (!bh_vector_append(&proc_nodes, &node)) {
127
+ ret = false;
128
+ goto final;
129
+ }
130
+
131
+ node = bh_list_first_elem(&cluster->exec_env_list);
132
+ }
133
+
134
+ final:
135
+ bh_vector_destroy(&proc_nodes);
136
+
137
+ return ret;
138
+ }
139
+
140
+ /* The caller must not have any locks */
141
+ bool
142
+ wasm_cluster_allocate_aux_stack(WASMExecEnv *exec_env, uint64 *p_start,
143
+ uint32 *p_size)
144
+ {
145
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
146
+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
147
+ WASMModuleInstanceCommon *module_inst =
148
+ wasm_exec_env_get_module_inst(exec_env);
149
+ uint64 stack_end;
150
+
151
+ stack_end = wasm_runtime_module_malloc_internal(module_inst, exec_env,
152
+ cluster->stack_size, NULL);
153
+ *p_start = stack_end + cluster->stack_size;
154
+ *p_size = cluster->stack_size;
155
+
156
+ return stack_end != 0;
157
+ #else
158
+ uint32 i;
159
+
160
+ /* If the module doesn't have aux stack info,
161
+ it can't create any threads */
162
+
163
+ os_mutex_lock(&cluster->lock);
164
+ if (!cluster->stack_segment_occupied) {
165
+ os_mutex_unlock(&cluster->lock);
166
+ return false;
167
+ }
168
+
169
+ for (i = 0; i < cluster_max_thread_num; i++) {
170
+ if (!cluster->stack_segment_occupied[i]) {
171
+ if (p_start)
172
+ *p_start = cluster->stack_tops[i];
173
+ if (p_size)
174
+ *p_size = cluster->stack_size;
175
+ cluster->stack_segment_occupied[i] = true;
176
+ os_mutex_unlock(&cluster->lock);
177
+ return true;
178
+ }
179
+ }
180
+ os_mutex_unlock(&cluster->lock);
181
+
182
+ return false;
183
+ #endif
184
+ }
185
+
186
+ /* The caller must not have any locks */
187
+ bool
188
+ wasm_cluster_free_aux_stack(WASMExecEnv *exec_env, uint64 start)
189
+ {
190
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
191
+
192
+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
193
+ WASMModuleInstanceCommon *module_inst =
194
+ wasm_exec_env_get_module_inst(exec_env);
195
+
196
+ if (!wasm_exec_env_is_aux_stack_managed_by_runtime(exec_env)) {
197
+ return true;
198
+ }
199
+
200
+ bh_assert(start >= cluster->stack_size);
201
+
202
+ wasm_runtime_module_free_internal(module_inst, exec_env,
203
+ start - cluster->stack_size);
204
+
205
+ return true;
206
+ #else
207
+ uint32 i;
208
+
209
+ os_mutex_lock(&cluster->lock);
210
+ for (i = 0; i < cluster_max_thread_num; i++) {
211
+ if (start == cluster->stack_tops[i]) {
212
+ cluster->stack_segment_occupied[i] = false;
213
+ os_mutex_unlock(&cluster->lock);
214
+ return true;
215
+ }
216
+ }
217
+ os_mutex_unlock(&cluster->lock);
218
+ return false;
219
+ #endif
220
+ }
221
+
222
+ WASMCluster *
223
+ wasm_cluster_create(WASMExecEnv *exec_env)
224
+ {
225
+ WASMCluster *cluster;
226
+ uint32 aux_stack_size;
227
+ uint64 aux_stack_start;
228
+
229
+ bh_assert(exec_env->cluster == NULL);
230
+ if (!(cluster = wasm_runtime_malloc(sizeof(WASMCluster)))) {
231
+ LOG_ERROR("thread manager error: failed to allocate memory");
232
+ return NULL;
233
+ }
234
+ memset(cluster, 0, sizeof(WASMCluster));
235
+
236
+ exec_env->cluster = cluster;
237
+
238
+ bh_list_init(&cluster->exec_env_list);
239
+ bh_list_insert(&cluster->exec_env_list, exec_env);
240
+ if (os_mutex_init(&cluster->lock) != 0) {
241
+ wasm_runtime_free(cluster);
242
+ LOG_ERROR("thread manager error: failed to init mutex");
243
+ return NULL;
244
+ }
245
+
246
+ /* Prepare the aux stack top and size for every thread */
247
+ if (!wasm_exec_env_get_aux_stack(exec_env, &aux_stack_start,
248
+ &aux_stack_size)) {
249
+ #if WASM_ENABLE_LIB_WASI_THREADS == 0
250
+ LOG_VERBOSE("No aux stack info for this module, can't create thread");
251
+ #endif
252
+
253
+ /* If the module don't have aux stack info, don't throw error here,
254
+ but remain stack_tops and stack_segment_occupied as NULL */
255
+ os_mutex_lock(&cluster_list_lock);
256
+ if (bh_list_insert(cluster_list, cluster) != 0) {
257
+ os_mutex_unlock(&cluster_list_lock);
258
+ goto fail;
259
+ }
260
+ os_mutex_unlock(&cluster_list_lock);
261
+
262
+ return cluster;
263
+ }
264
+
265
+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION != 0
266
+ cluster->stack_size = aux_stack_size;
267
+ #else
268
+ cluster->stack_size = aux_stack_size / (cluster_max_thread_num + 1);
269
+ if (cluster->stack_size < WASM_THREAD_AUX_STACK_SIZE_MIN) {
270
+ goto fail;
271
+ }
272
+ /* Make stack size 16-byte aligned */
273
+ cluster->stack_size = cluster->stack_size & (~15);
274
+ #endif
275
+
276
+ /* Set initial aux stack top to the instance and
277
+ aux stack boundary to the main exec_env */
278
+ if (!wasm_exec_env_set_aux_stack(exec_env, aux_stack_start,
279
+ cluster->stack_size))
280
+ goto fail;
281
+
282
+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
283
+ if (cluster_max_thread_num != 0) {
284
+ uint64 total_size = cluster_max_thread_num * sizeof(uint64);
285
+ uint32 i;
286
+ if (total_size >= UINT32_MAX
287
+ || !(cluster->stack_tops =
288
+ wasm_runtime_malloc((uint32)total_size))) {
289
+ goto fail;
290
+ }
291
+ memset(cluster->stack_tops, 0, (uint32)total_size);
292
+
293
+ if (!(cluster->stack_segment_occupied =
294
+ wasm_runtime_malloc(cluster_max_thread_num * sizeof(bool)))) {
295
+ goto fail;
296
+ }
297
+ memset(cluster->stack_segment_occupied, 0,
298
+ cluster_max_thread_num * sizeof(bool));
299
+
300
+ /* Reserve space for main instance */
301
+ aux_stack_start -= cluster->stack_size;
302
+
303
+ for (i = 0; i < cluster_max_thread_num; i++) {
304
+ cluster->stack_tops[i] =
305
+ aux_stack_start - (uint64)cluster->stack_size * i;
306
+ }
307
+ }
308
+ #endif
309
+
310
+ os_mutex_lock(&cluster_list_lock);
311
+ if (bh_list_insert(cluster_list, cluster) != 0) {
312
+ os_mutex_unlock(&cluster_list_lock);
313
+ goto fail;
314
+ }
315
+ os_mutex_unlock(&cluster_list_lock);
316
+
317
+ return cluster;
318
+
319
+ fail:
320
+ if (cluster)
321
+ wasm_cluster_destroy(cluster);
322
+
323
+ return NULL;
324
+ }
325
+
326
+ static void
327
+ destroy_cluster_visitor(void *node, void *user_data)
328
+ {
329
+ DestroyCallBackNode *destroy_node = (DestroyCallBackNode *)node;
330
+ WASMCluster *cluster = (WASMCluster *)user_data;
331
+
332
+ destroy_node->destroy_cb(cluster);
333
+ }
334
+
335
+ void
336
+ wasm_cluster_destroy(WASMCluster *cluster)
337
+ {
338
+ traverse_list(destroy_callback_list, destroy_cluster_visitor,
339
+ (void *)cluster);
340
+
341
+ /* Remove the cluster from the cluster list */
342
+ os_mutex_lock(&cluster_list_lock);
343
+ bh_list_remove(cluster_list, cluster);
344
+ os_mutex_unlock(&cluster_list_lock);
345
+
346
+ os_mutex_destroy(&cluster->lock);
347
+
348
+ #if WASM_ENABLE_HEAP_AUX_STACK_ALLOCATION == 0
349
+ if (cluster->stack_tops)
350
+ wasm_runtime_free(cluster->stack_tops);
351
+ if (cluster->stack_segment_occupied)
352
+ wasm_runtime_free(cluster->stack_segment_occupied);
353
+ #endif
354
+
355
+ #if WASM_ENABLE_DEBUG_INTERP != 0
356
+ wasm_debug_instance_destroy(cluster);
357
+ #endif
358
+
359
+ #if WASM_ENABLE_DUMP_CALL_STACK != 0
360
+ bh_vector_destroy(&cluster->exception_frames);
361
+ #endif
362
+
363
+ wasm_runtime_free(cluster);
364
+ }
365
+
366
+ static void
367
+ free_node_visitor(void *node, void *user_data)
368
+ {
369
+ wasm_runtime_free(node);
370
+ }
371
+
372
+ void
373
+ wasm_cluster_cancel_all_callbacks()
374
+ {
375
+ traverse_list(destroy_callback_list, free_node_visitor, NULL);
376
+ bh_list_init(destroy_callback_list);
377
+ }
378
+
379
+ WASMCluster *
380
+ wasm_exec_env_get_cluster(WASMExecEnv *exec_env)
381
+ {
382
+ return exec_env->cluster;
383
+ }
384
+
385
+ /* The caller must lock cluster->lock */
386
+ static bool
387
+ wasm_cluster_add_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
388
+ {
389
+ bool ret = true;
390
+
391
+ exec_env->cluster = cluster;
392
+
393
+ if (cluster->exec_env_list.len == cluster_max_thread_num + 1) {
394
+ LOG_ERROR("thread manager error: "
395
+ "maximum number of threads exceeded");
396
+ ret = false;
397
+ }
398
+
399
+ if (ret && bh_list_insert(&cluster->exec_env_list, exec_env) != 0)
400
+ ret = false;
401
+
402
+ return ret;
403
+ }
404
+
405
+ static bool
406
+ wasm_cluster_del_exec_env_internal(WASMCluster *cluster, WASMExecEnv *exec_env,
407
+ bool can_destroy_cluster)
408
+ {
409
+ bool ret = true;
410
+ bh_assert(exec_env->cluster == cluster);
411
+
412
+ #if WASM_ENABLE_DEBUG_INTERP != 0
413
+ /* Wait for debugger control thread to process the
414
+ stop event of this thread */
415
+ if (cluster->debug_inst) {
416
+ /* lock the debug_inst->wait_lock so
417
+ other threads can't fire stop events */
418
+ os_mutex_lock(&cluster->debug_inst->wait_lock);
419
+ while (cluster->debug_inst->stopped_thread == exec_env) {
420
+ /* either wakes up by signal or by 1-second timeout */
421
+ os_cond_reltimedwait(&cluster->debug_inst->wait_cond,
422
+ &cluster->debug_inst->wait_lock, 1000000);
423
+ }
424
+ os_mutex_unlock(&cluster->debug_inst->wait_lock);
425
+ }
426
+ #endif
427
+ if (bh_list_remove(&cluster->exec_env_list, exec_env) != 0)
428
+ ret = false;
429
+
430
+ if (can_destroy_cluster) {
431
+ if (cluster->exec_env_list.len == 0) {
432
+ /* exec_env_list empty, destroy the cluster */
433
+ wasm_cluster_destroy(cluster);
434
+ }
435
+ }
436
+ else {
437
+ /* Don't destroy cluster as cluster->lock is being used */
438
+ }
439
+
440
+ return ret;
441
+ }
442
+
443
+ /* The caller should lock cluster->lock for thread safety */
444
+ bool
445
+ wasm_cluster_del_exec_env(WASMCluster *cluster, WASMExecEnv *exec_env)
446
+ {
447
+ return wasm_cluster_del_exec_env_internal(cluster, exec_env, true);
448
+ }
449
+
450
+ static WASMExecEnv *
451
+ wasm_cluster_search_exec_env(WASMCluster *cluster,
452
+ WASMModuleInstanceCommon *module_inst)
453
+ {
454
+ WASMExecEnv *node = NULL;
455
+
456
+ os_mutex_lock(&cluster->lock);
457
+ node = bh_list_first_elem(&cluster->exec_env_list);
458
+ while (node) {
459
+ if (node->module_inst == module_inst) {
460
+ os_mutex_unlock(&cluster->lock);
461
+ return node;
462
+ }
463
+ node = bh_list_elem_next(node);
464
+ }
465
+
466
+ os_mutex_unlock(&cluster->lock);
467
+ return NULL;
468
+ }
469
+
470
+ /* search the global cluster list to find if the given
471
+ module instance have a corresponding exec_env */
472
+ WASMExecEnv *
473
+ wasm_clusters_search_exec_env(WASMModuleInstanceCommon *module_inst)
474
+ {
475
+ WASMCluster *cluster = NULL;
476
+ WASMExecEnv *exec_env = NULL;
477
+
478
+ os_mutex_lock(&cluster_list_lock);
479
+ cluster = bh_list_first_elem(cluster_list);
480
+ while (cluster) {
481
+ exec_env = wasm_cluster_search_exec_env(cluster, module_inst);
482
+ if (exec_env) {
483
+ os_mutex_unlock(&cluster_list_lock);
484
+ return exec_env;
485
+ }
486
+ cluster = bh_list_elem_next(cluster);
487
+ }
488
+
489
+ os_mutex_unlock(&cluster_list_lock);
490
+ return NULL;
491
+ }
492
+
493
+ WASMExecEnv *
494
+ wasm_cluster_spawn_exec_env(WASMExecEnv *exec_env)
495
+ {
496
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
497
+ wasm_module_inst_t module_inst = get_module_inst(exec_env);
498
+ wasm_module_t module;
499
+ wasm_module_inst_t new_module_inst;
500
+ WASMExecEnv *new_exec_env;
501
+ uint32 aux_stack_size;
502
+ uint64 aux_stack_start;
503
+ uint32 stack_size = 8192;
504
+
505
+ if (!module_inst || !(module = wasm_exec_env_get_module(exec_env))) {
506
+ return NULL;
507
+ }
508
+
509
+ if (!(new_module_inst = wasm_runtime_instantiate_internal(
510
+ module, module_inst, exec_env, stack_size, 0, 0, NULL, 0))) {
511
+ return NULL;
512
+ }
513
+
514
+ /* Set custom_data to new module instance */
515
+ wasm_runtime_set_custom_data_internal(
516
+ new_module_inst, wasm_runtime_get_custom_data(module_inst));
517
+
518
+ wasm_native_inherit_contexts(new_module_inst, module_inst);
519
+
520
+ if (!(wasm_cluster_dup_c_api_imports(new_module_inst, module_inst))) {
521
+ goto fail1;
522
+ }
523
+
524
+ if (!wasm_cluster_allocate_aux_stack(exec_env, &aux_stack_start,
525
+ &aux_stack_size)) {
526
+ LOG_ERROR("thread manager error: "
527
+ "failed to allocate aux stack space for new thread");
528
+ goto fail1;
529
+ }
530
+
531
+ os_mutex_lock(&cluster->lock);
532
+
533
+ if (cluster->has_exception || cluster->processing) {
534
+ goto fail2;
535
+ }
536
+
537
+ #if WASM_ENABLE_INTERP != 0
538
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
539
+ stack_size =
540
+ ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;
541
+ }
542
+ #endif
543
+
544
+ #if WASM_ENABLE_AOT != 0
545
+ if (module_inst->module_type == Wasm_Module_AoT) {
546
+ stack_size =
547
+ ((AOTModuleInstance *)module_inst)->default_wasm_stack_size;
548
+ }
549
+ #endif
550
+
551
+ new_exec_env = wasm_exec_env_create_internal(new_module_inst,
552
+ exec_env->wasm_stack_size);
553
+ if (!new_exec_env) {
554
+ goto fail2;
555
+ }
556
+
557
+ /* Set aux stack for current thread */
558
+ if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
559
+ aux_stack_size)) {
560
+ goto fail3;
561
+ }
562
+ new_exec_env->is_aux_stack_allocated = true;
563
+
564
+ /* Inherit suspend_flags of parent thread */
565
+ new_exec_env->suspend_flags.flags =
566
+ (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK);
567
+
568
+ if (!wasm_cluster_add_exec_env(cluster, new_exec_env)) {
569
+ goto fail3;
570
+ }
571
+
572
+ os_mutex_unlock(&cluster->lock);
573
+
574
+ return new_exec_env;
575
+
576
+ fail3:
577
+ wasm_exec_env_destroy_internal(new_exec_env);
578
+ fail2:
579
+ os_mutex_unlock(&cluster->lock);
580
+ /* free the allocated aux stack space */
581
+ wasm_cluster_free_aux_stack(exec_env, aux_stack_start);
582
+ fail1:
583
+ wasm_runtime_deinstantiate_internal(new_module_inst, true);
584
+
585
+ return NULL;
586
+ }
587
+
588
+ void
589
+ wasm_cluster_destroy_spawned_exec_env(WASMExecEnv *exec_env)
590
+ {
591
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
592
+ wasm_module_inst_t module_inst = wasm_runtime_get_module_inst(exec_env);
593
+ bh_assert(cluster != NULL);
594
+ WASMExecEnv *exec_env_tls = NULL;
595
+
596
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
597
+ /* Note: free_aux_stack can execute the module's "free" function
598
+ * using the specified exec_env. In case of OS_ENABLE_HW_BOUND_CHECK,
599
+ * it needs to match the TLS exec_env if available. (Consider a native
600
+ * function which calls wasm_cluster_destroy_spawned_exec_env.)
601
+ */
602
+ exec_env_tls = wasm_runtime_get_exec_env_tls();
603
+ #endif
604
+ if (exec_env_tls == NULL) {
605
+ exec_env_tls = exec_env;
606
+ }
607
+
608
+ /* Free aux stack space which was allocated in
609
+ wasm_cluster_spawn_exec_env */
610
+ bh_assert(exec_env_tls->is_aux_stack_allocated);
611
+ wasm_cluster_free_aux_stack(exec_env_tls,
612
+ (uint64)exec_env->aux_stack_bottom);
613
+
614
+ os_mutex_lock(&cluster->lock);
615
+
616
+ /* Remove exec_env */
617
+ wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
618
+ /* Destroy exec_env */
619
+ wasm_exec_env_destroy_internal(exec_env);
620
+ /* Routine exit, destroy instance */
621
+ wasm_runtime_deinstantiate_internal(module_inst, true);
622
+
623
+ os_mutex_unlock(&cluster->lock);
624
+ }
625
+
626
+ /* start routine of thread manager */
627
+ static void *
628
+ thread_manager_start_routine(void *arg)
629
+ {
630
+ void *ret;
631
+ WASMExecEnv *exec_env = (WASMExecEnv *)arg;
632
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
633
+ WASMModuleInstanceCommon *module_inst =
634
+ wasm_exec_env_get_module_inst(exec_env);
635
+
636
+ bh_assert(cluster != NULL);
637
+ bh_assert(module_inst != NULL);
638
+
639
+ os_mutex_lock(&exec_env->wait_lock);
640
+ exec_env->handle = os_self_thread();
641
+ /* Notify the parent thread to continue running */
642
+ os_cond_signal(&exec_env->wait_cond);
643
+ os_mutex_unlock(&exec_env->wait_lock);
644
+
645
+ ret = exec_env->thread_start_routine(exec_env);
646
+
647
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
648
+ os_mutex_lock(&exec_env->wait_lock);
649
+ if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags)
650
+ & WASM_SUSPEND_FLAG_EXIT)
651
+ ret = exec_env->thread_ret_value;
652
+ os_mutex_unlock(&exec_env->wait_lock);
653
+ #endif
654
+
655
+ /* Routine exit */
656
+
657
+ #if WASM_ENABLE_DEBUG_INTERP != 0
658
+ wasm_cluster_thread_exited(exec_env);
659
+ #endif
660
+
661
+ /* Free aux stack space */
662
+ if (exec_env->is_aux_stack_allocated)
663
+ wasm_cluster_free_aux_stack(exec_env,
664
+ (uint64)exec_env->aux_stack_bottom);
665
+
666
+ os_mutex_lock(&cluster_list_lock);
667
+
668
+ os_mutex_lock(&cluster->lock);
669
+
670
+ /* Detach the native thread here to ensure the resources are freed */
671
+ if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
672
+ /* Only detach current thread when there is no other thread
673
+ joining it, otherwise let the system resources for the
674
+ thread be released after joining */
675
+ os_thread_detach(exec_env->handle);
676
+ /* No need to set exec_env->thread_is_detached to true here
677
+ since we will exit soon */
678
+ }
679
+
680
+ #if WASM_ENABLE_PERF_PROFILING != 0
681
+ os_printf("============= Spawned thread ===========\n");
682
+ wasm_runtime_dump_perf_profiling(module_inst);
683
+ os_printf("========================================\n");
684
+ #endif
685
+
686
+ /* Remove exec_env */
687
+ wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
688
+ /* Destroy exec_env */
689
+ wasm_exec_env_destroy_internal(exec_env);
690
+ /* Routine exit, destroy instance */
691
+ wasm_runtime_deinstantiate_internal(module_inst, true);
692
+
693
+ os_mutex_unlock(&cluster->lock);
694
+
695
+ os_mutex_unlock(&cluster_list_lock);
696
+
697
+ os_thread_exit(ret);
698
+ return ret;
699
+ }
700
+
701
+ int32
702
+ wasm_cluster_create_thread(WASMExecEnv *exec_env,
703
+ wasm_module_inst_t module_inst,
704
+ bool is_aux_stack_allocated, uint64 aux_stack_start,
705
+ uint32 aux_stack_size,
706
+ void *(*thread_routine)(void *), void *arg)
707
+ {
708
+ WASMCluster *cluster;
709
+ WASMExecEnv *new_exec_env;
710
+ korp_tid tid;
711
+
712
+ cluster = wasm_exec_env_get_cluster(exec_env);
713
+ bh_assert(cluster);
714
+
715
+ os_mutex_lock(&cluster->lock);
716
+
717
+ if (cluster->has_exception || cluster->processing) {
718
+ goto fail1;
719
+ }
720
+
721
+ new_exec_env =
722
+ wasm_exec_env_create_internal(module_inst, exec_env->wasm_stack_size);
723
+ if (!new_exec_env)
724
+ goto fail1;
725
+
726
+ if (is_aux_stack_allocated) {
727
+ /* Set aux stack for current thread */
728
+ if (!wasm_exec_env_set_aux_stack(new_exec_env, aux_stack_start,
729
+ aux_stack_size)) {
730
+ goto fail2;
731
+ }
732
+ new_exec_env->is_aux_stack_allocated = true;
733
+ }
734
+ else {
735
+ /* Disable aux stack */
736
+ new_exec_env->aux_stack_boundary = 0;
737
+ new_exec_env->aux_stack_bottom = UINTPTR_MAX;
738
+ new_exec_env->is_aux_stack_allocated = false;
739
+ }
740
+
741
+ /* Inherit suspend_flags of parent thread */
742
+ new_exec_env->suspend_flags.flags =
743
+ (exec_env->suspend_flags.flags & WASM_SUSPEND_FLAG_INHERIT_MASK);
744
+
745
+ if (!wasm_cluster_add_exec_env(cluster, new_exec_env))
746
+ goto fail2;
747
+
748
+ new_exec_env->thread_start_routine = thread_routine;
749
+ new_exec_env->thread_arg = arg;
750
+
751
+ os_mutex_lock(&new_exec_env->wait_lock);
752
+
753
+ if (0
754
+ != os_thread_create(&tid, thread_manager_start_routine,
755
+ (void *)new_exec_env,
756
+ APP_THREAD_STACK_SIZE_DEFAULT)) {
757
+ os_mutex_unlock(&new_exec_env->wait_lock);
758
+ goto fail3;
759
+ }
760
+
761
+ /* Wait until the new_exec_env->handle is set to avoid it is
762
+ illegally accessed after unlocking cluster->lock */
763
+ os_cond_wait(&new_exec_env->wait_cond, &new_exec_env->wait_lock);
764
+ os_mutex_unlock(&new_exec_env->wait_lock);
765
+
766
+ os_mutex_unlock(&cluster->lock);
767
+
768
+ return 0;
769
+
770
+ fail3:
771
+ wasm_cluster_del_exec_env_internal(cluster, new_exec_env, false);
772
+ fail2:
773
+ wasm_exec_env_destroy_internal(new_exec_env);
774
+ fail1:
775
+ os_mutex_unlock(&cluster->lock);
776
+
777
+ return -1;
778
+ }
779
+
780
+ bool
781
+ wasm_cluster_dup_c_api_imports(WASMModuleInstanceCommon *module_inst_dst,
782
+ const WASMModuleInstanceCommon *module_inst_src)
783
+ {
784
+ /* workaround about passing instantiate-linking information */
785
+ CApiFuncImport **new_c_api_func_imports = NULL;
786
+ CApiFuncImport *c_api_func_imports = NULL;
787
+ uint32 import_func_count = 0;
788
+ uint32 size_in_bytes = 0;
789
+
790
+ #if WASM_ENABLE_INTERP != 0
791
+ if (module_inst_src->module_type == Wasm_Module_Bytecode) {
792
+ new_c_api_func_imports =
793
+ &(((WASMModuleInstance *)module_inst_dst)->c_api_func_imports);
794
+ c_api_func_imports =
795
+ ((const WASMModuleInstance *)module_inst_src)->c_api_func_imports;
796
+ import_func_count =
797
+ ((WASMModule *)(((const WASMModuleInstance *)module_inst_src)
798
+ ->module))
799
+ ->import_function_count;
800
+ }
801
+ #endif
802
+ #if WASM_ENABLE_AOT != 0
803
+ if (module_inst_src->module_type == Wasm_Module_AoT) {
804
+ new_c_api_func_imports =
805
+ &(((AOTModuleInstance *)module_inst_dst)->c_api_func_imports);
806
+ c_api_func_imports =
807
+ ((const AOTModuleInstance *)module_inst_src)->c_api_func_imports;
808
+ import_func_count =
809
+ ((AOTModule *)(((AOTModuleInstance *)module_inst_src)->module))
810
+ ->import_func_count;
811
+ }
812
+ #endif
813
+
814
+ if (import_func_count != 0 && c_api_func_imports) {
815
+ size_in_bytes = sizeof(CApiFuncImport) * import_func_count;
816
+ *new_c_api_func_imports = wasm_runtime_malloc(size_in_bytes);
817
+ if (!(*new_c_api_func_imports))
818
+ return false;
819
+
820
+ bh_memcpy_s(*new_c_api_func_imports, size_in_bytes, c_api_func_imports,
821
+ size_in_bytes);
822
+ }
823
+ return true;
824
+ }
825
+
826
+ #if WASM_ENABLE_DEBUG_INTERP != 0
827
+ WASMCurrentEnvStatus *
828
+ wasm_cluster_create_exenv_status()
829
+ {
830
+ WASMCurrentEnvStatus *status;
831
+
832
+ if (!(status = wasm_runtime_malloc(sizeof(WASMCurrentEnvStatus)))) {
833
+ return NULL;
834
+ }
835
+
836
+ status->step_count = 0;
837
+ status->signal_flag = 0;
838
+ status->running_status = 0;
839
+ return status;
840
+ }
841
+
842
+ void
843
+ wasm_cluster_destroy_exenv_status(WASMCurrentEnvStatus *status)
844
+ {
845
+ wasm_runtime_free(status);
846
+ }
847
+
848
+ inline static bool
849
+ wasm_cluster_thread_is_running(WASMExecEnv *exec_env)
850
+ {
851
+ return exec_env->current_status->running_status == STATUS_RUNNING
852
+ || exec_env->current_status->running_status == STATUS_STEP;
853
+ }
854
+
855
+ void
856
+ wasm_cluster_clear_thread_signal(WASMExecEnv *exec_env)
857
+ {
858
+ exec_env->current_status->signal_flag = 0;
859
+ }
860
+
861
+ void
862
+ wasm_cluster_thread_send_signal(WASMExecEnv *exec_env, uint32 signo)
863
+ {
864
+ exec_env->current_status->signal_flag = signo;
865
+ }
866
+
867
+ static void
868
+ notify_debug_instance(WASMExecEnv *exec_env)
869
+ {
870
+ WASMCluster *cluster;
871
+
872
+ cluster = wasm_exec_env_get_cluster(exec_env);
873
+ bh_assert(cluster);
874
+
875
+ if (!cluster->debug_inst) {
876
+ return;
877
+ }
878
+
879
+ on_thread_stop_event(cluster->debug_inst, exec_env);
880
+ }
881
+
882
+ static void
883
+ notify_debug_instance_exit(WASMExecEnv *exec_env)
884
+ {
885
+ WASMCluster *cluster;
886
+
887
+ cluster = wasm_exec_env_get_cluster(exec_env);
888
+ bh_assert(cluster);
889
+
890
+ if (!cluster->debug_inst) {
891
+ return;
892
+ }
893
+
894
+ on_thread_exit_event(cluster->debug_inst, exec_env);
895
+ }
896
+
897
+ void
898
+ wasm_cluster_thread_waiting_run(WASMExecEnv *exec_env)
899
+ {
900
+ exec_env->current_status->running_status = STATUS_STOP;
901
+ notify_debug_instance(exec_env);
902
+
903
+ while (!wasm_cluster_thread_is_running(exec_env)) {
904
+ os_cond_wait(&exec_env->wait_cond, &exec_env->wait_lock);
905
+ }
906
+ }
907
+
908
+ void
909
+ wasm_cluster_send_signal_all(WASMCluster *cluster, uint32 signo)
910
+ {
911
+ WASMExecEnv *exec_env = bh_list_first_elem(&cluster->exec_env_list);
912
+ while (exec_env) {
913
+ wasm_cluster_thread_send_signal(exec_env, signo);
914
+ exec_env = bh_list_elem_next(exec_env);
915
+ }
916
+ }
917
+
918
+ void
919
+ wasm_cluster_thread_exited(WASMExecEnv *exec_env)
920
+ {
921
+ exec_env->current_status->running_status = STATUS_EXIT;
922
+ notify_debug_instance_exit(exec_env);
923
+ }
924
+
925
+ void
926
+ wasm_cluster_thread_continue(WASMExecEnv *exec_env)
927
+ {
928
+ os_mutex_lock(&exec_env->wait_lock);
929
+ wasm_cluster_clear_thread_signal(exec_env);
930
+ exec_env->current_status->running_status = STATUS_RUNNING;
931
+ os_cond_signal(&exec_env->wait_cond);
932
+ os_mutex_unlock(&exec_env->wait_lock);
933
+ }
934
+
935
+ void
936
+ wasm_cluster_thread_step(WASMExecEnv *exec_env)
937
+ {
938
+ os_mutex_lock(&exec_env->wait_lock);
939
+ exec_env->current_status->running_status = STATUS_STEP;
940
+ os_cond_signal(&exec_env->wait_cond);
941
+ os_mutex_unlock(&exec_env->wait_lock);
942
+ }
943
+
944
+ void
945
+ wasm_cluster_set_debug_inst(WASMCluster *cluster, WASMDebugInstance *inst)
946
+ {
947
+ cluster->debug_inst = inst;
948
+ }
949
+
950
+ #endif /* end of WASM_ENABLE_DEBUG_INTERP */
951
+
952
+ /* Check whether the exec_env is in one of all clusters, the caller
953
+ should add lock to the cluster list before calling us */
954
+ static bool
955
+ clusters_have_exec_env(WASMExecEnv *exec_env)
956
+ {
957
+ WASMCluster *cluster = bh_list_first_elem(cluster_list);
958
+ WASMExecEnv *node;
959
+
960
+ while (cluster) {
961
+ os_mutex_lock(&cluster->lock);
962
+ node = bh_list_first_elem(&cluster->exec_env_list);
963
+
964
+ while (node) {
965
+ if (node == exec_env) {
966
+ bh_assert(exec_env->cluster == cluster);
967
+ os_mutex_unlock(&cluster->lock);
968
+ return true;
969
+ }
970
+ node = bh_list_elem_next(node);
971
+ }
972
+ os_mutex_unlock(&cluster->lock);
973
+
974
+ cluster = bh_list_elem_next(cluster);
975
+ }
976
+
977
+ return false;
978
+ }
979
+
980
+ int32
981
+ wasm_cluster_join_thread(WASMExecEnv *exec_env, void **ret_val)
982
+ {
983
+ korp_tid handle;
984
+
985
+ os_mutex_lock(&cluster_list_lock);
986
+
987
+ if (!clusters_have_exec_env(exec_env) || exec_env->thread_is_detached) {
988
+ /* Invalid thread, thread has exited or thread has been detached */
989
+ if (ret_val)
990
+ *ret_val = NULL;
991
+ os_mutex_unlock(&cluster_list_lock);
992
+ return 0;
993
+ }
994
+
995
+ os_mutex_lock(&exec_env->wait_lock);
996
+ exec_env->wait_count++;
997
+ handle = exec_env->handle;
998
+ os_mutex_unlock(&exec_env->wait_lock);
999
+
1000
+ os_mutex_unlock(&cluster_list_lock);
1001
+
1002
+ return os_thread_join(handle, ret_val);
1003
+ }
1004
+
1005
+ int32
1006
+ wasm_cluster_detach_thread(WASMExecEnv *exec_env)
1007
+ {
1008
+ int32 ret = 0;
1009
+
1010
+ os_mutex_lock(&cluster_list_lock);
1011
+ if (!clusters_have_exec_env(exec_env)) {
1012
+ /* Invalid thread or the thread has exited */
1013
+ os_mutex_unlock(&cluster_list_lock);
1014
+ return 0;
1015
+ }
1016
+ if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
1017
+ /* Only detach current thread when there is no other thread
1018
+ joining it, otherwise let the system resources for the
1019
+ thread be released after joining */
1020
+ ret = os_thread_detach(exec_env->handle);
1021
+ exec_env->thread_is_detached = true;
1022
+ }
1023
+ os_mutex_unlock(&cluster_list_lock);
1024
+ return ret;
1025
+ }
1026
+
1027
+ void
1028
+ wasm_cluster_exit_thread(WASMExecEnv *exec_env, void *retval)
1029
+ {
1030
+ WASMCluster *cluster;
1031
+ WASMModuleInstanceCommon *module_inst;
1032
+
1033
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
1034
+ if (exec_env->jmpbuf_stack_top) {
1035
+ /* Store the return value in exec_env */
1036
+ exec_env->thread_ret_value = retval;
1037
+
1038
+ WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
1039
+ WASM_SUSPEND_FLAG_EXIT);
1040
+
1041
+ #ifndef BH_PLATFORM_WINDOWS
1042
+ /* Pop all jmpbuf_node except the last one */
1043
+ while (exec_env->jmpbuf_stack_top->prev) {
1044
+ wasm_exec_env_pop_jmpbuf(exec_env);
1045
+ }
1046
+ os_longjmp(exec_env->jmpbuf_stack_top->jmpbuf, 1);
1047
+ return;
1048
+ #endif
1049
+ }
1050
+ #endif
1051
+
1052
+ cluster = wasm_exec_env_get_cluster(exec_env);
1053
+ bh_assert(cluster);
1054
+ #if WASM_ENABLE_DEBUG_INTERP != 0
1055
+ wasm_cluster_clear_thread_signal(exec_env);
1056
+ wasm_cluster_thread_exited(exec_env);
1057
+ #endif
1058
+
1059
+ /* Free aux stack space */
1060
+ if (exec_env->is_aux_stack_allocated)
1061
+ wasm_cluster_free_aux_stack(exec_env,
1062
+ (uint64)exec_env->aux_stack_bottom);
1063
+
1064
+ /* App exit the thread, free the resources before exit native thread */
1065
+
1066
+ os_mutex_lock(&cluster_list_lock);
1067
+
1068
+ os_mutex_lock(&cluster->lock);
1069
+
1070
+ /* Detach the native thread here to ensure the resources are freed */
1071
+ if (exec_env->wait_count == 0 && !exec_env->thread_is_detached) {
1072
+ /* Only detach current thread when there is no other thread
1073
+ joining it, otherwise let the system resources for the
1074
+ thread be released after joining */
1075
+ os_thread_detach(exec_env->handle);
1076
+ /* No need to set exec_env->thread_is_detached to true here
1077
+ since we will exit soon */
1078
+ }
1079
+
1080
+ module_inst = exec_env->module_inst;
1081
+
1082
+ /* Remove exec_env */
1083
+ wasm_cluster_del_exec_env_internal(cluster, exec_env, false);
1084
+ /* Destroy exec_env */
1085
+ wasm_exec_env_destroy_internal(exec_env);
1086
+ /* Routine exit, destroy instance */
1087
+ wasm_runtime_deinstantiate_internal(module_inst, true);
1088
+
1089
+ os_mutex_unlock(&cluster->lock);
1090
+
1091
+ os_mutex_unlock(&cluster_list_lock);
1092
+
1093
+ os_thread_exit(retval);
1094
+ }
1095
+
1096
+ static void
1097
+ set_thread_cancel_flags(WASMExecEnv *exec_env)
1098
+ {
1099
+ os_mutex_lock(&exec_env->wait_lock);
1100
+
1101
+ #if WASM_ENABLE_DEBUG_INTERP != 0
1102
+ wasm_cluster_thread_send_signal(exec_env, WAMR_SIG_TERM);
1103
+ #endif
1104
+ WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
1105
+ WASM_SUSPEND_FLAG_TERMINATE);
1106
+
1107
+ os_mutex_unlock(&exec_env->wait_lock);
1108
+
1109
+ #ifdef OS_ENABLE_WAKEUP_BLOCKING_OP
1110
+ wasm_runtime_interrupt_blocking_op(exec_env);
1111
+ #endif
1112
+ }
1113
+
1114
+ static void
1115
+ clear_thread_cancel_flags(WASMExecEnv *exec_env)
1116
+ {
1117
+ os_mutex_lock(&exec_env->wait_lock);
1118
+ WASM_SUSPEND_FLAGS_FETCH_AND(exec_env->suspend_flags,
1119
+ ~WASM_SUSPEND_FLAG_TERMINATE);
1120
+ os_mutex_unlock(&exec_env->wait_lock);
1121
+ }
1122
+
1123
+ int32
1124
+ wasm_cluster_cancel_thread(WASMExecEnv *exec_env)
1125
+ {
1126
+ os_mutex_lock(&cluster_list_lock);
1127
+
1128
+ if (!exec_env->cluster) {
1129
+ os_mutex_unlock(&cluster_list_lock);
1130
+ return 0;
1131
+ }
1132
+
1133
+ if (!clusters_have_exec_env(exec_env)) {
1134
+ /* Invalid thread or the thread has exited */
1135
+ goto final;
1136
+ }
1137
+
1138
+ set_thread_cancel_flags(exec_env);
1139
+
1140
+ final:
1141
+ os_mutex_unlock(&cluster_list_lock);
1142
+
1143
+ return 0;
1144
+ }
1145
+
1146
+ static void
1147
+ terminate_thread_visitor(void *node, void *user_data)
1148
+ {
1149
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1150
+ WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
1151
+
1152
+ if (curr_exec_env == exec_env)
1153
+ return;
1154
+
1155
+ wasm_cluster_cancel_thread(curr_exec_env);
1156
+ wasm_cluster_join_thread(curr_exec_env, NULL);
1157
+ }
1158
+
1159
+ void
1160
+ wasm_cluster_terminate_all(WASMCluster *cluster)
1161
+ {
1162
+ os_mutex_lock(&cluster->lock);
1163
+ cluster->processing = true;
1164
+
1165
+ safe_traverse_exec_env_list(cluster, terminate_thread_visitor, NULL);
1166
+
1167
+ cluster->processing = false;
1168
+ os_mutex_unlock(&cluster->lock);
1169
+ }
1170
+
1171
+ void
1172
+ wasm_cluster_terminate_all_except_self(WASMCluster *cluster,
1173
+ WASMExecEnv *exec_env)
1174
+ {
1175
+ os_mutex_lock(&cluster->lock);
1176
+ cluster->processing = true;
1177
+
1178
+ safe_traverse_exec_env_list(cluster, terminate_thread_visitor,
1179
+ (void *)exec_env);
1180
+
1181
+ cluster->processing = false;
1182
+ os_mutex_unlock(&cluster->lock);
1183
+ }
1184
+
1185
+ static void
1186
+ wait_for_thread_visitor(void *node, void *user_data)
1187
+ {
1188
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1189
+ WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
1190
+
1191
+ if (curr_exec_env == exec_env)
1192
+ return;
1193
+
1194
+ wasm_cluster_join_thread(curr_exec_env, NULL);
1195
+ }
1196
+
1197
+ void
1198
+ wasm_cluster_wait_for_all(WASMCluster *cluster)
1199
+ {
1200
+ os_mutex_lock(&cluster->lock);
1201
+ cluster->processing = true;
1202
+
1203
+ safe_traverse_exec_env_list(cluster, wait_for_thread_visitor, NULL);
1204
+
1205
+ cluster->processing = false;
1206
+ os_mutex_unlock(&cluster->lock);
1207
+ }
1208
+
1209
+ void
1210
+ wasm_cluster_wait_for_all_except_self(WASMCluster *cluster,
1211
+ WASMExecEnv *exec_env)
1212
+ {
1213
+ os_mutex_lock(&cluster->lock);
1214
+ cluster->processing = true;
1215
+
1216
+ safe_traverse_exec_env_list(cluster, wait_for_thread_visitor,
1217
+ (void *)exec_env);
1218
+
1219
+ cluster->processing = false;
1220
+ os_mutex_unlock(&cluster->lock);
1221
+ }
1222
+
1223
+ bool
1224
+ wasm_cluster_register_destroy_callback(void (*callback)(WASMCluster *))
1225
+ {
1226
+ DestroyCallBackNode *node;
1227
+
1228
+ if (!(node = wasm_runtime_malloc(sizeof(DestroyCallBackNode)))) {
1229
+ LOG_ERROR("thread manager error: failed to allocate memory");
1230
+ return false;
1231
+ }
1232
+ node->destroy_cb = callback;
1233
+ bh_list_insert(destroy_callback_list, node);
1234
+ return true;
1235
+ }
1236
+
1237
+ void
1238
+ wasm_cluster_suspend_thread(WASMExecEnv *exec_env)
1239
+ {
1240
+ /* Set the suspend flag */
1241
+ WASM_SUSPEND_FLAGS_FETCH_OR(exec_env->suspend_flags,
1242
+ WASM_SUSPEND_FLAG_SUSPEND);
1243
+ }
1244
+
1245
+ static void
1246
+ suspend_thread_visitor(void *node, void *user_data)
1247
+ {
1248
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1249
+ WASMExecEnv *exec_env = (WASMExecEnv *)user_data;
1250
+
1251
+ if (curr_exec_env == exec_env)
1252
+ return;
1253
+
1254
+ wasm_cluster_suspend_thread(curr_exec_env);
1255
+ }
1256
+
1257
+ void
1258
+ wasm_cluster_suspend_all(WASMCluster *cluster)
1259
+ {
1260
+ os_mutex_lock(&cluster->lock);
1261
+ traverse_list(&cluster->exec_env_list, suspend_thread_visitor, NULL);
1262
+ os_mutex_unlock(&cluster->lock);
1263
+ }
1264
+
1265
+ void
1266
+ wasm_cluster_suspend_all_except_self(WASMCluster *cluster,
1267
+ WASMExecEnv *exec_env)
1268
+ {
1269
+ os_mutex_lock(&cluster->lock);
1270
+ traverse_list(&cluster->exec_env_list, suspend_thread_visitor,
1271
+ (void *)exec_env);
1272
+ os_mutex_unlock(&cluster->lock);
1273
+ }
1274
+
1275
+ void
1276
+ wasm_cluster_resume_thread(WASMExecEnv *exec_env)
1277
+ {
1278
+ WASM_SUSPEND_FLAGS_FETCH_AND(exec_env->suspend_flags,
1279
+ ~WASM_SUSPEND_FLAG_SUSPEND);
1280
+ os_cond_signal(&exec_env->wait_cond);
1281
+ }
1282
+
1283
+ static void
1284
+ resume_thread_visitor(void *node, void *user_data)
1285
+ {
1286
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1287
+
1288
+ wasm_cluster_resume_thread(curr_exec_env);
1289
+ }
1290
+
1291
+ void
1292
+ wasm_cluster_resume_all(WASMCluster *cluster)
1293
+ {
1294
+ os_mutex_lock(&cluster->lock);
1295
+ traverse_list(&cluster->exec_env_list, resume_thread_visitor, NULL);
1296
+ os_mutex_unlock(&cluster->lock);
1297
+ }
1298
+
1299
+ struct spread_exception_data {
1300
+ WASMExecEnv *skip;
1301
+ const char *exception;
1302
+ };
1303
+
1304
+ static void
1305
+ set_exception_visitor(void *node, void *user_data)
1306
+ {
1307
+ const struct spread_exception_data *data = user_data;
1308
+ WASMExecEnv *exec_env = (WASMExecEnv *)node;
1309
+
1310
+ if (exec_env != data->skip) {
1311
+ WASMModuleInstance *wasm_inst =
1312
+ (WASMModuleInstance *)get_module_inst(exec_env);
1313
+
1314
+ exception_lock(wasm_inst);
1315
+ if (data->exception != NULL) {
1316
+ snprintf(wasm_inst->cur_exception, sizeof(wasm_inst->cur_exception),
1317
+ "Exception: %s", data->exception);
1318
+ }
1319
+ else {
1320
+ wasm_inst->cur_exception[0] = '\0';
1321
+ }
1322
+ exception_unlock(wasm_inst);
1323
+
1324
+ /* Terminate the thread so it can exit from dead loops */
1325
+ if (data->exception != NULL) {
1326
+ set_thread_cancel_flags(exec_env);
1327
+ }
1328
+ else {
1329
+ clear_thread_cancel_flags(exec_env);
1330
+ }
1331
+ }
1332
+ }
1333
+
1334
+ void
1335
+ wasm_cluster_set_exception(WASMExecEnv *exec_env, const char *exception)
1336
+ {
1337
+ const bool has_exception = exception != NULL;
1338
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
1339
+ bh_assert(cluster);
1340
+
1341
+ struct spread_exception_data data;
1342
+ data.skip = NULL;
1343
+ data.exception = exception;
1344
+
1345
+ os_mutex_lock(&cluster->lock);
1346
+ #if WASM_ENABLE_DUMP_CALL_STACK != 0
1347
+ if (has_exception) {
1348
+ /* Save the stack frames of the crashed thread into the cluster */
1349
+ WASMModuleInstance *module_inst =
1350
+ (WASMModuleInstance *)get_module_inst(exec_env);
1351
+
1352
+ #if WASM_ENABLE_INTERP != 0
1353
+ if (module_inst->module_type == Wasm_Module_Bytecode
1354
+ && wasm_interp_create_call_stack(exec_env)) {
1355
+ wasm_frame_vec_clone_internal(module_inst->frames,
1356
+ &cluster->exception_frames);
1357
+ }
1358
+ #endif
1359
+
1360
+ #if WASM_ENABLE_AOT != 0
1361
+ if (module_inst->module_type == Wasm_Module_AoT
1362
+ && aot_create_call_stack(exec_env)) {
1363
+ wasm_frame_vec_clone_internal(module_inst->frames,
1364
+ &cluster->exception_frames);
1365
+ }
1366
+ #endif
1367
+ }
1368
+ #endif /* WASM_ENABLE_DUMP_CALL_STACK != 0 */
1369
+ cluster->has_exception = has_exception;
1370
+ traverse_list(&cluster->exec_env_list, set_exception_visitor, &data);
1371
+ os_mutex_unlock(&cluster->lock);
1372
+ }
1373
+
1374
+ static void
1375
+ set_custom_data_visitor(void *node, void *user_data)
1376
+ {
1377
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1378
+ WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
1379
+
1380
+ wasm_runtime_set_custom_data_internal(module_inst, user_data);
1381
+ }
1382
+
1383
+ void
1384
+ wasm_cluster_spread_custom_data(WASMModuleInstanceCommon *module_inst,
1385
+ void *custom_data)
1386
+ {
1387
+ WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
1388
+
1389
+ if (exec_env == NULL) {
1390
+ /* Maybe threads have not been started yet. */
1391
+ wasm_runtime_set_custom_data_internal(module_inst, custom_data);
1392
+ }
1393
+ else {
1394
+ WASMCluster *cluster;
1395
+
1396
+ cluster = wasm_exec_env_get_cluster(exec_env);
1397
+ bh_assert(cluster);
1398
+
1399
+ os_mutex_lock(&cluster->lock);
1400
+ traverse_list(&cluster->exec_env_list, set_custom_data_visitor,
1401
+ custom_data);
1402
+ os_mutex_unlock(&cluster->lock);
1403
+ }
1404
+ }
1405
+
1406
+ #if WASM_ENABLE_SHARED_HEAP != 0
1407
+ static void
1408
+ attach_shared_heap_visitor(void *node, void *heap)
1409
+ {
1410
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1411
+ WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
1412
+
1413
+ wasm_runtime_attach_shared_heap_internal(module_inst, heap);
1414
+ }
1415
+
1416
+ static void
1417
+ detach_shared_heap_visitor(void *node, void *heap)
1418
+ {
1419
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1420
+ WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
1421
+
1422
+ (void)heap;
1423
+ wasm_runtime_detach_shared_heap_internal(module_inst);
1424
+ }
1425
+
1426
+ bool
1427
+ wasm_cluster_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
1428
+ WASMSharedHeap *heap)
1429
+ {
1430
+ WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
1431
+
1432
+ if (exec_env == NULL) {
1433
+ /* Maybe threads have not been started yet. */
1434
+ return wasm_runtime_attach_shared_heap_internal(module_inst, heap);
1435
+ }
1436
+ else {
1437
+ WASMCluster *cluster;
1438
+
1439
+ cluster = wasm_exec_env_get_cluster(exec_env);
1440
+ bh_assert(cluster);
1441
+
1442
+ os_mutex_lock(&cluster->lock);
1443
+ /* Try attaching shared heap to this module instance first
1444
+ to ensure that we can attach it to all other instances. */
1445
+ if (!wasm_runtime_attach_shared_heap_internal(module_inst, heap)) {
1446
+ os_mutex_unlock(&cluster->lock);
1447
+ return false;
1448
+ }
1449
+ /* Detach the shared heap so it can be attached again. */
1450
+ wasm_runtime_detach_shared_heap_internal(module_inst);
1451
+ traverse_list(&cluster->exec_env_list, attach_shared_heap_visitor,
1452
+ heap);
1453
+ os_mutex_unlock(&cluster->lock);
1454
+ }
1455
+
1456
+ return true;
1457
+ }
1458
+
1459
+ void
1460
+ wasm_cluster_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
1461
+ {
1462
+ WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
1463
+
1464
+ if (exec_env == NULL) {
1465
+ /* Maybe threads have not been started yet. */
1466
+ wasm_runtime_detach_shared_heap_internal(module_inst);
1467
+ }
1468
+ else {
1469
+ WASMCluster *cluster;
1470
+
1471
+ cluster = wasm_exec_env_get_cluster(exec_env);
1472
+ bh_assert(cluster);
1473
+
1474
+ os_mutex_lock(&cluster->lock);
1475
+ traverse_list(&cluster->exec_env_list, detach_shared_heap_visitor,
1476
+ NULL);
1477
+ os_mutex_unlock(&cluster->lock);
1478
+ }
1479
+ }
1480
+ #endif
1481
+
1482
+ #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
1483
+ struct inst_set_context_data {
1484
+ void *key;
1485
+ void *ctx;
1486
+ };
1487
+
1488
+ static void
1489
+ set_context_visitor(void *node, void *user_data)
1490
+ {
1491
+ WASMExecEnv *curr_exec_env = (WASMExecEnv *)node;
1492
+ WASMModuleInstanceCommon *module_inst = get_module_inst(curr_exec_env);
1493
+ const struct inst_set_context_data *data = user_data;
1494
+
1495
+ wasm_runtime_set_context(module_inst, data->key, data->ctx);
1496
+ }
1497
+
1498
+ void
1499
+ wasm_cluster_set_context(WASMModuleInstanceCommon *module_inst, void *key,
1500
+ void *ctx)
1501
+ {
1502
+ WASMExecEnv *exec_env = wasm_clusters_search_exec_env(module_inst);
1503
+
1504
+ if (exec_env == NULL) {
1505
+ /* Maybe threads have not been started yet. */
1506
+ wasm_runtime_set_context(module_inst, key, ctx);
1507
+ }
1508
+ else {
1509
+ WASMCluster *cluster;
1510
+ struct inst_set_context_data data;
1511
+ data.key = key;
1512
+ data.ctx = ctx;
1513
+
1514
+ cluster = wasm_exec_env_get_cluster(exec_env);
1515
+ bh_assert(cluster);
1516
+
1517
+ os_mutex_lock(&cluster->lock);
1518
+ traverse_list(&cluster->exec_env_list, set_context_visitor, &data);
1519
+ os_mutex_unlock(&cluster->lock);
1520
+ }
1521
+ }
1522
+ #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
1523
+
1524
+ bool
1525
+ wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
1526
+ {
1527
+ os_mutex_lock(&exec_env->wait_lock);
1528
+ bool is_thread_terminated = (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags)
1529
+ & WASM_SUSPEND_FLAG_TERMINATE)
1530
+ ? true
1531
+ : false;
1532
+ os_mutex_unlock(&exec_env->wait_lock);
1533
+
1534
+ return is_thread_terminated;
1535
+ }
1536
+
1537
+ void
1538
+ exception_lock(WASMModuleInstance *module_inst)
1539
+ {
1540
+ /*
1541
+ * Note: this lock could be per module instance if desirable.
1542
+ * We can revisit on AOT version bump.
1543
+ * It probably doesn't matter though because the exception handling
1544
+ * logic should not be executed too frequently anyway.
1545
+ */
1546
+ os_mutex_lock(&_exception_lock);
1547
+ }
1548
+
1549
+ void
1550
+ exception_unlock(WASMModuleInstance *module_inst)
1551
+ {
1552
+ os_mutex_unlock(&_exception_lock);
1553
+ }
1554
+
1555
+ void
1556
+ wasm_cluster_traverse_lock(WASMExecEnv *exec_env)
1557
+ {
1558
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
1559
+ bh_assert(cluster);
1560
+ os_mutex_lock(&cluster->lock);
1561
+ }
1562
+
1563
+ void
1564
+ wasm_cluster_traverse_unlock(WASMExecEnv *exec_env)
1565
+ {
1566
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
1567
+ bh_assert(cluster);
1568
+ os_mutex_unlock(&cluster->lock);
1569
+ }