Skip to content

Commit 798a7e5

Browse files
committed
Use checked_add instead of manual overflow check
1 parent 642e960 commit 798a7e5

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

library/std/src/thread/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,14 +1305,13 @@ impl ThreadId {
13051305

13061306
// SAFETY: we have an exclusive lock on the counter.
13071307
unsafe {
1308-
if *COUNTER.get() == u64::MAX {
1309-
COUNTER_LOCKED.store(false, Ordering::Release);
1310-
exhausted()
1311-
} else {
1312-
let id = *COUNTER.get() + 1;
1308+
if let Some(id) = (*COUNTER.get()).checked_add(1) {
13131309
*COUNTER.get() = id;
13141310
COUNTER_LOCKED.store(false, Ordering::Release);
13151311
ThreadId(NonZero::new(id).unwrap())
1312+
} else {
1313+
COUNTER_LOCKED.store(false, Ordering::Release);
1314+
exhausted()
13161315
}
13171316
}
13181317
}

0 commit comments

Comments
 (0)