Skip to content

Commit

Permalink
simplify the implement of mark_sleep and mark_woken
Browse files Browse the repository at this point in the history
  • Loading branch information
glorv committed Aug 16, 2022
1 parent 2f5f6e4 commit dd99e60
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/pool/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,38 +103,22 @@ impl<T> QueueCore<T> {
///
/// It can be marked as sleep only when the pool is not shutting down.
pub fn mark_sleep(&self) -> bool {
let mut cnt = self.active_workers.load(Ordering::SeqCst);
loop {
if is_shutdown(cnt) {
return false;
}

match self.active_workers.compare_exchange_weak(
cnt,
cnt - WORKER_COUNT_BASE,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(_) => return true,
Err(n) => cnt = n,
}
let shutdown = is_shutdown(
self.active_workers
.fetch_sub(WORKER_COUNT_BASE, Ordering::SeqCst),
);
if shutdown {
// keep the right number here though it is not used anymore
self.active_workers
.fetch_add(WORKER_COUNT_BASE, Ordering::SeqCst);
}
!shutdown
}

/// Marks current thread as woken up states.
pub fn mark_woken(&self) {
let mut cnt = self.active_workers.load(Ordering::SeqCst);
loop {
match self.active_workers.compare_exchange_weak(
cnt,
cnt + WORKER_COUNT_BASE,
Ordering::SeqCst,
Ordering::SeqCst,
) {
Ok(_) => return,
Err(n) => cnt = n,
}
}
self.active_workers
.fetch_add(WORKER_COUNT_BASE, Ordering::SeqCst);
}

/// Scale workers.
Expand Down

0 comments on commit dd99e60

Please sign in to comment.