@@ -71,32 +71,34 @@ struct minst_ecsact_system_impls {
71
71
72
72
auto trap_handler = ecsactsi_wasm_trap_handler{};
73
73
74
- auto all_minsts = std::vector<minst_ecsact_system_impls>{};
75
- auto next_available_minst_index = std::atomic_size_t {};
76
- thread_local auto thread_minst =
77
- std::optional<std::reference_wrapper<minst_ecsact_system_impls>>{};
74
+ auto all_minsts = std::vector<std::shared_ptr<minst_ecsact_system_impls>>{};
75
+ auto next_available_minst_index = std::atomic_size_t {};
78
76
79
- auto ensure_minst () -> minst_ecsact_system_impls& {
80
- if (!thread_minst) {
77
+ thread_local auto thread_minst = std::weak_ptr<minst_ecsact_system_impls>{};
78
+
79
+ auto ensure_minst () -> std::shared_ptr<minst_ecsact_system_impls> {
80
+ auto minst = thread_minst.lock ();
81
+ if (!minst) {
81
82
auto index = ++next_available_minst_index % all_minsts.size ();
82
- thread_minst = all_minsts[index ];
83
+ minst = all_minsts[index ];
84
+ thread_minst = minst;
83
85
}
84
86
85
- return thread_minst-> get () ;
87
+ return minst ;
86
88
}
87
89
88
90
void ecsact_si_wasm_system_impl (ecsact_system_execution_context* ctx) {
89
- auto & minst = ensure_minst ();
90
- auto system_id = ecsact_system_execution_context_id (ctx);
91
- auto itr = minst. sys_impl_exports .find (system_id);
92
- assert (itr != minst. sys_impl_exports .end ());
91
+ auto minst = ensure_minst ();
92
+ auto system_id = ecsact_system_execution_context_id (ctx);
93
+ auto itr = minst-> sys_impl_exports .find (system_id);
94
+ assert (itr != minst-> sys_impl_exports .end ());
93
95
94
96
auto mem_data = std::array<std::byte, 4096 >{};
95
97
set_call_mem_data (mem_data.data (), mem_data.size ());
96
98
defer {
97
99
set_call_mem_data (nullptr , 0 );
98
100
};
99
- call_mem_alloc (minst. memory .memory );
101
+ call_mem_alloc (minst-> memory .memory );
100
102
itr->second .func_call (call_mem_alloc (ctx));
101
103
}
102
104
@@ -261,11 +263,11 @@ ecsactsi_wasm_error ecsactsi_wasm_load(
261
263
return ECSACTSI_WASM_ERR_INITIALIZE_FAIL;
262
264
}
263
265
264
- all_minsts.emplace_back ( //
266
+ all_minsts.emplace_back (std::make_shared<minst_ecsact_system_impls>( //
265
267
std::move (inst),
266
268
system_impl_exports,
267
269
*wasm_mem
268
- );
270
+ )) ;
269
271
}
270
272
271
273
for (auto i = 0 ; systems_count > i; ++i) {
@@ -320,6 +322,8 @@ void ecsactsi_wasm_unload(
320
322
}
321
323
322
324
void ecsactsi_wasm_reset () {
325
+ all_minsts.clear ();
326
+ next_available_minst_index = 0 ;
323
327
}
324
328
325
329
void ecsactsi_wasm_consume_logs (
0 commit comments