westerndigitalcorporation/desmod

Queue difference between when_any and when_new

abeham opened this issue · 3 comments

I don't see a difference between these two events. when_any checks if _self.items: but that always needs to satisfy given that it is called only in _trigger_put after a new item has been enqueued.

Shouldn't when_any also be called in _trigger_get?

Hi Andreas. Thanks for taking an interest in desmod!

The reason _trigger_when_any() is not called from _trigger_get() is that when when_any() is called (when the QueueWhenAnyEvent event is created), that event is either going to be triggered at that moment because the queue has items; or it will be triggered later, when an item is put into the queue. Thus at get()-time, _any_waiters will be empty because if the get() can be fulfilled, that means that the queue became non-empty, and if the queue became non-empty then the _any_waiters will have been cleared.

I am open to the above explanation being wrong--it's easy to miss edge cases. A test case that demonstrates a problem would be helpful.

W.r.t. the difference between when_any() and when_new(): with an empty queue they will behave the same. The difference between when_any() and when_new() is how they behave when called against a non-empty queue:

  • when_any() will trigger/succeed immediately (because the queue has "any" items).
  • when_new() will not trigger immediately. It will trigger when the next new item is put() to the queue.

Hope this helps. Please let me know if there are any specific cases where these methods aren't working as expected.

Ok, thanks I did not notice the _trigger* calls in the init methods of QueueWhenAnyEvent and QueueWhenFullEvent. Since you trigger immediately in case any item is in the queue, I also don't see a necessity for _trigger_get(). I had misinterpreted this as "any change" to the queue.