Skip to content

Commit 26aa924

Browse files
authored
Move exception lock to ModuleInstance data. (#4772)
* 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. Update AOT_CURRENT_VERSION constant.
1 parent 46472ee commit 26aa924

File tree

7 files changed

+43
-16
lines changed

7 files changed

+43
-16
lines changed

core/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
#endif
8585

8686
#define AOT_MAGIC_NUMBER 0x746f6100
87-
#define AOT_CURRENT_VERSION 5
87+
#define AOT_CURRENT_VERSION 6
8888

8989
#ifndef WASM_ENABLE_JIT
9090
#define WASM_ENABLE_JIT 0

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/common/wasm_runtime_common.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8136,3 +8136,18 @@ wasm_runtime_check_and_update_last_used_shared_heap(
81368136
return false;
81378137
}
81388138
#endif
8139+
8140+
WASMModuleInstanceExtraCommon *
8141+
GetModuleInstanceExtraCommon(WASMModuleInstance *module_inst)
8142+
{
8143+
#if WASM_ENABLE_AOT != 0
8144+
if (module_inst->module_type == Wasm_Module_AoT) {
8145+
return &((AOTModuleInstanceExtra *)module_inst->e)->common;
8146+
}
8147+
else {
8148+
return &module_inst->e->common;
8149+
}
8150+
#else
8151+
return &module_inst->e->common;
8152+
#endif
8153+
}

core/iwasm/common/wasm_runtime_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,9 @@ wasm_runtime_check_and_update_last_used_shared_heap(
14271427
uint8 **shared_heap_base_addr_adj_p, bool is_memory64);
14281428
#endif
14291429

1430+
struct WASMModuleInstanceExtraCommon *
1431+
GetModuleInstanceExtraCommon(struct WASMModuleInstance *module_inst);
1432+
14301433
#ifdef __cplusplus
14311434
}
14321435
#endif

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: 2 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

@@ -1540,19 +1533,13 @@ wasm_cluster_is_thread_terminated(WASMExecEnv *exec_env)
15401533
void
15411534
exception_lock(WASMModuleInstance *module_inst)
15421535
{
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);
1536+
os_mutex_lock(&GetModuleInstanceExtraCommon(module_inst)->exception_lock);
15501537
}
15511538

15521539
void
15531540
exception_unlock(WASMModuleInstance *module_inst)
15541541
{
1555-
os_mutex_unlock(&_exception_lock);
1542+
os_mutex_unlock(&GetModuleInstanceExtraCommon(module_inst)->exception_lock);
15561543
}
15571544

15581545
void

0 commit comments

Comments
 (0)