Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify the implement of mark_sleep and mark_woken #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, the active count should be not important once the pool is shut down.

@BusyJay Is there any special reason to check shutdown first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Combining a loop and compare_exchange_weak just to be more performant when there is something to check.

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