@@ -67,6 +67,71 @@ 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
+ #[ inline]
80
+ pub fn get_mut( & mut self ) -> & mut T {
81
+ self . 0 . get_mut( )
82
+ }
83
+ }
84
+
85
+ impl <T : Copy > AtomicCell <T > {
86
+ pub fn into_inner( self ) -> T {
87
+ self . 0 . into_inner( )
88
+ }
89
+
90
+ #[ inline]
91
+ pub fn load( & self ) -> T {
92
+ self . 0 . get( )
93
+ }
94
+
95
+ #[ inline]
96
+ pub fn store( & self , val: T ) {
97
+ self . 0 . set( val)
98
+ }
99
+
100
+ pub fn swap( & self , val: T ) -> T {
101
+ self . 0 . replace( val)
102
+ }
103
+ }
104
+
105
+ impl <T : Copy + PartialEq > AtomicCell <T > {
106
+ pub fn compare_and_swap( & self , current: T , new: T ) -> T {
107
+ match self . compare_exchange( current, new) {
108
+ Ok ( v) => v,
109
+ Err ( v) => v,
110
+ }
111
+ }
112
+
113
+ pub fn compare_exchange( & self ,
114
+ current: T ,
115
+ new: T )
116
+ -> Result <T , T > {
117
+ let read = self . 0 . get( ) ;
118
+ if read == current {
119
+ self . 0 . set( new) ;
120
+ Ok ( read)
121
+ } else {
122
+ Err ( read)
123
+ }
124
+ }
125
+ }
126
+
127
+ impl <T : Add <Output =T > + Copy > AtomicCell <T > {
128
+ pub fn fetch_add( & self , val: T ) -> T {
129
+ let old = self . 0 . get( ) ;
130
+ self . 0 . set( old + val) ;
131
+ old
132
+ }
133
+ }
134
+
70
135
#[ derive( Debug ) ]
71
136
pub struct Atomic <T : Copy >( Cell <T >) ;
72
137
@@ -77,7 +142,7 @@ cfg_if! {
77
142
}
78
143
}
79
144
80
- impl <T : Copy + PartialEq > Atomic <T > {
145
+ impl <T : Copy > Atomic <T > {
81
146
pub fn into_inner( self ) -> T {
82
147
self . 0 . into_inner( )
83
148
}
@@ -95,7 +160,9 @@ cfg_if! {
95
160
pub fn swap( & self , val: T , _: Ordering ) -> T {
96
161
self . 0 . replace( val)
97
162
}
163
+ }
98
164
165
+ impl <T : Copy + PartialEq > Atomic <T > {
99
166
pub fn compare_exchange( & self ,
100
167
current: T ,
101
168
new: T ,
@@ -271,6 +338,8 @@ cfg_if! {
271
338
272
339
pub use std:: sync:: atomic:: { AtomicBool , AtomicUsize , AtomicU32 , AtomicU64 } ;
273
340
341
+ pub use crossbeam_utils:: atomic:: AtomicCell ;
342
+
274
343
pub use std:: sync:: Arc as Lrc ;
275
344
pub use std:: sync:: Weak as Weak ;
276
345
0 commit comments