@@ -5,6 +5,7 @@ use std::future::Future;
55use std:: sync:: Arc ;
66
77use async_lock:: Mutex ;
8+ use async_lock:: MutexGuard ;
89use vortex_dtype:: DType ;
910use vortex_error:: VortexResult ;
1011
@@ -36,11 +37,24 @@ impl SharedArray {
3637 }
3738 }
3839
40+ #[ cfg( not( target_family = "wasm" ) ) ]
41+ fn lock_sync ( & self ) -> MutexGuard < ' _ , SharedState > {
42+ self . state . lock_blocking ( )
43+ }
44+
45+ #[ cfg( target_family = "wasm" ) ]
46+ fn lock_sync ( & self ) -> MutexGuard < ' _ , SharedState > {
47+ // this should mirror how parking_lot compiles to wasm
48+ self . state
49+ . try_lock ( )
50+ . expect ( "SharedArray: mutex contention on single-threaded wasm target" )
51+ }
52+
3953 pub fn get_or_compute (
4054 & self ,
4155 f : impl FnOnce ( & ArrayRef ) -> VortexResult < Canonical > ,
4256 ) -> VortexResult < Canonical > {
43- let mut state = self . state . lock_blocking ( ) ;
57+ let mut state = self . lock_sync ( ) ;
4458 match & * state {
4559 SharedState :: Cached ( canonical) => Ok ( canonical. clone ( ) ) ,
4660 SharedState :: Source ( source) => {
@@ -69,7 +83,7 @@ impl SharedArray {
6983 }
7084
7185 pub ( super ) fn current_array_ref ( & self ) -> ArrayRef {
72- let state = self . state . lock_blocking ( ) ;
86+ let state = self . lock_sync ( ) ;
7387 match & * state {
7488 SharedState :: Source ( source) => source. clone ( ) ,
7589 SharedState :: Cached ( canonical) => canonical. clone ( ) . into_array ( ) ,
@@ -78,6 +92,6 @@ impl SharedArray {
7892
7993 pub ( super ) fn set_source ( & mut self , source : ArrayRef ) {
8094 self . dtype = source. dtype ( ) . clone ( ) ;
81- * self . state . lock_blocking ( ) = SharedState :: Source ( source) ;
95+ * self . lock_sync ( ) = SharedState :: Source ( source) ;
8296 }
8397}
0 commit comments