clean up new() logic

This commit is contained in:
Joe Ardent 2023-08-05 12:16:23 -07:00
parent 7067721f72
commit 50a59e1898
1 changed files with 11 additions and 15 deletions

View File

@ -54,24 +54,20 @@ impl Julid {
.as_millis() as u64; .as_millis() as u64;
let last = LAST_MSB.load(Ordering::SeqCst); let last = LAST_MSB.load(Ordering::SeqCst);
let ots = last >> COUNTER_BITS; let ots = last >> COUNTER_BITS;
if ots < ts { let msb = if ots < ts {
let msb = ts << COUNTER_BITS; ts << COUNTER_BITS
if LAST_MSB
.compare_exchange(last, msb, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
{
break (msb, lsb).into();
}
} else { } else {
let counter = ((last & bitmask!(COUNTER_BITS) as u64) as u16).saturating_add(1); let counter = ((last & bitmask!(COUNTER_BITS) as u64) as u16).saturating_add(1);
let msb = (ots << COUNTER_BITS) + counter as u64; (ots << COUNTER_BITS) + counter as u64
if LAST_MSB };
.compare_exchange(last, msb, Ordering::SeqCst, Ordering::Relaxed)
.is_ok() if LAST_MSB
{ .compare_exchange(last, msb, Ordering::SeqCst, Ordering::Relaxed)
break (msb, lsb).into(); .is_ok()
} {
break (msb, lsb).into();
} }
// we didn't update the global counter, try again // we didn't update the global counter, try again
let micros = thread_rng().gen_range(10..50); let micros = thread_rng().gen_range(10..50);
std::thread::sleep(Duration::from_micros(micros)); std::thread::sleep(Duration::from_micros(micros));