@@ -91,9 +91,8 @@ impl<T> DerefMut for CachePadded<T> {
9191}
9292
9393const SPIN_LIMIT : u32 = 6 ;
94- const YIELD_LIMIT : u32 = 10 ;
9594
96- /// Performs exponential backoff in spin loops.
95+ /// Performs quadratic backoff in spin loops.
9796pub struct Backoff {
9897 step : Cell < u32 > ,
9998}
@@ -104,25 +103,27 @@ impl Backoff {
104103 Backoff { step : Cell :: new ( 0 ) }
105104 }
106105
107- /// Backs off in a lock-free loop .
106+ /// Backs off using lightweight spinning .
108107 ///
109- /// This method should be used when we need to retry an operation because another thread made
110- /// progress.
108+ /// This method should be used for:
109+ /// - Retrying an operation because another thread made progress. i.e. on CAS failure.
110+ /// - Waiting for an operation to complete by spinning optimistically for a few iterations
111+ /// before falling back to parking the thread (see `Backoff::is_completed`).
111112 #[ inline]
112- pub fn spin ( & self ) {
113+ pub fn spin_light ( & self ) {
113114 let step = self . step . get ( ) . min ( SPIN_LIMIT ) ;
114115 for _ in 0 ..step. pow ( 2 ) {
115116 crate :: hint:: spin_loop ( ) ;
116117 }
117118
118- if self . step . get ( ) <= SPIN_LIMIT {
119- self . step . set ( self . step . get ( ) + 1 ) ;
120- }
119+ self . step . set ( self . step . get ( ) + 1 ) ;
121120 }
122121
123- /// Backs off in a blocking loop.
122+ /// Backs off using heavyweight spinning.
123+ ///
124+ /// This method should be used in blocking loops where parking the thread is not an option.
124125 #[ inline]
125- pub fn snooze ( & self ) {
126+ pub fn spin_heavy ( & self ) {
126127 if self . step . get ( ) <= SPIN_LIMIT {
127128 for _ in 0 ..self . step . get ( ) . pow ( 2 ) {
128129 crate :: hint:: spin_loop ( )
@@ -131,14 +132,12 @@ impl Backoff {
131132 crate :: thread:: yield_now ( ) ;
132133 }
133134
134- if self . step . get ( ) <= YIELD_LIMIT {
135- self . step . set ( self . step . get ( ) + 1 ) ;
136- }
135+ self . step . set ( self . step . get ( ) + 1 ) ;
137136 }
138137
139- /// Returns `true` if quadratic backoff has completed and blocking the thread is advised.
138+ /// Returns `true` if quadratic backoff has completed and parking the thread is advised.
140139 #[ inline]
141140 pub fn is_completed ( & self ) -> bool {
142- self . step . get ( ) > YIELD_LIMIT
141+ self . step . get ( ) > SPIN_LIMIT
143142 }
144143}
0 commit comments