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 {
Some(hard_limit) => {
// We have hard limit
@ -59,7 +58,7 @@ async fn work() -> Result<(), Box<dyn std::error::Error>>
}
},
_ => args.max_children,
}, tokio::signal::ctrl_c().boxed()).await
}).await
}

@ -59,11 +59,6 @@ pub enum CommandKind
AddTask(Task),
RemoveTask(usize),
RemoveAll {
lock: bool,
title: Option<Cow<'static, str>>,
},
Complete,
Many(Vec<CommandKind>),
@ -199,12 +194,6 @@ impl<'a> CommandBuilder<'a>
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
pub fn shutdown(&mut self) -> &mut Self
{
@ -328,11 +317,6 @@ impl ProgressSender
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
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());
}
},
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 => {
if has_blanked {
progress.refresh();

@ -1,9 +1,5 @@
use super::*;
use std::iter::FromIterator;
use std::sync::atomic::{
AtomicBool,
Ordering,
};
#[cfg(nightly)] type ListAbs<T> = std::collections::LinkedList<T>;
#[cfg(not(nightly))] type ListAbs<T> = Vec<T>;
@ -13,8 +9,6 @@ pub struct TaskList {
list: ListAbs<(usize, String)>,
buffer: String,
index: usize,
poisoned: AtomicBool,
}
fn find<T,F>(list: &ListAbs<T>, mut fun: F) -> Option<usize>
@ -38,8 +32,6 @@ impl TaskList
list: ListAbs::new(),
buffer: String::new(),
index: 0,
poisoned: AtomicBool::new(false),
}
}
@ -52,15 +44,10 @@ impl TaskList
/// Push a new task string, and return its ID.
pub fn push(&mut self, string: impl Into<String>) -> usize
{
if self.poisoned.load(Ordering::SeqCst) {
return 0;
}
let idx = {
self.index+=1;
self.index
};
#[cfg(nightly)] self.list.push_back((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.
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)
{
self.list.remove(idx);
@ -85,10 +68,6 @@ impl TaskList
}
}
pub fn poison(&self) {
self.poisoned.store(true, Ordering::SeqCst);
}
/// Clear the `TaskList`
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(),
buffer: Default::default(),
index: 0,
poisoned: AtomicBool::new(false),
};
this.index = i;
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>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
T: AsRef<OsStr> + Send + Sync + 'static + Clone,
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 semaphore = children.map(|children| Arc::new(Semaphore::new(children.into())));
let process = Arc::new(Process::new(process, flags.leanify_flags.clone()));
@ -177,18 +161,15 @@ where I: IntoIterator<Item=T>,
};
let mut i=0usize;
let results =
pass_cancel!(join_all(
join_all(
files
.map(|filename| {
let semaphore = semaphore.clone();
let process = Arc::clone(&process);
let mut tx = tx.clone();
let cancel = cancel.clone();
#[cfg(feature="progress")] let mut progress = progress.clone();
//TODO: Where to put the cancellation check in here?? (XXX: Current
//`AbortHandle` (old tokio version) does not have `is_aborted()`... :/)
(tokio::spawn(async move {
#[cfg(feature="progress")] type Opt<T> = OptionFuture<T>;
#[cfg(not(feature="progress"))] type Opt<T> = std::marker::PhantomData<T>;
@ -251,23 +232,9 @@ where I: IntoIterator<Item=T>,
let _ = progress.bump_min(1).await;
}
}),i+=1).0
}))).await
.map(|x| x
})).await
.into_iter()
.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,
};
.filter_map(|x| x.err());
#[cfg(feature="progress")] progress.shutdown().await?;
for failed in results

Loading…
Cancel
Save