Fixed task::spawn not properly running things in another thread.

This commit is contained in:
Filipe Rodrigues 2021-05-31 04:17:42 +01:00
parent a41b04acb3
commit 72c74b89d6
3 changed files with 8 additions and 5 deletions

View File

@ -6,7 +6,6 @@ edition = "2018"
[dependencies]
# Dcb
dcb = { path = "../../dcb" }
dcb-bytes = { path = "../../dcb-bytes" }
dcb-util = { path = "../../dcb-util" }
dcb-cdrom-xa = { path = "../../dcb-cdrom-xa" }

View File

@ -25,4 +25,7 @@ ref-cast = "1.0.6"
# Thread
rayon = "1.5.1"
futures = "0.3.15"
futures = "0.3.15"
# Logging
log = "0.4.14"

View File

@ -9,7 +9,7 @@ use std::{
};
/// Spawns a task and returns a future for awaiting it's value
pub fn spawn<T: Send>(f: impl FnOnce() -> T + Send) -> ValueFuture<T> {
pub fn spawn<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> ValueFuture<T> {
// Create the value mutex
let mutex = Arc::new(Mutex::new(None));
let future = ValueFuture {
@ -18,8 +18,9 @@ pub fn spawn<T: Send>(f: impl FnOnce() -> T + Send) -> ValueFuture<T> {
};
// Spawn the task
rayon::scope(move |_| {
*mutex.lock().expect("Poisoned") = Some(f());
rayon::spawn(move || {
let value = f();
*mutex.lock().expect("Poisoned") = Some(value);
});
// And return the future