Skip to main content

Budget

Struct Budget 

Source
pub struct Budget<const N: usize> { /* private fields */ }
Expand description

A budget of u32 units divided among N claimant slots.

Declared (as a pub static) by supervisor_graph! for each divisible resource name, sized to the nodes and pool members that declare it: one slot each, numbered in declaration order. The protocol:

  1. Something provides the capacity — main for a fixed budget, or an allocator node that also names the slot in provides: so the budget empties when it stops. A holder whose budget is still unprovided at its gate deadline faults with FaultKind::ResourceMissing, like any other slot.
  2. Each holder’s shell receives a Claimant bound to its slot and states what it wants. The allocator divides the capacity over the wants with a BudgetPolicy (rebalance), and each holder reads its grant, or parks on wait_grant_change until it moves.
  3. When a holder stops — cleanly, or by missing its shutdown ack — the supervisor releases its slot, so a dead session never strands its share. A parked (Pause) holder keeps its claim.

The budget never chooses a division itself: what “fair” means (equal, proportional, ramped) is the policy’s, and when to re-divide is the allocator’s — usually on wait_change, which fires on every want, release and capacity change. Costs 4 + 8N bytes of atomics, two Signals and N single-waker slots per budget: 28 + 16N bytes on a 32-bit target.

Implementations§

Source§

impl<const N: usize> Budget<N>

Source

pub const fn new() -> Self

An unprovided budget (const — it lives in a static the macro emits).

Source

pub fn provide(&self, capacity: u32)

Set the capacity and wake both the supervisor’s gate wait and the allocator.

Source

pub const fn slots(&self) -> usize

The number of claimant slots: what the graph sized the budget to.

Source

pub fn capacity(&self) -> u32

The provided capacity, 0 when unprovided.

Source

pub fn want(&self, slot: u8, units: u32)

State slot slot’s demand and wake the allocator.

Source

pub fn release(&self, slot: u8)

Drop slot slot’s demand and grant, and wake the allocator: what the supervisor does when the slot’s holder stops.

Source

pub fn grant(&self, slot: u8) -> u32

Slot slot’s current grant.

Source

pub fn want_of(&self, slot: u8) -> u32

Slot slot’s stated demand.

Source

pub fn total_granted(&self) -> u32

The sum of every slot’s grant.

Source

pub fn rebalance( &self, policy: &impl BudgetPolicy, now: Instant, ) -> Option<Instant>

Re-divide the capacity over the current wants with policy, publish the new grants, and wake every holder whose grant moved.

Source

pub async fn wait_change(&self)

The allocator’s wait: resolves after any want, release or capacity change since the last wait (latching, single waiter).

Source

pub fn claimant(&'static self, slot: u8) -> Claimant

The handle a holder of slot claims through. Emitted by the macro into the holder’s task shell; hand-built nodes may call it directly.

Trait Implementations§

Source§

impl<const N: usize> Default for Budget<N>

Source§

fn default() -> Self

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

impl<const N: usize> Divisible for Budget<N>

Source§

fn want(&self, slot: u8, units: u32)

State slot’s demand.
Source§

fn release(&self, slot: u8)

Drop slot’s demand and grant.
Source§

fn grant(&self, slot: u8) -> u32

slot’s current grant.
Source§

fn register(&self, slot: u8, waker: &Waker)

Park waker until the next rebalance that moves slot’s grant.
Source§

impl<const N: usize> ResourceGate for Budget<N>

Source§

fn clear(&self)

Empty the budget: no capacity, no grants. Holders are woken so a loop parked on its grant sees the zero.

Source§

fn is_filled(&self) -> bool

Non-consuming “is the slot currently filled” check.
Source§

fn filled_signal(&self) -> &Signal<CriticalSectionRawMutex, ()>

The latching Signal fired by provide/restore, for the supervisor’s bounded pre-spawn wait (see Supervisor::start).

Auto Trait Implementations§

§

impl<const N: usize> !Freeze for Budget<N>

§

impl<const N: usize> !RefUnwindSafe for Budget<N>

§

impl<const N: usize> Send for Budget<N>
where [AtomicU32; N]: Send, [AtomicWaker; N]: Send,

§

impl<const N: usize> Sync for Budget<N>
where [AtomicU32; N]: Sync, [AtomicWaker; N]: Sync,

§

impl<const N: usize> Unpin for Budget<N>

§

impl<const N: usize> UnsafeUnpin for Budget<N>

§

impl<const N: usize> UnwindSafe for Budget<N>

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> CouplingPoint for T
where T: Sync + ?Sized,

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>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.