pub struct PreemptiveResource { /* private fields */ }Expand description
A cloneable handle to a capacity-limited pool whose held units can be preempted by higher-priority requests.
Like PriorityResource, units are requested at a
priority (lower number = higher priority) and blocked waiters are served in
priority order. Unlike it, when every unit is in use a higher-priority
request does not wait behind the holders — it evicts the lowest-priority
holder whose priority is strictly worse than its own, taking that unit
immediately.
Preemption is cooperative-at-yield: a higher-priority request fires the
victim’s preempted signal, and the victim is
expected to bail via any_of![work, guard.preempted()]. A well-behaved
holder races its work against that signal:
use simu::{SimEnv, PreemptiveResource, any_of};
let mut env = SimEnv::with_seed(0);
let res = PreemptiveResource::new(1);
let h = env.handle();
let r = res.clone();
env.spawn(async move {
let guard = r.request(2).await;
let service = 10.0;
// Race the service time against a possible preemption.
any_of![h.timeout(service), guard.preempted()].await;
if guard.is_preempted() {
// Higher-priority work took the unit — abandon and clean up.
return;
}
// Completed normally; dropping the guard releases the unit.
});
env.run();PreemptiveResource wraps an Rc<RefCell<>> internally, so cloning is
cheap and all clones share the same pool. It is !Send + !Sync.
Implementations§
Source§impl PreemptiveResource
impl PreemptiveResource
Sourcepub fn request(&self, priority: u32) -> PreemptiveRequest ⓘ
pub fn request(&self, priority: u32) -> PreemptiveRequest ⓘ
Request one unit at the given priority (lower = higher priority).
Resolves immediately if a unit is free or if a strictly lower-priority holder can be preempted; otherwise suspends in priority order until a unit is released or becomes preemptible.
The returned PreemptiveGuard releases the unit when dropped, and
exposes preempted /
is_preempted so the holder can yield
the unit cooperatively.
Trait Implementations§
Source§impl Clone for PreemptiveResource
impl Clone for PreemptiveResource
Source§fn clone(&self) -> PreemptiveResource
fn clone(&self) -> PreemptiveResource
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 PreemptiveResource
impl !Send for PreemptiveResource
impl !Sync for PreemptiveResource
impl !UnwindSafe for PreemptiveResource
impl Freeze for PreemptiveResource
impl Unpin for PreemptiveResource
impl UnsafeUnpin for PreemptiveResource
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