please critique my mpmc stack implementation. I am unsure about a few things. First, have I used the unsafe package correctly? Second, will this implementation suffer the ABA problem?
type Stack struct {
next unsafe.Pointer
}
type Element struct {
next unsafe.Pointer
value int
}
func (stack *Stack) Push(value int) (b bool) {
pushed := Element{value: value}
up_pushed := unsafe.Pointer(&pushed)
for {
pushed.next = atomic.LoadPointer(&stack.next)
if atomic.CompareAndSwapPointer(&stack.next, pushed.next, up_pushed) {
b = true
return
}
}
return
}
func (stack *Stack) Pop() (b bool, value int) {
for {
up_popped := atomic.LoadPointer(&stack.next)
if up_popped == nil {
return
}
popped := *(*Element)(unsafe.Pointer(up_popped))
if atomic.CompareAndSwapPointer(&stack.next, up_popped, popped.next) {
b = true
value = popped.value
popped.next = nil
return
}
}
return
}