8787use crate :: cell:: Cell ;
8888use crate :: fmt;
8989use crate :: marker;
90- use crate :: sync:: atomic:: { AtomicUsize , AtomicBool , Ordering } ;
90+ use crate :: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
9191use crate :: thread:: { self , Thread } ;
9292
9393/// A synchronization primitive which can be used to run a one-time global
@@ -149,7 +149,7 @@ pub struct OnceState {
149149#[ rustc_deprecated(
150150 since = "1.38.0" ,
151151 reason = "the `new` function is now preferred" ,
152- suggestion = "Once::new()" ,
152+ suggestion = "Once::new()"
153153) ]
154154pub const ONCE_INIT : Once = Once :: new ( ) ;
155155
@@ -185,15 +185,11 @@ struct WaiterQueue<'a> {
185185 set_state_on_drop_to : usize ,
186186}
187187
188-
189188impl Once {
190189 /// Creates a new `Once` value.
191190 #[ stable( feature = "once_new" , since = "1.2.0" ) ]
192191 pub const fn new ( ) -> Once {
193- Once {
194- state_and_queue : AtomicUsize :: new ( INCOMPLETE ) ,
195- _marker : marker:: PhantomData ,
196- }
192+ Once { state_and_queue : AtomicUsize :: new ( INCOMPLETE ) , _marker : marker:: PhantomData }
197193 }
198194
199195 /// Performs an initialization routine once and only once. The given closure
@@ -254,7 +250,10 @@ impl Once {
254250 ///
255251 /// [poison]: struct.Mutex.html#poisoning
256252 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
257- pub fn call_once < F > ( & self , f : F ) where F : FnOnce ( ) {
253+ pub fn call_once < F > ( & self , f : F )
254+ where
255+ F : FnOnce ( ) ,
256+ {
258257 // Fast path check
259258 if self . is_completed ( ) {
260259 return ;
@@ -311,16 +310,17 @@ impl Once {
311310 /// INIT.call_once(|| {});
312311 /// ```
313312 #[ unstable( feature = "once_poison" , issue = "33577" ) ]
314- pub fn call_once_force < F > ( & self , f : F ) where F : FnOnce ( & OnceState ) {
313+ pub fn call_once_force < F > ( & self , f : F )
314+ where
315+ F : FnOnce ( & OnceState ) ,
316+ {
315317 // Fast path check
316318 if self . is_completed ( ) {
317319 return ;
318320 }
319321
320322 let mut f = Some ( f) ;
321- self . call_inner ( true , & mut |p| {
322- f. take ( ) . unwrap ( ) ( & OnceState { poisoned : p } )
323- } ) ;
323+ self . call_inner ( true , & mut |p| f. take ( ) . unwrap ( ) ( & OnceState { poisoned : p } ) ) ;
324324 }
325325
326326 /// Returns `true` if some `call_once` call has completed
@@ -385,10 +385,7 @@ impl Once {
385385 // currently no way to take an `FnOnce` and call it via virtual dispatch
386386 // without some allocation overhead.
387387 #[ cold]
388- fn call_inner ( & self ,
389- ignore_poisoning : bool ,
390- init : & mut dyn FnMut ( bool ) )
391- {
388+ fn call_inner ( & self , ignore_poisoning : bool , init : & mut dyn FnMut ( bool ) ) {
392389 let mut state_and_queue = self . state_and_queue . load ( Ordering :: Acquire ) ;
393390 loop {
394391 match state_and_queue {
@@ -397,15 +394,16 @@ impl Once {
397394 // Panic to propagate the poison.
398395 panic ! ( "Once instance has previously been poisoned" ) ;
399396 }
400- POISONED |
401- INCOMPLETE => {
397+ POISONED | INCOMPLETE => {
402398 // Try to register this thread as the one RUNNING.
403- let old = self . state_and_queue . compare_and_swap ( state_and_queue,
404- RUNNING ,
405- Ordering :: Acquire ) ;
399+ let old = self . state_and_queue . compare_and_swap (
400+ state_and_queue,
401+ RUNNING ,
402+ Ordering :: Acquire ,
403+ ) ;
406404 if old != state_and_queue {
407405 state_and_queue = old;
408- continue
406+ continue ;
409407 }
410408 // `waiter_queue` will manage other waiting threads, and
411409 // wake them up on drop.
@@ -417,7 +415,7 @@ impl Once {
417415 // poisoned or not.
418416 init ( state_and_queue == POISONED ) ;
419417 waiter_queue. set_state_on_drop_to = COMPLETE ;
420- break
418+ break ;
421419 }
422420 _ => {
423421 // All other values must be RUNNING with possibly a
@@ -451,9 +449,7 @@ fn wait(state_and_queue: &AtomicUsize, mut current_state: usize) {
451449
452450 // Try to slide in the node at the head of the linked list, making sure
453451 // that another thread didn't just replace the head of the linked list.
454- let old = state_and_queue. compare_and_swap ( current_state,
455- me | RUNNING ,
456- Ordering :: Release ) ;
452+ let old = state_and_queue. compare_and_swap ( current_state, me | RUNNING , Ordering :: Release ) ;
457453 if old != current_state {
458454 current_state = old;
459455 continue ;
@@ -485,8 +481,8 @@ impl fmt::Debug for Once {
485481impl Drop for WaiterQueue < ' _ > {
486482 fn drop ( & mut self ) {
487483 // Swap out our state with however we finished.
488- let state_and_queue = self . state_and_queue . swap ( self . set_state_on_drop_to ,
489- Ordering :: AcqRel ) ;
484+ let state_and_queue =
485+ self . state_and_queue . swap ( self . set_state_on_drop_to , Ordering :: AcqRel ) ;
490486
491487 // We should only ever see an old state which was RUNNING.
492488 assert_eq ! ( state_and_queue & STATE_MASK , RUNNING ) ;
@@ -562,10 +558,10 @@ impl OnceState {
562558
563559#[ cfg( all( test, not( target_os = "emscripten" ) ) ) ]
564560mod tests {
561+ use super :: Once ;
565562 use crate :: panic;
566563 use crate :: sync:: mpsc:: channel;
567564 use crate :: thread;
568- use super :: Once ;
569565
570566 #[ test]
571567 fn smoke_once ( ) {
@@ -585,8 +581,10 @@ mod tests {
585581 let ( tx, rx) = channel ( ) ;
586582 for _ in 0 ..10 {
587583 let tx = tx. clone ( ) ;
588- thread:: spawn ( move || {
589- for _ in 0 ..4 { thread:: yield_now ( ) }
584+ thread:: spawn ( move || {
585+ for _ in 0 ..4 {
586+ thread:: yield_now ( )
587+ }
590588 unsafe {
591589 O . call_once ( || {
592590 assert ! ( !RUN ) ;
@@ -675,6 +673,5 @@ mod tests {
675673
676674 assert ! ( t1. join( ) . is_ok( ) ) ;
677675 assert ! ( t2. join( ) . is_ok( ) ) ;
678-
679676 }
680677}
0 commit comments