@@ -130,18 +130,18 @@ impl BlockingThreadpool {
130
130
T : Send + ' static ,
131
131
E : fmt:: Debug + Send + InternalError + ' static ,
132
132
{
133
- self . spawned_tasks . fetch_add ( 1 , Ordering :: SeqCst ) ;
133
+ self . spawned_tasks . fetch_add ( 1 , Ordering :: Relaxed ) ;
134
134
// Ensure the counter's always decremented (whether the task completed,
135
135
// was cancelled or panicked)
136
136
scopeguard:: defer! {
137
- self . spawned_tasks. fetch_sub( 1 , Ordering :: SeqCst ) ;
137
+ self . spawned_tasks. fetch_sub( 1 , Ordering :: Relaxed ) ;
138
138
}
139
139
140
140
let active_threads = Arc :: clone ( & self . active_threads ) ;
141
141
let f_with_metrics = move || {
142
- active_threads. fetch_add ( 1 , Ordering :: SeqCst ) ;
142
+ active_threads. fetch_add ( 1 , Ordering :: Relaxed ) ;
143
143
scopeguard:: defer! {
144
- active_threads. fetch_sub( 1 , Ordering :: SeqCst ) ;
144
+ active_threads. fetch_sub( 1 , Ordering :: Relaxed ) ;
145
145
}
146
146
f ( )
147
147
} ;
@@ -154,11 +154,14 @@ impl BlockingThreadpool {
154
154
155
155
/// Return the pool's current metrics
156
156
pub fn metrics ( & self ) -> BlockingThreadpoolMetrics {
157
- // active_threads is decremented on a separate thread so we need a
158
- // strong Ordering to ensure it's in sync w/ spawned_tasks (otherwise
159
- // it could underflow queued_tasks)
160
- let spawned_tasks = self . spawned_tasks . load ( Ordering :: SeqCst ) ;
161
- let active_threads = self . active_threads . load ( Ordering :: SeqCst ) ;
157
+ let spawned_tasks = self . spawned_tasks . load ( Ordering :: Relaxed ) ;
158
+ // active_threads is decremented on a separate thread so there's no
159
+ // Drop order guarantee of spawned_tasks decrementing before it does:
160
+ // catch the case where active_threads is larger
161
+ let active_threads = self
162
+ . active_threads
163
+ . load ( Ordering :: Relaxed )
164
+ . min ( spawned_tasks) ;
162
165
BlockingThreadpoolMetrics {
163
166
queued_tasks : spawned_tasks - active_threads,
164
167
active_threads,
0 commit comments