Browse Source

system: StoppableTask, add non-async stop_nowait(), and make Drop call this automatically so tasks are auto-cleaned up on exit.

rsx 2 years ago
parent
commit
3b4a64c054
1 changed files with 13 additions and 0 deletions
  1. 13 0
      src/system/stoppable_task.rs

+ 13 - 0
src/system/stoppable_task.rs

@@ -119,6 +119,13 @@ impl StoppableTask {
         self.barrier.wait().await;
         self.barrier.wait().await;
         trace!(target: "system::StoppableTask", "Stopped task {}", self.task_id);
         trace!(target: "system::StoppableTask", "Stopped task {}", self.task_id);
     }
     }
+
+    /// Sends a stop signal and returns immediately. Doesn't guarantee the task
+    /// stopped on completion.
+    pub fn stop_nowait(&self) {
+        trace!(target: "system::StoppableTask", "Stopping task (nowait) {}", self.task_id);
+        self.signal.notify();
+    }
 }
 }
 
 
 impl std::hash::Hash for StoppableTask {
 impl std::hash::Hash for StoppableTask {
@@ -138,6 +145,12 @@ impl std::cmp::PartialEq for StoppableTask {
 
 
 impl std::cmp::Eq for StoppableTask {}
 impl std::cmp::Eq for StoppableTask {}
 
 
+impl Drop for StoppableTask {
+    fn drop(&mut self) {
+        self.stop_nowait()
+    }
+}
+
 #[cfg(test)]
 #[cfg(test)]
 mod tests {
 mod tests {
     use super::*;
     use super::*;