jdonaldson / promhx

A promise and functional reactive programming library for Haxe
MIT License
145 stars 24 forks source link

Use peekLast, as peek can return null while the queue still has items. #41

Closed vizanto closed 9 years ago

vizanto commented 9 years ago

Turns out java.vm.AtomicList.peek() is not safe/useful at all. Should probably report a bug. peekLast will work.

AtomicList is implemented with 2 pointers head and tail. Don't know why the implementer thought this would work without locking ... you can only CAS one value at a time. (Disclaimer: I only looked at the AtomicList code for 2 minutes.)

So it's possible that head is null, while tail still has data. Only tail is an atomic reference. peekLast returns tail and is correct.

Discovered this while testing a bunch of promises in a simple single threaded loop:

promhx.base.EventLoop.nextLoop = function(_) {};
// create a bunch of promises...
while (!promhx.base.EventLoop.queueEmpty()){
    promhx.base.EventLoop.f();
}