Skip to content

Added a way to obtain an indication of the current task state #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.39"
msrv = "1.40"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
matrix:
# When updating this, the reminder to update the minimum supported
# Rust version in Cargo.toml and .clippy.toml.
rust: ['1.39']
rust: ['1.40']
steps:
- uses: actions/checkout@v2
- name: Install Rust
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "async-task"
version = "4.1.0"
authors = ["Stjepan Glavina <[email protected]>"]
edition = "2018"
rust-version = "1.39"
rust-version = "1.40"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/smol-rs/async-task"
description = "Task abstraction for building executors"
Expand Down
28 changes: 28 additions & 0 deletions src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,23 @@ impl<T> Task<T> {
let header = ptr as *const Header;
unsafe { &*header }
}

/// Get the current state of the task.
///
/// Note that in a multithreaded environment, this state can change immediately after calling this function.
pub fn state(&self) -> TaskState {
let ptr = self.ptr.as_ptr();
let header = ptr as *const Header;

unsafe {
let state = (*header).state.load(Ordering::Acquire);
if state & (CLOSED | COMPLETED) != 0 {
TaskState::Completed
} else {
TaskState::Running
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that TaskState is non_exhaustive, the user can really only rely on state==Completed (since state==Running may change its meaning in the future).

So I am starting to think that it might actually be preferable to have a method that returns a bool like is_completed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std JoinHandle has a similar unstable feature, which appears to be planned to be renamed is_finished.
rust-lang/rust#94549

}
}
}

impl<T> Drop for Task<T> {
Expand Down Expand Up @@ -515,3 +532,14 @@ impl<T> fmt::Debug for FallibleTask<T> {
.finish()
}
}

/// The state of a task.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TaskState {
/// The task is still running.
Running,

/// The task has completed.
Completed,
}