Skip to content

Commit 3d96fbe

Browse files
committed
Move exception lock to ModuleInstance data.
This lock acquired on each native function call. This cause performance impact on programs, containing many native function calls, and running in multithreaded environment.
1 parent 9d1dc3e commit 3d96fbe

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,12 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
19631963
module_inst->e =
19641964
(WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
19651965
extra = (AOTModuleInstanceExtra *)module_inst->e;
1966+
#if WASM_ENABLE_THREAD_MGR != 0
1967+
if (os_mutex_init(&extra->common.exception_lock) != 0) {
1968+
wasm_runtime_free(module_inst);
1969+
return NULL;
1970+
}
1971+
#endif
19661972

19671973
#if WASM_ENABLE_GC != 0
19681974
/* Initialize gc heap first since it may be used when initializing
@@ -2353,6 +2359,10 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
23532359
wasm_exec_env_destroy((WASMExecEnv *)module_inst->exec_env_singleton);
23542360
}
23552361

2362+
#if WASM_ENABLE_THREAD_MGR != 0
2363+
os_mutex_destroy(&extra->common.exception_lock);
2364+
#endif
2365+
23562366
#if WASM_ENABLE_PERF_PROFILING != 0
23572367
if (module_inst->func_perf_profilings)
23582368
wasm_runtime_free(module_inst->func_perf_profilings);

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,6 +2512,12 @@ wasm_instantiate(WASMModule *module, WASMModuleInstance *parent,
25122512
module_inst->module = module;
25132513
module_inst->e =
25142514
(WASMModuleInstanceExtra *)((uint8 *)module_inst + extra_info_offset);
2515+
#if WASM_ENABLE_THREAD_MGR != 0
2516+
if (os_mutex_init(&module_inst->e->common.exception_lock) != 0) {
2517+
wasm_runtime_free(module_inst);
2518+
return NULL;
2519+
}
2520+
#endif
25152521

25162522
#if WASM_ENABLE_MULTI_MODULE != 0
25172523
module_inst->e->sub_module_inst_list =
@@ -3501,6 +3507,9 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
35013507
bh_bitmap_delete(module_inst->e->common.elem_dropped);
35023508
#endif
35033509

3510+
#if WASM_ENABLE_THREAD_MGR != 0
3511+
os_mutex_destroy(&module_inst->e->common.exception_lock);
3512+
#endif
35043513
wasm_runtime_free(module_inst);
35053514
}
35063515

core/iwasm/interpreter/wasm_runtime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ typedef struct WASMModuleInstanceExtraCommon {
334334
/* The gc heap created */
335335
void *gc_heap_handle;
336336
#endif
337+
#if WASM_ENABLE_THREAD_MGR != 0
338+
korp_mutex exception_lock;
339+
#endif
337340
} WASMModuleInstanceExtraCommon;
338341

339342
/* Extra info of WASM module instance for interpreter/jit mode */

core/iwasm/libraries/thread-mgr/thread_manager.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ static bh_list cluster_list_head;
2929
static bh_list *const cluster_list = &cluster_list_head;
3030
static korp_mutex cluster_list_lock;
3131

32-
static korp_mutex _exception_lock;
33-
3432
typedef void (*list_visitor)(void *, void *);
3533

3634
static uint32 cluster_max_thread_num = CLUSTER_MAX_THREAD_NUM;
@@ -51,10 +49,6 @@ thread_manager_init()
5149
return false;
5250
if (os_mutex_init(&cluster_list_lock) != 0)
5351
return false;
54-
if (os_mutex_init(&_exception_lock) != 0) {
55-
os_mutex_destroy(&cluster_list_lock);
56-
return false;
57-
}
5852
return true;
5953
}
6054

@@ -69,7 +63,6 @@ thread_manager_destroy()
6963
cluster = next;
7064
}
7165
wasm_cluster_cancel_all_callbacks();
72-
os_mutex_destroy(&_exception_lock);
7366
os_mutex_destroy(&cluster_list_lock);
7467
}
7568

@@ -1537,22 +1530,31 @@ wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
15371530
return is_thread_terminated;
15381531
}
15391532

1533+
static WASMModuleInstanceExtraCommon *
1534+
GetModuleinstanceCommon(WASMModuleInstance *module_inst)
1535+
{
1536+
#if WASM_ENABLE_AOT != 0
1537+
if (module_inst->module_type == Wasm_Module_AoT) {
1538+
return &((AOTModuleInstanceExtra *)module_inst->e)->common;
1539+
}
1540+
else {
1541+
return &module_inst->e->common;
1542+
}
1543+
#else
1544+
return &module_inst->e->common;
1545+
#endif
1546+
}
1547+
15401548
void
15411549
exception_lock(WASMModuleInstance *module_inst)
15421550
{
1543-
/*
1544-
* Note: this lock could be per module instance if desirable.
1545-
* We can revisit on AOT version bump.
1546-
* It probably doesn't matter though because the exception handling
1547-
* logic should not be executed too frequently anyway.
1548-
*/
1549-
os_mutex_lock(&_exception_lock);
1551+
os_mutex_lock(&GetModuleinstanceCommon(module_inst)->exception_lock);
15501552
}
15511553

15521554
void
15531555
exception_unlock(WASMModuleInstance *module_inst)
15541556
{
1555-
os_mutex_unlock(&_exception_lock);
1557+
os_mutex_unlock(&GetModuleinstanceCommon(module_inst)->exception_lock);
15561558
}
15571559

15581560
void

0 commit comments

Comments
 (0)