1
1
//! A simple atomic counter that uses mutexes if the platform doesn't support atomic u64s.
2
2
3
- #[ cfg( target_has_atomic = "64" ) ]
4
- use core:: sync:: atomic:: { AtomicU64 , Ordering } ;
5
3
#[ cfg( not( target_has_atomic = "64" ) ) ]
6
4
use crate :: sync:: Mutex ;
5
+ #[ cfg( target_has_atomic = "64" ) ]
6
+ use core:: sync:: atomic:: { AtomicU64 , Ordering } ;
7
7
8
8
pub ( crate ) struct AtomicCounter {
9
9
#[ cfg( target_has_atomic = "64" ) ]
@@ -22,23 +22,27 @@ impl AtomicCounter {
22
22
}
23
23
}
24
24
pub ( crate ) fn next ( & self ) -> u64 {
25
- #[ cfg( target_has_atomic = "64" ) ] {
25
+ #[ cfg( target_has_atomic = "64" ) ]
26
+ {
26
27
self . counter . fetch_add ( 1 , Ordering :: AcqRel )
27
28
}
28
- #[ cfg( not( target_has_atomic = "64" ) ) ] {
29
+ #[ cfg( not( target_has_atomic = "64" ) ) ]
30
+ {
29
31
let mut mtx = self . counter . lock ( ) . unwrap ( ) ;
30
32
* mtx += 1 ;
31
33
* mtx - 1
32
34
}
33
35
}
34
36
#[ cfg( test) ]
35
37
pub ( crate ) fn set_counter ( & self , count : u64 ) {
36
- #[ cfg( target_has_atomic = "64" ) ] {
38
+ #[ cfg( target_has_atomic = "64" ) ]
39
+ {
37
40
self . counter . store ( count, Ordering :: Release ) ;
38
41
}
39
- #[ cfg( not( target_has_atomic = "64" ) ) ] {
42
+ #[ cfg( not( target_has_atomic = "64" ) ) ]
43
+ {
40
44
let mut mtx = self . counter . lock ( ) . unwrap ( ) ;
41
- * mtx = count;
45
+ * mtx = count;
42
46
}
43
47
}
44
48
}
0 commit comments