| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 2 | syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::{Condvarrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/condvar/Condvar#, Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#}, |
| 3 | timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, |
| 4 | }; |
| 5 | |
| 6 | /// A cancellation token. |
| 7 | pub(craterust-analyzer cargo bottom 0.14.9 crate/) struct CancellationTokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken# { |
| 8 | // The "check" for the cancellation token. Setting this to true will mark the cancellation |
| 9 | // token as "cancelled". |
| 10 | mutexrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#mutex.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#<bool>, |
| 11 | cvarrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#cvar.: Condvarrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/condvar/Condvar#, |
| 12 | } |
| 13 | |
| 14 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for CancellationTokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken# { |
| 15 | fn defaultrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken][Default]default().() -> Selfrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken][Default] { |
| 16 | Selfrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken][Default] { |
| 17 | mutexrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#mutex.: Mutexrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/Mutex#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]new().(false), |
| 18 | cvarrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#cvar.: Condvarrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/condvar/Condvar#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/condvar/impl#[Condvar]new().(), |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl CancellationTokenrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken# { |
| 24 | /// Mark the [`CancellationToken`] as cancelled. |
| 25 | /// |
| 26 | /// This is idempotent, and once cancelled, will stay cancelled. Sending it |
| 27 | /// again will not do anything. |
| 28 | pub fn cancelrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]cancel().(&self) { |
| 29 | let mut guard = self |
| 30 | .mutexrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#mutex. |
| 31 | .lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().() |
| 32 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("cancellation token lock should not be poisoned"); |
| 33 | |
| 34 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().*rust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`MutexGuard<'_, T>`][Deref]deref().guard { |
| 35 | *rust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`MutexGuard<'_, T>`][DerefMut]deref_mut().guard = true; |
| 36 | self.cvarrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#cvar..notify_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/condvar/impl#[Condvar]notify_all().(); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Try and check the [`CancellationToken`]'s status. Note that |
| 41 | /// this will not block. |
| 42 | pub fn try_checkrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]try_check().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<bool> { |
| 43 | self.mutexrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#mutex..try_lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]try_lock().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|guard| *rust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`MutexGuard<'_, T>`][Deref]deref().guard) |
| 44 | } |
| 45 | |
| 46 | /// Allows a thread to sleep while still being interruptible with by the |
| 47 | /// token. |
| 48 | /// |
| 49 | /// Returns the condition state after either sleeping or being woken up. |
| 50 | pub fn sleep_with_cancellationrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/impl#[CancellationToken]sleep_with_cancellation().(&self, duration: Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#) -> bool { |
| 51 | let guard = self |
| 52 | .mutexrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#mutex. |
| 53 | .lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`Mutex<T>`]lock().() |
| 54 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("cancellation token lock should not be poisoned"); |
| 55 | |
| 56 | let (result, _) = self |
| 57 | .cvarrust-analyzer cargo bottom 0.14.9 utils/cancellation_token/CancellationToken#cvar. |
| 58 | .wait_timeoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/condvar/impl#[Condvar]wait_timeout().(guard, duration) |
| 59 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("cancellation token lock should not be poisoned"); |
| 60 | |
| 61 | *rust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/poison/mutex/impl#[`MutexGuard<'_, T>`][Deref]deref().result |
| 62 | } |
| 63 | } |