clean up new() logic

This commit is contained in:
Joe Ardent 2023-08-05 12:16:23 -07:00
parent 7067721f72
commit 50a59e1898

View file

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