@@ -67,6 +67,59 @@ cfg_if! {
67
67
use std:: ops:: Add ;
68
68
use std:: panic:: { resume_unwind, catch_unwind, AssertUnwindSafe } ;
69
69
70
+ #[ derive( Debug ) ]
71
+ pub struct AtomicCell <T : Copy >( Cell <T >) ;
72
+
73
+ impl <T : Copy > AtomicCell <T > {
74
+ #[ inline]
75
+ pub fn new( v: T ) -> Self {
76
+ AtomicCell ( Cell :: new( v) )
77
+ }
78
+ }
79
+
80
+ impl <T : Copy > AtomicCell <T > {
81
+ pub fn into_inner( self ) -> T {
82
+ self . 0 . into_inner( )
83
+ }
84
+
85
+ #[ inline]
86
+ pub fn load( & self ) -> T {
87
+ self . 0 . get( )
88
+ }
89
+
90
+ #[ inline]
91
+ pub fn store( & self , val: T ) {
92
+ self . 0 . set( val)
93
+ }
94
+
95
+ pub fn swap( & self , val: T ) -> T {
96
+ self . 0 . replace( val)
97
+ }
98
+ }
99
+
100
+ impl <T : Copy + PartialEq > AtomicCell <T > {
101
+ pub fn compare_exchange( & self ,
102
+ current: T ,
103
+ new: T )
104
+ -> Result <T , T > {
105
+ let read = self . 0 . get( ) ;
106
+ if read == current {
107
+ self . 0 . set( new) ;
108
+ Ok ( read)
109
+ } else {
110
+ Err ( read)
111
+ }
112
+ }
113
+ }
114
+
115
+ impl <T : Add <Output =T > + Copy > AtomicCell <T > {
116
+ pub fn fetch_add( & self , val: T ) -> T {
117
+ let old = self . 0 . get( ) ;
118
+ self . 0 . set( old + val) ;
119
+ old
120
+ }
121
+ }
122
+
70
123
#[ derive( Debug ) ]
71
124
pub struct Atomic <T : Copy >( Cell <T >) ;
72
125
@@ -77,7 +130,7 @@ cfg_if! {
77
130
}
78
131
}
79
132
80
- impl <T : Copy + PartialEq > Atomic <T > {
133
+ impl <T : Copy > Atomic <T > {
81
134
pub fn into_inner( self ) -> T {
82
135
self . 0 . into_inner( )
83
136
}
@@ -95,7 +148,9 @@ cfg_if! {
95
148
pub fn swap( & self , val: T , _: Ordering ) -> T {
96
149
self . 0 . replace( val)
97
150
}
151
+ }
98
152
153
+ impl <T : Copy + PartialEq > Atomic <T > {
99
154
pub fn compare_exchange( & self ,
100
155
current: T ,
101
156
new: T ,
@@ -271,6 +326,8 @@ cfg_if! {
271
326
272
327
pub use std:: sync:: atomic:: { AtomicBool , AtomicUsize , AtomicU32 , AtomicU64 } ;
273
328
329
+ pub use crossbeam_utils:: atomic:: AtomicCell ;
330
+
274
331
pub use std:: sync:: Arc as Lrc ;
275
332
pub use std:: sync:: Weak as Weak ;
276
333
0 commit comments