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