@@ -67,6 +67,59 @@ cfg_if! {
6767 use std:: ops:: Add ;
6868 use std:: panic:: { resume_unwind, catch_unwind, AssertUnwindSafe } ;
6969
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+
70123 #[ derive( Debug ) ]
71124 pub struct Atomic <T : Copy >( Cell <T >) ;
72125
@@ -77,7 +130,7 @@ cfg_if! {
77130 }
78131 }
79132
80- impl <T : Copy + PartialEq > Atomic <T > {
133+ impl <T : Copy > Atomic <T > {
81134 pub fn into_inner( self ) -> T {
82135 self . 0 . into_inner( )
83136 }
@@ -95,7 +148,9 @@ cfg_if! {
95148 pub fn swap( & self , val: T , _: Ordering ) -> T {
96149 self . 0 . replace( val)
97150 }
151+ }
98152
153+ impl <T : Copy + PartialEq > Atomic <T > {
99154 pub fn compare_exchange( & self ,
100155 current: T ,
101156 new: T ,
@@ -271,6 +326,8 @@ cfg_if! {
271326
272327 pub use std:: sync:: atomic:: { AtomicBool , AtomicUsize , AtomicU32 , AtomicU64 } ;
273328
329+ pub use crossbeam_utils:: atomic:: AtomicCell ;
330+
274331 pub use std:: sync:: Arc as Lrc ;
275332 pub use std:: sync:: Weak as Weak ;
276333
0 commit comments