Skip to content

Commit eed3e9e

Browse files
committed
simplify the implement of mark_sleep and mark_woken
Signed-off-by: glorv <[email protected]>
1 parent 2f5f6e4 commit eed3e9e

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

src/pool/spawn.rs

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,38 +103,22 @@ impl<T> QueueCore<T> {
103103
///
104104
/// It can be marked as sleep only when the pool is not shutting down.
105105
pub fn mark_sleep(&self) -> bool {
106-
let mut cnt = self.active_workers.load(Ordering::SeqCst);
107-
loop {
108-
if is_shutdown(cnt) {
109-
return false;
110-
}
111-
112-
match self.active_workers.compare_exchange_weak(
113-
cnt,
114-
cnt - WORKER_COUNT_BASE,
115-
Ordering::SeqCst,
116-
Ordering::SeqCst,
117-
) {
118-
Ok(_) => return true,
119-
Err(n) => cnt = n,
120-
}
106+
let shutdown = is_shutdown(
107+
self.active_workers
108+
.fetch_sub(WORKER_COUNT_BASE, Ordering::SeqCst),
109+
);
110+
if shutdown {
111+
// keep the right number here though it is not used anymore
112+
self.active_workers
113+
.fetch_add(WORKER_COUNT_BASE, Ordering::SeqCst);
121114
}
115+
!shutdown
122116
}
123117

124118
/// Marks current thread as woken up states.
125119
pub fn mark_woken(&self) {
126-
let mut cnt = self.active_workers.load(Ordering::SeqCst);
127-
loop {
128-
match self.active_workers.compare_exchange_weak(
129-
cnt,
130-
cnt + WORKER_COUNT_BASE,
131-
Ordering::SeqCst,
132-
Ordering::SeqCst,
133-
) {
134-
Ok(_) => return,
135-
Err(n) => cnt = n,
136-
}
137-
}
120+
self.active_workers
121+
.fetch_add(WORKER_COUNT_BASE, Ordering::SeqCst);
138122
}
139123

140124
/// Scale workers.

0 commit comments

Comments
 (0)