pub struct PriorityResource { /* private fields */ }Expand description
A cloneable handle to a capacity-limited resource pool with priority scheduling.
Like Resource, units are acquired by calling
request and awaiting the returned future.
Unlike Resource, waiters are served in priority order: the waiter with
the lowest priority number is served first. Within the same priority level,
waiters are served FIFO.
Priority convention: lower number = higher priority (0 is highest).
PriorityResource wraps an Rc<RefCell<>> internally, so cloning is cheap
and all clones share the same pool. It is !Send + !Sync — consistent with
SimEnv.
An urgent case overtakes a routine one that queued first:
use std::cell::RefCell;
use std::rc::Rc;
use simu::{SimEnv, PriorityResource};
let mut env = SimEnv::with_seed(0);
let doctor = PriorityResource::new(1);
let seen = Rc::new(RefCell::new(Vec::new()));
// Occupy the doctor until t = 1.
let h = env.handle();
let d = doctor.clone();
env.spawn(async move {
let _g = d.request(5).await;
h.timeout(1.0).await;
});
// Two patients queue while the doctor is busy — routine (10) arrives
// before urgent (0), but urgent is served first.
for (name, priority) in [("routine", 10), ("urgent", 0)] {
let d = doctor.clone();
let seen = Rc::clone(&seen);
env.spawn(async move {
let _g = d.request(priority).await;
seen.borrow_mut().push(name);
});
}
env.run();
assert_eq!(*seen.borrow(), ["urgent", "routine"]); // lower number winsInternally this is a WaitQueue<u32> (a pub(crate) helper): the
u32 priority is the ordering key, and the queue’s internal sequence
counter provides FIFO tie-breaking within a level.
Implementations§
Source§impl PriorityResource
impl PriorityResource
Sourcepub fn request(&self, priority: u32) -> PriorityResourceRequest ⓘ
pub fn request(&self, priority: u32) -> PriorityResourceRequest ⓘ
Request one unit at the given priority (lower = higher priority).
Resolves immediately if a unit is available; otherwise suspends the calling process and wakes it before any lower-priority waiter when a unit becomes free.
The returned PriorityResourceGuard releases the unit when dropped.
Trait Implementations§
Source§impl Clone for PriorityResource
impl Clone for PriorityResource
Source§fn clone(&self) -> PriorityResource
fn clone(&self) -> PriorityResource
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for PriorityResource
impl !Send for PriorityResource
impl !Sync for PriorityResource
impl !UnwindSafe for PriorityResource
impl Freeze for PriorityResource
impl Unpin for PriorityResource
impl UnsafeUnpin for PriorityResource
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more