@@ -23,21 +23,27 @@ use super::{
23
23
pub ( crate ) fn load_hrtf_processor ( sample_rate : u32 ) -> ( HrtfProcessor , usize ) {
24
24
static INSTANCE : OnceLock < Mutex < HashMap < u32 , ( HrtfProcessor , usize ) > > > = OnceLock :: new ( ) ;
25
25
let cache = INSTANCE . get_or_init ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
26
- let mut guard = cache. lock ( ) . unwrap ( ) ;
27
- guard
28
- . entry ( sample_rate)
29
- . or_insert_with ( || {
30
- let resource = include_bytes ! ( "../../resources/IRC_1003_C.bin" ) ;
31
- let hrir_sphere = HrirSphere :: new ( & resource[ ..] , sample_rate) . unwrap ( ) ;
32
- let len = hrir_sphere. len ( ) ;
33
-
34
- let interpolation_steps = 1 ; // TODO?
35
- let samples_per_step = RENDER_QUANTUM_SIZE / interpolation_steps;
36
- let processor = HrtfProcessor :: new ( hrir_sphere, interpolation_steps, samples_per_step) ;
37
-
38
- ( processor, len)
39
- } )
40
- . clone ( )
26
+
27
+ // To avoid poisening the cache mutex, don't use the `entry()` API on HashMap
28
+ {
29
+ if let Some ( value) = cache. lock ( ) . unwrap ( ) . get ( & sample_rate) {
30
+ return value. clone ( ) ;
31
+ }
32
+ }
33
+
34
+ // The following snippet might panic
35
+ let resource = include_bytes ! ( "../../resources/IRC_1003_C.bin" ) ;
36
+ let hrir_sphere = HrirSphere :: new ( & resource[ ..] , sample_rate) . unwrap ( ) ;
37
+ let len = hrir_sphere. len ( ) ;
38
+
39
+ let interpolation_steps = 1 ; // TODO?
40
+ let samples_per_step = RENDER_QUANTUM_SIZE / interpolation_steps;
41
+ let processor = HrtfProcessor :: new ( hrir_sphere, interpolation_steps, samples_per_step) ;
42
+
43
+ let value = ( processor, len) ;
44
+ cache. lock ( ) . unwrap ( ) . insert ( sample_rate, value. clone ( ) ) ;
45
+
46
+ value
41
47
}
42
48
43
49
/// Spatialization algorithm used to position the audio in 3D space
0 commit comments