|
|
@ -11,29 +11,26 @@ use lazy_static::lazy_static; |
|
|
|
use tokio::{ |
|
|
|
use tokio::{ |
|
|
|
spawn, |
|
|
|
spawn, |
|
|
|
task::JoinHandle, |
|
|
|
task::JoinHandle, |
|
|
|
//sync::mpsc::{channel, Receiver, Sender}
|
|
|
|
sync::mpsc::{channel, Receiver, Sender} |
|
|
|
}; |
|
|
|
}; |
|
|
|
use uuid::Uuid; |
|
|
|
use uuid::Uuid; |
|
|
|
|
|
|
|
|
|
|
|
pub type FutRes = UResult<JobResult>; |
|
|
|
pub type FutRes = UResult<ExactJob>; |
|
|
|
pub type DynFut = BoxFuture<'static, FutRes>; |
|
|
|
pub type DynFut = BoxFuture<'static, FutRes>; |
|
|
|
|
|
|
|
|
|
|
|
lazy_static! { |
|
|
|
lazy_static! { |
|
|
|
static ref FUT_RESULTS: Mutex<HashMap<Uuid, JoinInfo>> = Mutex::new(HashMap::new()); |
|
|
|
static ref FUT_RESULTS: Mutex<HashMap<Uuid, JoinInfo>> = Mutex::new(HashMap::new()); |
|
|
|
/*static ref FUT_CHANNEL: (Mutex<Sender<Uuid>>, Mutex<Receiver<Uuid>>) = {
|
|
|
|
static ref FUT_CHANNEL: (Sender<Uuid>, Mutex<Receiver<Uuid>>) = { |
|
|
|
spawn(init_receiver()); |
|
|
|
spawn(init_receiver()); |
|
|
|
let (tx, rx) = channel(100); |
|
|
|
let (tx, rx) = channel(100); |
|
|
|
(Mutex::new(tx), Mutex::new(rx)) |
|
|
|
(tx, Mutex::new(rx)) |
|
|
|
};*/ |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
/* |
|
|
|
|
|
|
|
async fn get_static_channel() -> (Sender<Uuid>, MutexGuard<'static, Receiver<Uuid>>) { |
|
|
|
async fn get_sender() -> Sender<Uuid> { |
|
|
|
( |
|
|
|
FUT_CHANNEL.0.clone() |
|
|
|
FUT_CHANNEL.0.lock().await.clone(), |
|
|
|
|
|
|
|
FUT_CHANNEL.1.lock().await |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
struct JoinInfo { |
|
|
|
struct JoinInfo { |
|
|
|
handle: JoinHandle<FutRes>, |
|
|
|
handle: JoinHandle<FutRes>, |
|
|
|
completed: bool, |
|
|
|
completed: bool, |
|
|
@ -54,26 +51,25 @@ impl Waiter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub async fn spawn(mut self) -> Self { |
|
|
|
pub async fn spawn(mut self) -> Self { |
|
|
|
let collectable = self.tasks.len() != 1; |
|
|
|
let collectable = true; //TODO: self.tasks.len() != 1;
|
|
|
|
for f in self.tasks.drain(..) { |
|
|
|
for f in self.tasks.drain(..) { |
|
|
|
//eprintln!("before static channel");
|
|
|
|
let tx = get_sender().await; |
|
|
|
//let tx = get_static_channel().await.0;
|
|
|
|
|
|
|
|
//eprintln!("after static channel");
|
|
|
|
|
|
|
|
let fid = Uuid::new_v4(); |
|
|
|
let fid = Uuid::new_v4(); |
|
|
|
self.fids.push(fid); |
|
|
|
self.fids.push(fid); |
|
|
|
/*let task_wrapper = async move {
|
|
|
|
let task_wrapper = async move { |
|
|
|
//eprintln!("inside wrapper (started): {}", fid);
|
|
|
|
debug!("inside wrapper (started): {}", fid); |
|
|
|
let result = f.await; |
|
|
|
let result = f.await; |
|
|
|
tx.send(fid).await.unwrap(); |
|
|
|
tx.send(fid).await.unwrap(); |
|
|
|
result |
|
|
|
result |
|
|
|
};*/ |
|
|
|
}; |
|
|
|
|
|
|
|
debug!("before JoinInfo"); |
|
|
|
let handle = JoinInfo { |
|
|
|
let handle = JoinInfo { |
|
|
|
handle: spawn(f), |
|
|
|
handle: spawn(task_wrapper), |
|
|
|
completed: false, |
|
|
|
completed: false, |
|
|
|
collectable |
|
|
|
collectable |
|
|
|
}; |
|
|
|
}; |
|
|
|
//eprintln!("before push: {}", fid);
|
|
|
|
debug!("before push: {}", fid); |
|
|
|
spawn(async {}).await.ok(); |
|
|
|
//spawn(async {}).await.ok();
|
|
|
|
FUT_RESULTS.lock().await.insert(fid, handle); |
|
|
|
FUT_RESULTS.lock().await.insert(fid, handle); |
|
|
|
} |
|
|
|
} |
|
|
|
self |
|
|
|
self |
|
|
@ -105,15 +101,19 @@ impl Waiter { |
|
|
|
async fn pop_task(fid: Uuid) -> Option<JoinInfo> { |
|
|
|
async fn pop_task(fid: Uuid) -> Option<JoinInfo> { |
|
|
|
FUT_RESULTS.lock().await.remove(&fid) |
|
|
|
FUT_RESULTS.lock().await.remove(&fid) |
|
|
|
} |
|
|
|
} |
|
|
|
/* |
|
|
|
|
|
|
|
async fn init_receiver() { |
|
|
|
async fn init_receiver() { |
|
|
|
while let Some(fid) = get_static_channel().await.1.recv().await { |
|
|
|
while let Some(fid) = FUT_CHANNEL.1.lock().await.recv().await { |
|
|
|
if let Some(j) = FUT_RESULTS.lock().await.get_mut(&fid) { |
|
|
|
//info!("init_receiver: next val: {}", &fid);
|
|
|
|
j.completed = true; |
|
|
|
if let Some(mut lock) = FUT_RESULTS.try_lock() { |
|
|
|
|
|
|
|
if let Some(j) = lock.get_mut(&fid) { |
|
|
|
|
|
|
|
//info!("init_receiver: marked as completed");
|
|
|
|
|
|
|
|
j.completed = true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
pub async fn pop_task_if_completed(fid: Uuid) -> Option<FutRes> { |
|
|
|
pub async fn pop_task_if_completed(fid: Uuid) -> Option<FutRes> { |
|
|
|
let &mut JoinInfo {handle: _, collectable, completed} = match FUT_RESULTS |
|
|
|
let &mut JoinInfo {handle: _, collectable, completed} = match FUT_RESULTS |
|
|
|
.lock() |
|
|
|
.lock() |
|
|
@ -122,7 +122,6 @@ pub async fn pop_task_if_completed(fid: Uuid) -> Option<FutRes> { |
|
|
|
Some(t) => t, |
|
|
|
Some(t) => t, |
|
|
|
None => return None |
|
|
|
None => return None |
|
|
|
}; |
|
|
|
}; |
|
|
|
//eprint!("{}, {}: ", &fid, *collectable);
|
|
|
|
|
|
|
|
if collectable && completed { |
|
|
|
if collectable && completed { |
|
|
|
let task = pop_task(fid).await.unwrap(); |
|
|
|
let task = pop_task(fid).await.unwrap(); |
|
|
|
let result = task.handle.await.unwrap(); |
|
|
|
let result = task.handle.await.unwrap(); |
|
|
|