Repository navigation
Add optional FIFO fairness to TimedSemaphore (LANG-1790) - #1840
Merged
garydgregory merged 1 commit intoOct 25, 2026
Merged
garydgregory merged 1 commit into
garydgregory merged 1 commit into
Conversation
Add Builder.setFair(boolean) and queue blocking acquisitions in FIFO order when fairness is enabled. Preserve the default non-fair behavior and the existing tryAcquire() API. Keep permit accounting synchronized, remove queued acquisitions on interruption or shutdown, and allow new unlimited acquisitions to proceed without waiting. Share the wait-and-shutdown check between acquisition paths. Document fairness guarantees and exceptions, and add regression coverage for ordering, cancellation, limit changes, and builder reuse.
garydgregory
force-pushed
the
feature/lang-1790-timed-semaphore-fairness
branch
from
October 25, 2026 17:48
227f0a8 to
991ba79
Compare
garydgregory
added a commit
that referenced
this pull request
Oct 25, 2026
garydgregory
added a commit
that referenced
this pull request
Oct 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add optional FIFO fairness to TimedSemaphore (LANG-1790).
Offered as an alternative to PR #1544
This changeset addresses LANG-1790 while preserving the existing default behavior and public API.
The main difference is that fairness is enforced by an explicit FIFO queue. In the original PR, callers still block on
Object.wait(), while the backingSemaphoreis only polled with immediate acquisition attempts. Those callers never join the semaphore’s waiting queue, so its fairness setting cannot enforce their acquisition order.This implementation also:
acquireCount.tryAcquire()signature and documents that it can bypass queued callers.tryAcquire()overload that this class does not provide.The regression tests establish that callers are waiting before checking ordering. They cover newcomer bypass prevention, explicit non-fair behavior, cancellation, shutdown, limit changes, and builder reuse. A controlled test also distinguishes FIFO behavior from non-fair behavior without relying on thread-start order or unrestricted completion order.
The original enhancement request is valid; these changes make the fairness guarantee effective while avoiding compatibility and concurrency regressions.