Skip to content

Commit

Permalink
Safe arithmetic ops
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexclique committed Aug 30, 2019
1 parent 6664f4b commit 0377429
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/task/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ fn create_blocking_thread() {
let _ = thread::Builder::new()
.name("async-blocking-driver-dynamic".to_string())
.spawn(move || {
let wait_limit = Duration::from_millis(1000 + rand_sleep_ms);
let wait_limit = Duration::from_millis(1000_u64 + rand_sleep_ms);

// Adjust the pool size counter before and after spawn
*POOL_SIZE.lock().unwrap() += 1;
Expand All @@ -276,7 +276,12 @@ fn create_blocking_thread() {
// Also, some systems have it(like macOS), and some don't(Linux).
// This case expected not to happen.
// But when happened this shouldn't throw a panic.
MAX_THREADS.store(*POOL_SIZE.lock().unwrap() - 1, Ordering::SeqCst);
let guarded_count = POOL_SIZE
.lock()
.unwrap()
.checked_sub(1)
.expect("shouldn't underflow");
MAX_THREADS.store(guarded_count, Ordering::SeqCst);
}
_ => eprintln!(
"cannot start a dynamic thread driving blocking tasks: {}",
Expand Down

0 comments on commit 0377429

Please sign in to comment.