Compare commits

..

No commits in common. 'safe-cancel-interrupt' and 'master' have entirely different histories.

@ -45,7 +45,6 @@ async fn work() -> Result<(), Box<dyn std::error::Error>>
} }
} }
use futures::future::FutureExt;
work::work(&args.flags, leanify, args.files, match args.flags.hard_limit { work::work(&args.flags, leanify, args.files, match args.flags.hard_limit {
Some(hard_limit) => { Some(hard_limit) => {
// We have hard limit // We have hard limit
@ -59,7 +58,7 @@ async fn work() -> Result<(), Box<dyn std::error::Error>>
} }
}, },
_ => args.max_children, _ => args.max_children,
}, tokio::signal::ctrl_c().boxed()).await }).await
} }

@ -59,11 +59,6 @@ pub enum CommandKind
AddTask(Task), AddTask(Task),
RemoveTask(usize), RemoveTask(usize),
RemoveAll {
lock: bool,
title: Option<Cow<'static, str>>,
},
Complete, Complete,
Many(Vec<CommandKind>), Many(Vec<CommandKind>),
@ -199,12 +194,6 @@ impl<'a> CommandBuilder<'a>
self self
} }
pub fn clear_tasks(&mut self, lock: bool, title: Option<&'static str>) -> &mut Self
{
self.send_command(CommandKind::RemoveAll{ title: title.map(Cow::Borrowed), lock });
self
}
/// Signal a shutdown to the worker /// Signal a shutdown to the worker
pub fn shutdown(&mut self) -> &mut Self pub fn shutdown(&mut self) -> &mut Self
{ {
@ -328,11 +317,6 @@ impl ProgressSender
self.send_command(CommandKind::RemoveTask(task_idx)).await self.send_command(CommandKind::RemoveTask(task_idx)).await
} }
pub async fn clear_tasks(&mut self, lock: bool, title: Option<String>) -> Result<CommandWaiter, Error>
{
self.send_command(CommandKind::RemoveAll{ title: title.map(Cow::Owned), lock }).await
}
/// Signal a shutdown to the worker /// Signal a shutdown to the worker
pub async fn shutdown(&mut self) -> Result<CommandWaiter, Error> pub async fn shutdown(&mut self) -> Result<CommandWaiter, Error>
{ {
@ -479,20 +463,6 @@ pub fn create_progress<P: ProgressBar + WithTitle + Send + 'static,
progress.set_title(format!("({}/{}) {}", stat.low, stat.high, list.as_ref()).as_str()); progress.set_title(format!("({}/{}) {}", stat.low, stat.high, list.as_ref()).as_str());
} }
}, },
CommandKind::RemoveAll{ title: None, lock } => {
if lock {
list.poison();
}
list.clear();
progress.set_title(format!("({}/{}) {}", stat.low, stat.high, list.as_ref()).as_str());
},
CommandKind::RemoveAll{ title: Some(title), lock } => {
if lock {
list.poison();
}
list.clear();
progress.set_title(format!("({}/{}) {}", stat.low, stat.high, title.as_ref()).as_str());
},
CommandKind::Complete => { CommandKind::Complete => {
if has_blanked { if has_blanked {
progress.refresh(); progress.refresh();

@ -1,9 +1,5 @@
use super::*; use super::*;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::sync::atomic::{
AtomicBool,
Ordering,
};
#[cfg(nightly)] type ListAbs<T> = std::collections::LinkedList<T>; #[cfg(nightly)] type ListAbs<T> = std::collections::LinkedList<T>;
#[cfg(not(nightly))] type ListAbs<T> = Vec<T>; #[cfg(not(nightly))] type ListAbs<T> = Vec<T>;
@ -13,8 +9,6 @@ pub struct TaskList {
list: ListAbs<(usize, String)>, list: ListAbs<(usize, String)>,
buffer: String, buffer: String,
index: usize, index: usize,
poisoned: AtomicBool,
} }
fn find<T,F>(list: &ListAbs<T>, mut fun: F) -> Option<usize> fn find<T,F>(list: &ListAbs<T>, mut fun: F) -> Option<usize>
@ -38,8 +32,6 @@ impl TaskList
list: ListAbs::new(), list: ListAbs::new(),
buffer: String::new(), buffer: String::new(),
index: 0, index: 0,
poisoned: AtomicBool::new(false),
} }
} }
@ -52,15 +44,10 @@ impl TaskList
/// Push a new task string, and return its ID. /// Push a new task string, and return its ID.
pub fn push(&mut self, string: impl Into<String>) -> usize pub fn push(&mut self, string: impl Into<String>) -> usize
{ {
if self.poisoned.load(Ordering::SeqCst) {
return 0;
}
let idx = { let idx = {
self.index+=1; self.index+=1;
self.index self.index
}; };
#[cfg(nightly)] self.list.push_back((idx,string.into())); #[cfg(nightly)] self.list.push_back((idx,string.into()));
#[cfg(not(nightly))] self.list.push((idx,string.into())); #[cfg(not(nightly))] self.list.push((idx,string.into()));
@ -71,10 +58,6 @@ impl TaskList
/// Pop a task off the string, returns `true` if successful, `false` if wasn't found. /// Pop a task off the string, returns `true` if successful, `false` if wasn't found.
pub fn pop(&mut self, idx: usize) -> bool pub fn pop(&mut self, idx: usize) -> bool
{ {
if idx == 0 && self.poisoned.load(Ordering::Relaxed) {
return false;
}
if let Some(idx) = find(&self.list, |&(i, _)| idx == i) if let Some(idx) = find(&self.list, |&(i, _)| idx == i)
{ {
self.list.remove(idx); self.list.remove(idx);
@ -85,10 +68,6 @@ impl TaskList
} }
} }
pub fn poison(&self) {
self.poisoned.store(true, Ordering::SeqCst);
}
/// Clear the `TaskList` /// Clear the `TaskList`
pub fn clear(&mut self) pub fn clear(&mut self)
{ {
@ -131,8 +110,6 @@ impl<T> FromIterator<T> for TaskList
list: iter.into_iter().map(|x| ((i, x.into()), i+=1).0).collect(), list: iter.into_iter().map(|x| ((i, x.into()), i+=1).0).collect(),
buffer: Default::default(), buffer: Default::default(),
index: 0, index: 0,
poisoned: AtomicBool::new(false),
}; };
this.index = i; this.index = i;
this.recalc(); this.recalc();

@ -108,28 +108,12 @@ async fn do_work(process: impl AsRef<Process>, file: impl AsRef<OsStr>, mut prog
} }
} }
pub async fn work<I,T,U>(flags: &arg::Flags, process: U, files: I, children: Option<NonZeroUsize>, cancel: impl Future + Send + Unpin + 'static) -> Result<(), Box<dyn std::error::Error>> pub async fn work<I,T,U>(flags: &arg::Flags, process: U, files: I, children: Option<NonZeroUsize>) -> Result<(), Box<dyn std::error::Error>>
where I: IntoIterator<Item=T>, where I: IntoIterator<Item=T>,
<I as IntoIterator>::IntoIter: ExactSizeIterator, <I as IntoIterator>::IntoIter: ExactSizeIterator,
T: AsRef<OsStr> + Send + Sync + 'static + Clone, T: AsRef<OsStr> + Send + Sync + 'static + Clone,
U: Into<PathBuf> U: Into<PathBuf>
{ {
let (cancel, cancel_register) = {
use futures::future::AbortHandle;
let (handle, reg) = AbortHandle::new_pair();
let rh = handle.clone();
tokio::spawn(async move {
let _ = cancel.await;
handle.abort();
});
(rh, reg)
};
/// Make a future cancellable by the passed `cancel` token.
macro_rules! pass_cancel {
($a:expr) => (::futures::future::Abortable::new($a, cancel_register));
}
let (tx,mut rx) = mpsc::channel::<(T, fixed_stack::IntoIter<(bool, String)>, usize)>(children.as_ref().map(|&x| usize::from(x)).unwrap_or(16)); let (tx,mut rx) = mpsc::channel::<(T, fixed_stack::IntoIter<(bool, String)>, usize)>(children.as_ref().map(|&x| usize::from(x)).unwrap_or(16));
let semaphore = children.map(|children| Arc::new(Semaphore::new(children.into()))); let semaphore = children.map(|children| Arc::new(Semaphore::new(children.into())));
let process = Arc::new(Process::new(process, flags.leanify_flags.clone())); let process = Arc::new(Process::new(process, flags.leanify_flags.clone()));
@ -177,19 +161,16 @@ where I: IntoIterator<Item=T>,
}; };
let mut i=0usize; let mut i=0usize;
let results = let results =
pass_cancel!(join_all( join_all(
files files
.map(|filename| { .map(|filename| {
let semaphore = semaphore.clone(); let semaphore = semaphore.clone();
let process = Arc::clone(&process); let process = Arc::clone(&process);
let mut tx = tx.clone(); let mut tx = tx.clone();
let cancel = cancel.clone();
#[cfg(feature="progress")] let mut progress = progress.clone(); #[cfg(feature="progress")] let mut progress = progress.clone();
//TODO: Where to put the cancellation check in here?? (XXX: Current (tokio::spawn(async move {
//`AbortHandle` (old tokio version) does not have `is_aborted()`... :/)
(tokio::spawn(async move {
#[cfg(feature="progress")] type Opt<T> = OptionFuture<T>; #[cfg(feature="progress")] type Opt<T> = OptionFuture<T>;
#[cfg(not(feature="progress"))] type Opt<T> = std::marker::PhantomData<T>; #[cfg(not(feature="progress"))] type Opt<T> = std::marker::PhantomData<T>;
let _task_id: Opt<_> = { let _task_id: Opt<_> = {
@ -198,7 +179,7 @@ where I: IntoIterator<Item=T>,
// (task_id, worker_result) // (task_id, worker_result)
let worker = { let worker = {
cfg_if!{ cfg_if!{
if #[cfg(feature="progress")] { if #[cfg(feature="progress")] {
let worker = do_work(&process, &filename, progress.clone()); let worker = do_work(&process, &filename, progress.clone());
let task = progress.add_task(format!("{:?}", filename.as_ref())); let task = progress.add_task(format!("{:?}", filename.as_ref()));
future::join(task, worker).await future::join(task, worker).await
@ -251,23 +232,9 @@ where I: IntoIterator<Item=T>,
let _ = progress.bump_min(1).await; let _ = progress.bump_min(1).await;
} }
}),i+=1).0 }),i+=1).0
}))).await })).await
.map(|x| x
.into_iter() .into_iter()
.filter_map(|x| x.err())); .filter_map(|x| x.err());
let results = match results {
Err(_) => {
#[cfg(feature="progress")] {
progress.eprintln("[!] Child aborting...").await?.await?;
progress.clear_tasks(true, Some("Waiting for existing...".to_string())).await?;
}
#[cfg(not(feature="progress"))] eprintln!("[!] Child aborting...");
todo!("XXX: How to actually implement this? Add global mutexed counter in `Process` itself to track them and optionally await on them? We have prevented any more from spawning, but how do we wait for the ones that already are (which is the whole point of this.)");
},
Ok(v) => v,
};
#[cfg(feature="progress")] progress.shutdown().await?; #[cfg(feature="progress")] progress.shutdown().await?;
for failed in results for failed in results

Loading…
Cancel
Save