crossbeam-rs/crossbeam-epoch

Using `Epoch::distance()` may give us a wrong result?

jeehoonkang opened this issue · 2 comments

Suppose that usize is 32bits. Then the implementation of Epoch::decompose() implies the global epoch wraps around in 31bits: https://github.com/crossbeam-rs/crossbeam-epoch/blob/master/src/epoch.rs#L71

Then to my eyes, Epoch::distance() may give us a wrong result: https://github.com/crossbeam-rs/crossbeam-epoch/blob/master/src/epoch.rs#L39 . Suppose e1 is the maximum number that is represented in 31bits (i.e. (1 << 31) - 1), and e2 is the next epoch, which is represented in 0. Then distance() gives us (1 << 31) - 1, while a desired result is 1.

This will probably happen in a long-running process in 32bit machines.

I'd like to propose to change Epoch::distance() as follows:

    fn decompose(&self) -> (usize, bool) {
        (self.data & !1, (self.data & 1) == 1)
    }

Then the stride of the epoch advancement becomes 2, and we should adjust the implementation a little bit.

You're right, this is a bug. Want to implement a fix?

Sure, will submit a PR soon.