So the question of whether we can use semaphores as an implementation detail of a kind of unbounded FIFO queue mechanism for an arbitrary number of goroutines came up with a colleague earlier today. My colleague pointed out that we couldn’t rely on FIFO behavior with semaphores since it’s neither implied by the API semantics nor is it documented at the package level. In fact, he was assuming that it isn’t a FIFO whereas I assumed that it is.
When I dug down into the implementation, I found that it is:
-
Weighted.Acquire
pushes an empty “waiter” channel to the back of alist.List
-
Weighted.notifyWaiters
takes items from the front of thelist.List
whenever any given holder releases the semaphore:
So my question – is FIFO behavior simply an accident of implementation here, or is it intended and protected from backwards-incompatible changes in the future?
If it’s intentional, would it make sense to document at the package level to clarify?
Thanks in advance to anyone who can take the time to clarify this!