diff options
author | Stjepan Glavina <stjepang@gmail.com> | 2020-04-12 19:46:20 +0200 |
---|---|---|
committer | Stjepan Glavina <stjepang@gmail.com> | 2020-04-12 19:46:20 +0200 |
commit | 9405905e51add3a14a96d360989c11a6bd164f62 (patch) | |
tree | 17450aeef27abff02d55b02f137766e4ead06583 /src | |
parent | b7a249680490991f92cc2144d4eff65e4effb3b7 (diff) | |
download | async-task-9405905e51add3a14a96d360989c11a6bd164f62.tar.gz |
Run returns a bool
Diffstat (limited to 'src')
-rw-r--r-- | src/raw.rs | 9 | ||||
-rw-r--r-- | src/task.rs | 7 |
2 files changed, 11 insertions, 5 deletions
@@ -31,7 +31,7 @@ pub(crate) struct TaskVTable { pub(crate) destroy: unsafe fn(*const ()), /// Runs the task. - pub(crate) run: unsafe fn(*const ()), + pub(crate) run: unsafe fn(*const ()) -> bool, /// Creates a new waker associated with the task. pub(crate) clone_waker: unsafe fn(ptr: *const ()) -> RawWaker, @@ -457,7 +457,7 @@ where /// /// If polling its future panics, the task will be closed and the panic will be propagated into /// the caller. - unsafe fn run(ptr: *const ()) { + unsafe fn run(ptr: *const ()) -> bool { let raw = Self::from_ptr(ptr); // Create a context from the raw task pointer and the vtable inside the its header. @@ -480,7 +480,7 @@ where // Drop the task reference. Self::drop_task(ptr); - return; + return false; } // Mark the task as unscheduled and running. @@ -587,6 +587,7 @@ where // The thread that woke the task up didn't reschedule it because // it was running so now it's our responsibility to do so. Self::schedule(ptr); + return true; } else { // Drop the task reference. Self::drop_task(ptr); @@ -599,6 +600,8 @@ where } } + return false; + /// A guard that closes the task if polling its future panics. struct Guard<F, R, S, T>(RawTask<F, R, S, T>) where diff --git a/src/task.rs b/src/task.rs index 8ef209c..c212f3b 100644 --- a/src/task.rs +++ b/src/task.rs @@ -217,6 +217,9 @@ impl<T> Task<T> { /// Runs the task. /// + /// Returns `true` if the task was woken while running, in which case it gets rescheduled at + /// the end of this method invocation. + /// /// This method polls the task's future. If the future completes, its result will become /// available to the [`JoinHandle`]. And if the future is still pending, the task will have to /// be woken up in order to be rescheduled and run again. @@ -230,13 +233,13 @@ impl<T> Task<T> { /// /// [`JoinHandle`]: struct.JoinHandle.html /// [`catch_unwind`]: https://doc.rust-lang.org/std/panic/fn.catch_unwind.html - pub fn run(self) { + pub fn run(self) -> bool { let ptr = self.raw_task.as_ptr(); let header = ptr as *const Header; mem::forget(self); unsafe { - ((*header).vtable.run)(ptr); + ((*header).vtable.run)(ptr) } } |