hedzr / go-ringbuf

Lock-free MPMC Ring Buffer (Generic) for SMP, in golang. Some posts in chinese:
https://hedzr.com/algorithm/golang/ringbuf-index/
Apache License 2.0
76 stars 4 forks source link

Potential unprotected critical zone? #4

Open abbychau opened 2 months ago

abbychau commented 2 months ago

Wanna know if it is an unprotected critical zone between 121 and 122

https://github.com/hedzr/go-ringbuf/blob/73252d2c16b7c25981c2ac6fdba56feb863e7bd4/mpmc/rb.go#L122

hedzr commented 2 months ago

the ever original idea is:

cas holder.readweite from 0 to 2 must not be failed, logically. so, line 122 would never get in control. line 122-126 is just a defensive block. it could be remove safely.

if it really reached, the enqueue or dequeue would be failure.

abbychau commented 2 months ago

120-123 looks like a potential spinlock to me, but 122 looks like getting away from the spin. so i asked why 121 and 122 works like this. Kind of unclear to me about the whole retry block ^^; Could you give a bit explanation?