Skip to content

Semaphore::acquire_many API with usize count #4446

@danburkert

Description

@danburkert

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-feature-requestCategory: A feature request.M-syncModule: tokio/sync

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions