Skip to main content

RunningProgressLoop

Struct RunningProgressLoop 

Source
pub struct RunningProgressLoop { /* private fields */ }
Expand description

Runs periodic running progress reports for work tracked elsewhere.

RunningProgressLoop is useful when worker threads update shared state and a separate reporter thread should periodically emit running events. The loop owns only the signal receiver. Callers provide a Progress instance and a snapshot closure that converts their domain state into ProgressCounters.

Use Self::channel to create a loop and its matching RunningProgressNotifier. Move the loop into a reporter thread, clone the notifier into workers when zero-interval wakeups are needed, and send RunningProgressNotifier::stop when the operation is complete.

§Examples

use std::{
    sync::{
        Arc,
        atomic::{
            AtomicUsize,
            Ordering,
        },
    },
    thread,
    time::Duration,
};

use qubit_progress::{
    NoOpProgressReporter,
    Progress,
    ProgressCounters,
    RunningProgressLoop,
};

let reporter = NoOpProgressReporter;
let completed = Arc::new(AtomicUsize::new(0));
let (progress_loop, notifier) = RunningProgressLoop::channel();

thread::scope(|scope| {
    let loop_completed = Arc::clone(&completed);
    let reporter_ref = &reporter;
    let progress_thread = scope.spawn(move || {
        // This background reporter thread owns the loop. It does not own
        // the operation state; it only reads a fresh counter snapshot when
        // the interval is due or a worker sends a running point.
        let progress = Progress::new(reporter_ref, Duration::ZERO);
        progress_loop.run(progress, || {
            ProgressCounters::new(Some(3))
                .with_completed_count(loop_completed.load(Ordering::Acquire))
        });
    });

    // Worker code updates domain state first, then wakes the loop. With a
    // zero interval, each running point may emit a `running` event.
    for _ in 0..3 {
        completed.fetch_add(1, Ordering::AcqRel);
        assert!(notifier.running_point());
    }

    // Stop the loop before leaving the scope so reporter panics can be
    // propagated through the join handle.
    assert!(notifier.stop());
    progress_thread.join().expect("progress loop should stop");
});

§Author

Haixing Hu

Implementations§

Source§

impl RunningProgressLoop

Source

pub fn channel() -> (Self, RunningProgressNotifier)

Creates a paired running progress loop and notifier.

§Returns

A loop that owns the signal receiver and a notifier that sends wakeup or stop signals to that loop.

Source

pub fn run<F>(self, progress: Progress<'_>, snapshot: F)
where F: FnMut() -> ProgressCounters,

Runs until a stop signal is received or every notifier is dropped.

§Parameters
  • progress - Progress run used to emit running events.
  • snapshot - Closure that returns the current counters whenever a running event may be due.
§Panics

Propagates panics from the configured reporter when a running event is due.

Auto Trait Implementations§

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.