You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently there is already a public Abortable::is_aborted() method, and there seems no obstacle in adding a AbortHandle::is_aborted()
Motivation
Sometimes Abortable needs to be accompanied by cooperative cancellation mechanism such as a atomic boolean flag. Rather than using another repetitive atomic variable, the AbortInner already hosts such atomic variable and the AbortHandle seems to be a good way to expose it.
Here is a hypothetical scenario where Abortable needs to be accompanied by cooperative cancellation mechanism. Suppose we have a blocking Mutex protecting a very tiny shared state, and we wish to stop another task from modifying it by calling its AbortHandle
// Aborter// ...let(new_abort_handle, new_abort_reg) = AbortHandle::new_pair();let cooperative_flag = Asc::new(AtomicBool::new(false));// new_abort_handle.clone();{letmut guard = mutex.lock();// Take out the old task's handlelet old_abort_handle = std::mem::replace(guard.abort_handle, new_abort_handle);
old_abort_handle.abort();// A modification to the cooperative flag is needed heredrop(guard);}// Spawn a new task using the new handle
tokio::spawn(Abortable::new(some_async_function(args, cooperative_flag), new_abort_reg);// We wish no modifications to the shared state from the old task from here on// Aborteeletmut guard = mutex.lock();// A cooperative checking is needed herewrite_into_shared_state(guard.state);drop(guard);
Since we used a blocking Mutex to protect our state (which is warranted in an async scenario) , it is very likely that our target Future is blocked on the mutex call when the AbortHandle::abort() was called. To solve this issue we would need a cooperative checking in our code. Exposing AbortHandle::is_aborted() would save the user from another atomic boolean flag.
The text was updated successfully, but these errors were encountered:
Currently there is already a public
Abortable::is_aborted()
method, and there seems no obstacle in adding aAbortHandle::is_aborted()
Motivation
Sometimes
Abortable
needs to be accompanied by cooperative cancellation mechanism such as a atomic boolean flag. Rather than using another repetitive atomic variable, theAbortInner
already hosts such atomic variable and theAbortHandle
seems to be a good way to expose it.Here is a hypothetical scenario where
Abortable
needs to be accompanied by cooperative cancellation mechanism. Suppose we have a blockingMutex
protecting a very tiny shared state, and we wish to stop another task from modifying it by calling itsAbortHandle
Since we used a blocking
Mutex
to protect our state (which is warranted in an async scenario) , it is very likely that our target Future is blocked on the mutex call when theAbortHandle::abort()
was called. To solve this issue we would need a cooperative checking in our code. ExposingAbortHandle::is_aborted()
would save the user from another atomic boolean flag.The text was updated successfully, but these errors were encountered: