G
YouTube TikTok Games
Skip to content

Add optional FIFO fairness to TimedSemaphore (LANG-1790) - #1840

Merged
garydgregory merged 1 commit into
apache:masterfrom
garydgregory:feature/lang-1790-timed-semaphore-fairness
Oct 25, 2026
Merged

garydgregory merged 1 commit into
apache:masterfrom
garydgregory:feature/lang-1790-timed-semaphore-fairness

Conversation

@garydgregory

@garydgregory garydgregory commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

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 backing Semaphore is 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:

  • Keeps permit acquisition, accounting, limit changes, and period rollover under the same monitor, avoiding the race between consuming a permit and updating acquireCount.
  • Preserves the non-throwing tryAcquire() signature and documents that it can bypass queued callers.
  • Handles interruption and shutdown by removing the correct queue entry and notifying remaining callers.
  • Preserves unlimited-mode behavior: new acquisitions proceed immediately, and existing waiters can proceed at rollover.
  • Documents the actual ordering guarantee without referring to a timed 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.

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
garydgregory force-pushed the feature/lang-1790-timed-semaphore-fairness branch from 227f0a8 to 991ba79 Compare October 25, 2026 17:48
@garydgregory
garydgregory merged commit e5f7f1b into apache:master Oct 25, 2026
22 of 23 checks passed
garydgregory added a commit that referenced this pull request Oct 25, 2026
@garydgregory
garydgregory deleted the feature/lang-1790-timed-semaphore-fairness branch October 25, 2026 18:02
garydgregory added a commit that referenced this pull request Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant