clean up new() logic
This commit is contained in:
parent
7067721f72
commit
50a59e1898
1 changed files with 11 additions and 15 deletions
26
src/julid.rs
26
src/julid.rs
|
@ -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));
|
||||||
|
|
Loading…
Reference in a new issue