Skip to main content

Task

Struct Task 

Source
pub struct Task(/* private fields */);
Expand description

A cancellable, concurrent-safe unit of work that tracks a start..end range.

Task is a reference-counted handle: each worker holds a strong Task (an Arc<TaskInner>), giving every worker a distinct identity even when two workers speculatively share the same progress cursor. This identity is what set_threads’s liveness sweep keys off (via WeakTask), so a dead worker is reclaimed regardless of how many twins still reference its cursor.

The range is stored as a single atomic u128 inside TaskInner, allowing lock-free reads and fine-grained progress updates. Multiple workers can safely steal sub-ranges from the same task via split_two.

Two Tasks are equal iff they point to the same underlying state (see the PartialEq impl, which uses Arc::ptr_eq).

Implementations§

Source§

impl Task

Source

pub fn new(range: Range<u64>) -> Self

§Panics

Panics when range.start > range.end

Source

pub fn get(&self) -> Range<u64>

Returns the current start..end range, loaded atomically with Acquire ordering.

Source

pub fn start(&self) -> u64

Returns the current start of the work range (the high 64 bits of the atomic state).

Loads independently of end: combining the two (task.end() - task.start()) can observe a torn snapshot under concurrency. Use get or remain when a consistent view of both bounds is required.

Source

pub fn safe_add_start( &self, start: u64, bias: u64, ) -> Result<Range<u64>, RangeError>

Atomically advances start to min(start + bias, end), but only if that makes forward progress; then returns the slice that was claimed.

start must be a cursor value the caller actually observed via start or get; a start that runs ahead of the task’s real cursor is rejected, because honouring it would skip work no worker ever executed. A stale start is still accepted, but the returned span then begins at the real cursor and is shorter than bias — always consume the returned span, never assume start..start + bias.

§Errors

Returns RangeError when start + bias would not exceed the current start (no progress, a non-positive bias), or when start runs ahead of the task’s cursor.

Source

pub fn end(&self) -> u64

Returns the current end of the work range (the low 64 bits of the atomic state).

Loads independently of start; see that method for the torn-snapshot caveat when combining the two.

Source

pub fn remain(&self) -> u64

Returns end - start (saturating), i.e. how much work is left.

Source

pub fn split_two( &self, min_chunk_size: u64, ) -> Result<Option<Range<u64>>, RangeError>

Splits the work range in half, handing mid..end back to the caller as a new task and keeping start..mid in self.

The split only proceeds when both halves are at least min_chunk_size, i.e. remain >= min_chunk_size * 2. That test runs inside the compare-and-swap loop against the same atomic snapshot the commit observes, so a concurrent cursor-sharer (share_state) advancing start between the call and the commit cannot leak a half smaller than min_chunk_size. When the range is too small to split under min_chunk_size, returns Ok(None) without modifying self.

§Errors
  1. Returns RangeError when start > end
  2. Returns None when remain < min_chunk_size * 2 without modifying itself
Source

pub fn take(&self) -> Result<Option<Range<u64>>, RangeError>

Atomically claims and returns the entire remaining range start..end, emptying this task (sets start = end).

Shares its error contract with split_two: both consume remaining work, so both report a violated range invariant instead of normalising it away.

§Errors
  1. Returns RangeError when start > end
  2. Returns Ok(None) when the task is already empty, without modifying it
Source

pub fn downgrade(&self) -> WeakTask

Creates a WeakTask that does not keep the task’s state alive.

Source

pub fn strong_count(&self) -> usize

Returns the number of strong Task references to this worker’s identity (TaskInner).

Task and WeakTask are a paired strong/weak view of the same identity allocation, so this equals WeakTask::strong_count on a matching WeakTask — exactly like Arc/Weak. The (different) cursor-sharer count that steal caps on is exposed separately as the crate-internal sharer_count accessor.

Source

pub fn weak_count(&self) -> usize

Returns the number of weak WeakTask references to this worker’s identity (TaskInner). Pairs with strong_count, and equals WeakTask::weak_count on a matching WeakTask.

Trait Implementations§

Source§

impl Clone for Task

Source§

fn clone(&self) -> Task

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Task

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Task

Source§

impl From<Range<u64>> for Task

Creates a Task from a start..end range.

§Panics

Panics (via Task::new) if range.start > range.end.

Source§

fn from(value: Range<u64>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Task

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl Freeze for Task

§

impl RefUnwindSafe for Task

§

impl Send for Task

§

impl Sync for Task

§

impl Unpin for Task

§

impl UnsafeUnpin for Task

§

impl UnwindSafe for Task

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.