Skip to content

Commit 7996d56

Browse files
Dr-EmannFreaky
authored andcommitted
Use thread parking to handle paused flag in background
1 parent 9070913 commit 7996d56

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/background.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<S> ControlToken<S> {
5858

5959
while self.is_paused() && !self.is_cancelled() {
6060
paused = true;
61-
thread::park_timeout(Duration::from_millis(10));
61+
thread::park();
6262
}
6363

6464
paused
@@ -92,6 +92,7 @@ impl<S> Default for ControlToken<S> {
9292
pub struct BackgroundHandle<T, S> {
9393
result: Receiver<std::thread::Result<T>>,
9494
control: ControlToken<S>,
95+
thread: thread::Thread,
9596
}
9697

9798
impl<T, S> BackgroundHandle<T, S> {
@@ -105,14 +106,17 @@ impl<T, S> BackgroundHandle<T, S> {
105106
let control = ControlToken::new();
106107
let inner_control = control.clone();
107108

108-
thread::spawn(move || {
109+
let handle = thread::spawn(move || {
109110
let response = catch_unwind(|| task.run(&inner_control));
110111
let _ = tx.send(response);
111112
});
112113

114+
let thread = handle.thread().clone();
115+
113116
BackgroundHandle {
114117
result: rx,
115118
control,
119+
thread,
116120
}
117121
}
118122

@@ -141,6 +145,7 @@ impl<T, S> BackgroundHandle<T, S> {
141145

142146
pub fn cancel(&self) {
143147
self.control.cancel();
148+
self.thread.unpark();
144149
}
145150

146151
pub fn is_cancelled(&self) -> bool {
@@ -157,6 +162,7 @@ impl<T, S> BackgroundHandle<T, S> {
157162

158163
pub fn resume(&self) {
159164
self.control.resume();
165+
self.thread.unpark();
160166
}
161167

162168
pub fn is_paused(&self) -> bool {

0 commit comments

Comments
 (0)