11//! A simple atomic counter that uses mutexes if the platform doesn't support atomic u64s.
22
3- #[ cfg( target_has_atomic = "64" ) ]
4- use core:: sync:: atomic:: { AtomicU64 , Ordering } ;
53#[ cfg( not( target_has_atomic = "64" ) ) ]
64use crate :: sync:: Mutex ;
5+ #[ cfg( target_has_atomic = "64" ) ]
6+ use core:: sync:: atomic:: { AtomicU64 , Ordering } ;
77
88pub ( crate ) struct AtomicCounter {
99 #[ cfg( target_has_atomic = "64" ) ]
@@ -22,23 +22,27 @@ impl AtomicCounter {
2222 }
2323 }
2424 pub ( crate ) fn next ( & self ) -> u64 {
25- #[ cfg( target_has_atomic = "64" ) ] {
25+ #[ cfg( target_has_atomic = "64" ) ]
26+ {
2627 self . counter . fetch_add ( 1 , Ordering :: AcqRel )
2728 }
28- #[ cfg( not( target_has_atomic = "64" ) ) ] {
29+ #[ cfg( not( target_has_atomic = "64" ) ) ]
30+ {
2931 let mut mtx = self . counter . lock ( ) . unwrap ( ) ;
3032 * mtx += 1 ;
3133 * mtx - 1
3234 }
3335 }
3436 #[ cfg( test) ]
3537 pub ( crate ) fn set_counter ( & self , count : u64 ) {
36- #[ cfg( target_has_atomic = "64" ) ] {
38+ #[ cfg( target_has_atomic = "64" ) ]
39+ {
3740 self . counter . store ( count, Ordering :: Release ) ;
3841 }
39- #[ cfg( not( target_has_atomic = "64" ) ) ] {
42+ #[ cfg( not( target_has_atomic = "64" ) ) ]
43+ {
4044 let mut mtx = self . counter . lock ( ) . unwrap ( ) ;
41- * mtx = count;
45+ * mtx = count;
4246 }
4347 }
4448}
0 commit comments