@@ -71,32 +71,34 @@ struct minst_ecsact_system_impls {
7171
7272auto trap_handler = ecsactsi_wasm_trap_handler{};
7373
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 {};
7876
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) {
8182 auto index = ++next_available_minst_index % all_minsts.size ();
82- thread_minst = all_minsts[index];
83+ minst = all_minsts[index];
84+ thread_minst = minst;
8385 }
8486
85- return thread_minst-> get () ;
87+ return minst ;
8688}
8789
8890void 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 ());
9395
9496 auto mem_data = std::array<std::byte, 4096 >{};
9597 set_call_mem_data (mem_data.data (), mem_data.size ());
9698 defer {
9799 set_call_mem_data (nullptr , 0 );
98100 };
99- call_mem_alloc (minst. memory .memory );
101+ call_mem_alloc (minst-> memory .memory );
100102 itr->second .func_call (call_mem_alloc (ctx));
101103}
102104
@@ -261,11 +263,11 @@ ecsactsi_wasm_error ecsactsi_wasm_load(
261263 return ECSACTSI_WASM_ERR_INITIALIZE_FAIL;
262264 }
263265
264- all_minsts.emplace_back ( //
266+ all_minsts.emplace_back (std::make_shared<minst_ecsact_system_impls>( //
265267 std::move (inst),
266268 system_impl_exports,
267269 *wasm_mem
268- );
270+ )) ;
269271 }
270272
271273 for (auto i = 0 ; systems_count > i; ++i) {
@@ -320,6 +322,8 @@ void ecsactsi_wasm_unload(
320322}
321323
322324void ecsactsi_wasm_reset () {
325+ all_minsts.clear ();
326+ next_available_minst_index = 0 ;
323327}
324328
325329void ecsactsi_wasm_consume_logs (
0 commit comments