mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-23 20:11:06 +00:00
Replace Condvars with blocking channel (#5815)
* remove condvars * return false on lost sender * fix * Update primitives/io/src/batch_verifier.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -114,8 +114,6 @@ impl BatchVerifier {
|
|||||||
/// aggregated result.
|
/// aggregated result.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn verify_and_clear(&mut self) -> bool {
|
pub fn verify_and_clear(&mut self) -> bool {
|
||||||
use std::sync::{Mutex, Condvar};
|
|
||||||
|
|
||||||
let pending = std::mem::replace(&mut self.pending_tasks, vec![]);
|
let pending = std::mem::replace(&mut self.pending_tasks, vec![]);
|
||||||
let started = std::time::Instant::now();
|
let started = std::time::Instant::now();
|
||||||
|
|
||||||
@@ -139,14 +137,12 @@ impl BatchVerifier {
|
|||||||
self.sr25519_items.clear();
|
self.sr25519_items.clear();
|
||||||
|
|
||||||
if pending.len() > 0 {
|
if pending.len() > 0 {
|
||||||
let pair = Arc::new((Mutex::new(false), Condvar::new()));
|
let (sender, receiver) = std::sync::mpsc::channel();
|
||||||
let pair_clone = pair.clone();
|
|
||||||
|
|
||||||
if self.scheduler.spawn_obj(FutureObj::new(async move {
|
if self.scheduler.spawn_obj(FutureObj::new(async move {
|
||||||
futures::future::join_all(pending).await;
|
futures::future::join_all(pending).await;
|
||||||
let mut finished = pair_clone.0.lock().expect("Locking can only fail when the mutex is poisoned; qed");
|
sender.send(())
|
||||||
*finished = true;
|
.expect("Channel never panics if receiver is live. \
|
||||||
pair_clone.1.notify_all();
|
Receiver is always live until received this data; qed. ");
|
||||||
}.boxed())).is_err() {
|
}.boxed())).is_err() {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
target: "runtime",
|
target: "runtime",
|
||||||
@@ -155,11 +151,9 @@ impl BatchVerifier {
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if receiver.recv().is_err() {
|
||||||
let (finished, cond_var) = &*pair;
|
log::warn!(target: "runtime", "Haven't received async result from verification task. Returning false.");
|
||||||
let mut finished = finished.lock().expect("Locking can only fail when the mutex is poisoned; qed");
|
return false;
|
||||||
while !*finished {
|
|
||||||
finished = cond_var.wait(finished).expect("Waiting can only fail when the mutex waited on is poisoned; qed");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user