Skip to content

Commit 216dc43

Browse files
authored
Use shared memory lock for threads generated from same module (bytecodealliance#1960)
Multiple threads generated from the same module should use the same lock to protect the atomic operations. Before this PR, each thread used a different lock to protect atomic operations (e.g. atomic add), making the lock ineffective. Fix bytecodealliance#1958.
1 parent 1c17665 commit 216dc43

File tree

11 files changed

+203
-146
lines changed

11 files changed

+203
-146
lines changed

core/iwasm/common/wasm_shared_memory.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ shared_memory_dec_reference(WASMModuleCommon *module)
172172
bh_list_remove(shared_memory_list, node);
173173
os_mutex_unlock(&shared_memory_list_lock);
174174

175+
os_mutex_destroy(&node->shared_mem_lock);
175176
os_mutex_destroy(&node->lock);
176177
wasm_runtime_free(node);
177178
}
@@ -200,7 +201,14 @@ shared_memory_set_memory_inst(WASMModuleCommon *module,
200201
node->module = module;
201202
node->memory_inst = memory;
202203
node->ref_count = 1;
204+
205+
if (os_mutex_init(&node->shared_mem_lock) != 0) {
206+
wasm_runtime_free(node);
207+
return NULL;
208+
}
209+
203210
if (os_mutex_init(&node->lock) != 0) {
211+
os_mutex_destroy(&node->shared_mem_lock);
204212
wasm_runtime_free(node);
205213
return NULL;
206214
}

core/iwasm/common/wasm_shared_memory.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ typedef struct WASMSharedMemNode {
2626
WASMModuleCommon *module;
2727
/* The memory information */
2828
WASMMemoryInstanceCommon *memory_inst;
29+
/* Lock used for atomic operations */
30+
korp_mutex shared_mem_lock;
2931

3032
/* reference count */
3133
uint32 ref_count;

core/iwasm/interpreter/wasm.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ struct WASMModule {
510510
uint64 load_size;
511511
#endif
512512

513-
#if WASM_ENABLE_DEBUG_INTERP != 0 \
514-
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
513+
#if WASM_ENABLE_DEBUG_INTERP != 0 \
514+
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
515515
&& WASM_ENABLE_LAZY_JIT != 0)
516516
/**
517517
* List of instances referred to this module. When source debugging

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 61 additions & 56 deletions
Large diffs are not rendered by default.

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 61 additions & 56 deletions
Large diffs are not rendered by default.

core/iwasm/interpreter/wasm_loader.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3864,8 +3864,8 @@ create_module(char *error_buf, uint32 error_buf_size)
38643864
bh_assert(ret == BH_LIST_SUCCESS);
38653865
#endif
38663866

3867-
#if WASM_ENABLE_DEBUG_INTERP != 0 \
3868-
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
3867+
#if WASM_ENABLE_DEBUG_INTERP != 0 \
3868+
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
38693869
&& WASM_ENABLE_LAZY_JIT != 0)
38703870
if (os_mutex_init(&module->instance_list_lock) != 0) {
38713871
set_error_buf(error_buf, error_buf_size,
@@ -4253,7 +4253,8 @@ wasm_loader_unload(WASMModule *module)
42534253
if (!module)
42544254
return;
42554255

4256-
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT && WASM_ENABLE_LAZY_JIT != 0
4256+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
4257+
&& WASM_ENABLE_LAZY_JIT != 0
42574258
module->orcjit_stop_compiling = true;
42584259
if (module->llvm_jit_init_thread)
42594260
os_thread_join(module->llvm_jit_init_thread, NULL);
@@ -4274,7 +4275,8 @@ wasm_loader_unload(WASMModule *module)
42744275
aot_destroy_comp_data(module->comp_data);
42754276
#endif
42764277

4277-
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT && WASM_ENABLE_LAZY_JIT != 0
4278+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
4279+
&& WASM_ENABLE_LAZY_JIT != 0
42784280
if (module->tierup_wait_lock_inited) {
42794281
os_mutex_destroy(&module->tierup_wait_lock);
42804282
os_cond_destroy(&module->tierup_wait_cond);
@@ -4403,8 +4405,8 @@ wasm_loader_unload(WASMModule *module)
44034405
}
44044406
#endif
44054407

4406-
#if WASM_ENABLE_DEBUG_INTERP != 0 \
4407-
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
4408+
#if WASM_ENABLE_DEBUG_INTERP != 0 \
4409+
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
44084410
&& WASM_ENABLE_LAZY_JIT != 0)
44094411
os_mutex_destroy(&module->instance_list_lock);
44104412
#endif

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,7 +2725,8 @@ create_module(char *error_buf, uint32 error_buf_size)
27252725
bh_assert(ret == BH_LIST_SUCCESS);
27262726
#endif
27272727

2728-
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT && WASM_ENABLE_LAZY_JIT != 0
2728+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
2729+
&& WASM_ENABLE_LAZY_JIT != 0
27292730
if (os_mutex_init(&module->instance_list_lock) != 0) {
27302731
set_error_buf(error_buf, error_buf_size,
27312732
"init instance list lock failed");
@@ -2946,7 +2947,8 @@ wasm_loader_unload(WASMModule *module)
29462947
if (!module)
29472948
return;
29482949

2949-
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT && WASM_ENABLE_LAZY_JIT != 0
2950+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
2951+
&& WASM_ENABLE_LAZY_JIT != 0
29502952
module->orcjit_stop_compiling = true;
29512953
if (module->llvm_jit_init_thread)
29522954
os_thread_join(module->llvm_jit_init_thread, NULL);
@@ -2967,7 +2969,8 @@ wasm_loader_unload(WASMModule *module)
29672969
aot_destroy_comp_data(module->comp_data);
29682970
#endif
29692971

2970-
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT && WASM_ENABLE_LAZY_JIT != 0
2972+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
2973+
&& WASM_ENABLE_LAZY_JIT != 0
29712974
if (module->tierup_wait_lock_inited) {
29722975
os_mutex_destroy(&module->tierup_wait_lock);
29732976
os_cond_destroy(&module->tierup_wait_cond);
@@ -3063,7 +3066,8 @@ wasm_loader_unload(WASMModule *module)
30633066
}
30643067
#endif
30653068

3066-
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT && WASM_ENABLE_LAZY_JIT != 0
3069+
#if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
3070+
&& WASM_ENABLE_LAZY_JIT != 0
30673071
os_mutex_destroy(&module->instance_list_lock);
30683072
#endif
30693073

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,15 +1587,6 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst, uint32 stack_size,
15871587
module_inst->e =
15881588
(WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
15891589

1590-
#if WASM_ENABLE_SHARED_MEMORY != 0
1591-
if (os_mutex_init(&module_inst->e->mem_lock) != 0) {
1592-
set_error_buf(error_buf, error_buf_size,
1593-
"create shared memory lock failed");
1594-
goto fail;
1595-
}
1596-
module_inst->e->mem_lock_inited = true;
1597-
#endif
1598-
15991590
#if WASM_ENABLE_MULTI_MODULE != 0
16001591
module_inst->e->sub_module_inst_list =
16011592
&module_inst->e->sub_module_inst_list_head;
@@ -2159,11 +2150,6 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
21592150
}
21602151
#endif
21612152

2162-
#if WASM_ENABLE_SHARED_MEMORY != 0
2163-
if (module_inst->e->mem_lock_inited)
2164-
os_mutex_destroy(&module_inst->e->mem_lock);
2165-
#endif
2166-
21672153
if (module_inst->e->c_api_func_imports)
21682154
wasm_runtime_free(module_inst->e->c_api_func_imports);
21692155

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,6 @@ typedef struct WASMModuleInstanceExtra {
223223
CApiFuncImport *c_api_func_imports;
224224
RunningMode running_mode;
225225

226-
#if WASM_ENABLE_SHARED_MEMORY != 0
227-
/* lock for shared memory atomic operations */
228-
korp_mutex mem_lock;
229-
bool mem_lock_inited;
230-
#endif
231-
232226
#if WASM_ENABLE_MULTI_MODULE != 0
233227
bh_list sub_module_inst_list_head;
234228
bh_list *sub_module_inst_list;
@@ -240,8 +234,8 @@ typedef struct WASMModuleInstanceExtra {
240234
uint32 max_aux_stack_used;
241235
#endif
242236

243-
#if WASM_ENABLE_DEBUG_INTERP != 0 \
244-
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
237+
#if WASM_ENABLE_DEBUG_INTERP != 0 \
238+
|| (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
245239
&& WASM_ENABLE_LAZY_JIT != 0)
246240
WASMModuleInstance *next;
247241
#endif

samples/multi-thread/wasm-apps/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ target_link_libraries(test.wasm)
4141

4242
add_executable(main_thread_exception.wasm main_thread_exception.c)
4343
target_link_libraries(main_thread_exception.wasm)
44+
45+
add_executable(main_global_atomic.wasm main_global_atomic.c)
46+
target_link_libraries(main_global_atomic.wasm)

0 commit comments

Comments
 (0)