@@ -25,7 +25,7 @@ pub enum Singleton {
2525 Production ( Container ! [ Send + Sync ] ) ,
2626
2727 #[ cfg( debug_assertions) ]
28- Testing ( parking_lot:: Mutex < std:: collections:: HashMap < String , Container ! [ Send + Sync ] > > ) ,
28+ Testing ( parking_lot:: RwLock < std:: collections:: HashMap < String , Container ! [ Send + Sync ] > > ) ,
2929}
3030
3131unsafe impl Send for Singleton { }
@@ -45,7 +45,7 @@ impl Singleton {
4545 Some ( name) => name,
4646 None => panic ! ( "thread doesn't have name" ) ,
4747 } ;
48- let guard = c. lock ( ) ;
48+ let guard = c. read_recursive ( ) ;
4949 let v: & T = guard
5050 . get ( thread_name)
5151 . unwrap_or_else ( || panic ! ( "thread {thread_name} is not initiated" ) )
@@ -65,9 +65,15 @@ impl Singleton {
6565 Some ( name) => name,
6666 None => panic ! ( "thread doesn't have name" ) ,
6767 } ;
68- let mut guard = c. lock ( ) ;
69- let c = guard. entry ( thread_name. to_string ( ) ) . or_default ( ) ;
70- c. set ( value)
68+ let guard = c. upgradable_read ( ) ;
69+ match guard. get ( thread_name) {
70+ Some ( c) => c. set ( value) ,
71+ None => {
72+ let mut guard = parking_lot:: RwLockUpgradableReadGuard :: upgrade ( guard) ;
73+ let c = guard. entry ( thread_name. to_string ( ) ) . or_default ( ) ;
74+ c. set ( value)
75+ }
76+ }
7177 }
7278 }
7379 }
@@ -104,28 +110,21 @@ impl GlobalInstance {
104110 /// Should only be initiated once and only used in testing.
105111 #[ cfg( debug_assertions) ]
106112 pub fn init_testing ( ) {
107- let _ = GLOBAL . set ( Singleton :: Testing ( parking_lot:: Mutex :: default ( ) ) ) ;
113+ let _ = GLOBAL . set ( Singleton :: Testing ( parking_lot:: RwLock :: default ( ) ) ) ;
108114 }
109115
110116 /// drop testing global data by thread name.
111117 ///
112118 /// Should only be used in testing code.
113119 #[ cfg( debug_assertions) ]
114120 pub fn drop_testing ( thread_name : & str ) {
115- use std:: thread;
116-
117121 match GLOBAL . wait ( ) {
118122 Singleton :: Production ( _) => {
119123 unreachable ! ( "drop_testing should never be called on production global" )
120124 }
121125 Singleton :: Testing ( c) => {
122- // let v = {
123- // let mut guard = c.lock();
124- // guard.remove(thread_name)
125- // };
126- // // We don't care about if about this container any more, just
127- // // move to another thread to make sure this call returned ASAP.
128- // thread::spawn(move || v);
126+ let mut guard = c. write ( ) ;
127+ guard. remove ( thread_name) ;
129128 }
130129 }
131130 }
0 commit comments