#4446·tokio

Semaphore::acquire_many API with `usize` count

Author: danburkertCreated Jan 29, 2022Updated Sep 19, 2026
LabelsA-tokioM-syncC-feature-request

Is your feature request related to a problem? Please describe.

The following Semaphore APIs use a u32 count of permits:

  • Semaphore::acquire_many
  • Semaphore::try_acquire_many
  • Semaphore::acquire_many_owned
  • Semaphore::try_acquire_many_owned

Whereas the following Semaphore APIs use a usize count of permits:

  • Semaphore::new
  • Semaphore::const_new
  • Semaphore::available_permits
  • Semaphore::add_permits

This incongruity in the API is awkward, but it also prohibits a key usecase for the Semaphore type: protecting access to memory, e.g. memory leases. In fact the PR which added the u32 APIs mentions this usecase, but presumably the author didn't need to support leases of more than 4GiB (#2607).

Describe the solution you'd like

  1. Change [Owned]SemaphorePermit to hold a usize permit count internally instead of u32. I haven't confirmed, but I believe the size_of SemaphorePermit won't change due to padding, so hopefully this won't have any performance impact.
  2. Add four new methods replacing each permutation of [try_]acquire_many[_owned], and which take a usize permit count. To kick off the painting, I propose potential names below.
  3. [optional] Deprecate Semaphore::[try_]acquire_many[_owned].

Describe alternatives you've considered

For applications which need to acquire more than 2^32 permits there really isn't a way to use the current semaphore API - it's not correct to acquire permits in a loop due to risk of deadlock.

Additional context

Potential names for the new methods:

  • [try_]acquire_many_permits[_owned]
  • [try_]acquire_permits[_owned]
  • [try_]acquire_n[_owned]