@@ -30,6 +30,11 @@ typedef struct AtomicWaitNode {
3030 korp_cond wait_cond ;
3131} AtomicWaitNode ;
3232
33+ typedef struct AtomicWaitAddressArgs {
34+ uint32 index ;
35+ void * * addr ;
36+ } AtomicWaitAddressArgs ;
37+
3338/* Atomic wait map */
3439static HashMap * wait_map ;
3540
@@ -87,6 +92,62 @@ search_module(WASMModuleCommon *module)
8792 return NULL ;
8893}
8994
95+ static void
96+ wait_map_address_count_callback (void * key , void * value ,
97+ void * p_total_elem_count )
98+ {
99+ * (uint32 * )p_total_elem_count = * (uint32 * )p_total_elem_count + 1 ;
100+ }
101+
102+ static void
103+ create_list_of_waiter_addresses (void * key , void * value , void * user_data )
104+ {
105+ AtomicWaitAddressArgs * data = (AtomicWaitAddressArgs * )user_data ;
106+ data -> addr [data -> index ++ ] = key ;
107+ }
108+
109+ void
110+ notify_stale_threads_on_exception (WASMModuleInstanceCommon * module_inst )
111+ {
112+ AtomicWaitAddressArgs * args = NULL ;
113+ uint32 i = 0 , total_elem_count = 0 ;
114+ if (!(args = wasm_runtime_malloc (sizeof (AtomicWaitAddressArgs )))) {
115+ LOG_ERROR ("failed to create atomic wait address args node" );
116+ return ;
117+ }
118+
119+ os_mutex_lock (& shared_memory_list_lock );
120+
121+ /* count number of addresses in wait_map */
122+ bh_hash_map_traverse (wait_map , wait_map_address_count_callback ,
123+ (void * )& total_elem_count );
124+
125+ /* allocate memory */
126+ uint64 total_size = offsetof(AtomicWaitAddressArgs , addr )
127+ + sizeof (void * ) * total_elem_count ;
128+
129+ if (!(args -> addr = wasm_runtime_malloc ((uint32 )total_size ))) {
130+ LOG_ERROR ("failed to allocate memory for list of atomic wait addresses "
131+ "in args node" );
132+ return ;
133+ }
134+ memset (args -> addr , 0 , total_size );
135+
136+ /* set values in list of addresses */
137+ bh_hash_map_traverse (wait_map , create_list_of_waiter_addresses , args );
138+ os_mutex_unlock (& shared_memory_list_lock );
139+
140+ /* notify */
141+ for (i = 0 ; i < args -> index ; i ++ ) {
142+ wasm_runtime_atomic_notify (module_inst , args -> addr [i ], UINT32_MAX );
143+ }
144+
145+ /* free memory allocated to args data */
146+ if (args -> addr )
147+ wasm_runtime_free (args -> addr );
148+ wasm_runtime_free (args );
149+ }
150+
90151WASMSharedMemNode *
91152wasm_module_get_shared_memory (WASMModuleCommon * module )
92153{
0 commit comments