pub struct Permit { /* private fields */ }
Expand description
A struct for cancelling operations.
Use new_sub()
to make a subordinate permit.
Call revoke()
to revoke a permit
and its subordinate permits, recursively.
Example
Graceful shutdown:
let top_permit = permit::Permit::new();
// Start some worker threads.
for _ in 0..5 {
let permit = top_permit.new_sub();
std::thread::spawn(move || {
while !permit.is_revoked() {
// ...
}
});
}
wait_for_shutdown_signal();
// Revoke all thread permits and wait for them to
// finish and drop their permits.
top_permit
.revoke()
.wait_subs_timeout(core::time::Duration::from_secs(3))
.unwrap();
Implementations§
source§impl Permit
impl Permit
sourcepub fn new() -> Self
pub fn new() -> Self
Makes a new permit.
This permit is not subordinate to any other permit. It has no superior.
Dropping the permit revokes it and any subordinate permits.
sourcepub fn new_sub(&self) -> Self
pub fn new_sub(&self) -> Self
Make a new permit that is subordinate to this permit.
Call revoke()
to revoke a permit
and its subordinate permits, recursively.
Dropping the permit revokes it and any subordinate permits.
sourcepub fn is_revoked(&self) -> bool
pub fn is_revoked(&self) -> bool
Returns true
if revoke()
has previously been called
on this permit or any of its superiors.
sourcepub fn ok(&self) -> Option<()>
pub fn ok(&self) -> Option<()>
Returns Some(())
if revoke()
has not been called
on this permit or any of its superiors.
sourcepub fn has_subs(&self) -> bool
pub fn has_subs(&self) -> bool
Returns true
if this permit has any subordinate permits that have not
been dropped.
This includes direct subordinates and their subordinates, recursively.
sourcepub fn sleep(&self, duration: Duration) -> Result<(), PermitRevoked>
pub fn sleep(&self, duration: Duration) -> Result<(), PermitRevoked>
Waits until duration
time passes or the permit is revoked.
Errors
Returns Err
when the permit is revoked.
sourcepub fn sleep_until(&self, deadline: Instant) -> Result<(), PermitRevoked>
pub fn sleep_until(&self, deadline: Instant) -> Result<(), PermitRevoked>
sourcepub fn wait_subs_timeout(
&self,
duration: Duration
) -> Result<(), DeadlineExceeded>
pub fn wait_subs_timeout( &self, duration: Duration ) -> Result<(), DeadlineExceeded>
Waits for all direct subordinate permits to drop.
Errors
Returns DeadlineExceeded
when duration
passes
and the permit has a subordinate permit.
sourcepub fn wait_subs_deadline(
&self,
deadline: Instant
) -> Result<(), DeadlineExceeded>
pub fn wait_subs_deadline( &self, deadline: Instant ) -> Result<(), DeadlineExceeded>
Waits for all direct subordinate permits to drop.
Errors
Returns DeadlineExceeded
when deadline
passes
and the permit has a subordinate permit.