Skip to content

Commit fbe5d1a

Browse files
committed
Use checked_add instead of manual overflow check
1 parent aa7fcec commit fbe5d1a

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
@@ -1308,14 +1308,13 @@ impl ThreadId {
13081308

13091309
// SAFETY: we have an exclusive lock on the counter.
13101310
unsafe {
1311-
if *COUNTER.get() == u64::MAX {
1312-
COUNTER_LOCKED.store(false, Ordering::Release);
1313-
exhausted()
1314-
} else {
1315-
let id = *COUNTER.get() + 1;
1311+
if let Some(id) = (*COUNTER.get()).checked_add(1) {
13161312
*COUNTER.get() = id;
13171313
COUNTER_LOCKED.store(false, Ordering::Release);
13181314
ThreadId(NonZero::new(id).unwrap())
1315+
} else {
1316+
COUNTER_LOCKED.store(false, Ordering::Release);
1317+
exhausted()
13191318
}
13201319
}
13211320
}

0 commit comments

Comments
 (0)