Struct linux_futex::Futex

source ·
#[repr(transparent)]
pub struct Futex<Scope> { pub value: AtomicU32, /* private fields */ }
Expand description

A Linux-specific fast user-space locking primitive.

Futex<Private> may only be used from the same address space (the same process) and is faster than a Futex<Shared>, which may be used accross address spaces (processes).

Fields§

§value: AtomicU32

Implementations§

source§

impl<S> Futex<S>

source

pub const fn new(value: u32) -> Self

Create a new Futex with an initial value.

source§

impl<S: Scope> Futex<S>

source

pub fn wait(&self, expected_value: u32) -> Result<(), WaitError>

Wait until this futex is awoken by a wake call.

The thread will only be sent to sleep if the futex’s value matches the expected value. Otherwise, it returns directly with WaitError::WrongValue.

source

pub fn wait_for( &self, expected_value: u32, timeout: Duration, ) -> Result<(), TimedWaitError>

Wait until this futex is awoken by a wake call, or until the timeout expires.

The thread will only be sent to sleep if the futex’s value matches the expected value. Otherwise, it returns directly with TimedWaitError::WrongValue.

If you want an absolute point in time as timeout, use wait_bitset_until instead, using a bitset of !0.

source

pub fn wake(&self, n: i32) -> i32

Wake up n waiters.

Returns the number of waiters that were woken up.

source

pub fn requeue(&self, n_wake: i32, to: &Futex<S>, n_requeue: i32) -> i32

Wake up n_wake waiters, and requeue up to n_requeue waiters to another futex.

Returns the number of waiters that were woken up.

source

pub fn cmp_requeue( &self, expected_value: u32, n_wake: i32, to: &Futex<S>, n_requeue: i32, ) -> Result<i32, WrongValueError>

Wake up n_wake waiters, and requeue up to n_requeue waiters to another futex.

The operation will only execute if the futex’s value matches the expected value. Otherwise, it returns directly with a WrongValueError.

Returns the total number of waiters that were woken up or requeued to the other futex.

source

pub fn wait_bitset( &self, expected_value: u32, bitset: u32, ) -> Result<(), WaitError>

Wait until this futex is awoken by a wake call matching a bitset.

  • Calls to wake will match any bitset.
  • Calls to wake_bitset will match if at least one 1-bit matches.

The thread will only be sent to sleep if the futex’s value matches the expected value. Otherwise, it returns directly with WaitError::WrongValue.

source

pub fn wait_bitset_until( &self, expected_value: u32, bitset: u32, timeout: impl Timeout, ) -> Result<(), TimedWaitError>

Wait until this futex is awoken by a wake call matching a bitset, or until the timeout expires.

  • Calls to wake will match any bitset.
  • Calls to wake_bitset will match if at least one 1-bit matches.

The thread will only be sent to sleep if the futex’s value matches the expected value. Otherwise, it returns directly with TimedWaitError::WrongValue.

source

pub fn wake_bitset(&self, n: i32, bitset: u32) -> i32

Wake up n waiters matching a bitset.

  • Waiters waiting using wait are always woken up, regardless of the bitset.
  • Waiters waiting using wait_bitset are woken up if they match at least one 1-bit.

Returns the number of waiters that were woken up.

source

pub fn wake_op(&self, n: i32, second: &Futex<S>, op: OpAndCmp, n2: i32) -> i32

Wake up n waiters, and conditionally n2 waiters on another futex after modifying it.

This operation first applies an operation to the second futex while remembering its old value, then wakes up n waiters on the first futex, and finally wakes n2 waiters on the second futex if its old value matches a condition. This all happens atomically.

Returns the total number of waiters that were woken up on either futex.

source

pub fn cmp_requeue_pi( &self, expected_value: i32, to: &PiFutex<S>, n_requeue: i32, ) -> Result<i32, TryAgainError>

Wake up one waiter, and requeue up to n_requeue to a PiFutex.

Only requeues waiters that are blocked by wait_requeue_pi or wait_requeue_pi_until. The PiFutex must be the same as the one the waiters are waiting to be requeued to.

The number of waiters to wake cannot be chosen and is always 1.

Returns the total number of waiters that were woken up or requeued to the other futex.

source

pub fn wait_requeue_pi( &self, expected_value: u32, second: &PiFutex<S>, ) -> Result<(), RequeuePiError>

Wait until this futex is awoken after potentially being requeued to a PiFutex.

A call to cmp_requeue_pi will requeue this waiter to the PiFutex. The call must refer to the same PiFutex.

A call to wake (or wake_bitset) will wake this thread without requeueing. This results in an RequeuePiError::TryAgain.

source

pub fn wait_requeue_pi_until( &self, expected_value: u32, second: &PiFutex<S>, timeout: impl Timeout, ) -> Result<(), TimedRequeuePiError>

Wait until this futex is awoken after potentially being requeued to a PiFutex, or until the timeout expires.

A call to cmp_requeue_pi will requeue this waiter to the PiFutex. The call must refer to the same PiFutex.

A call to wake (or wake_bitset) will wake this thread without requeueing. This results in an TimedRequeuePiError::TryAgain.

Trait Implementations§

source§

impl<S> Debug for Futex<S>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<S> Default for Futex<S>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Scope> !Freeze for Futex<Scope>

§

impl<Scope> RefUnwindSafe for Futex<Scope>
where Scope: RefUnwindSafe,

§

impl<Scope> Send for Futex<Scope>
where Scope: Send,

§

impl<Scope> Sync for Futex<Scope>
where Scope: Sync,

§

impl<Scope> Unpin for Futex<Scope>
where Scope: Unpin,

§

impl<Scope> UnwindSafe for Futex<Scope>
where Scope: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.