Skip to content

Commit

Permalink
Use tokio::sync::RwLock rather than futures::lock::Mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelff committed Nov 29, 2023
1 parent 04d6c7b commit 7b31e86
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use super::*;
use futures::lock::Mutex;
use once_cell::sync::Lazy;
use serde::de::DeserializeOwned;
use std::{
fmt::Display,
io::Write as _,
sync::{atomic::Ordering, Arc},
};
use tokio::sync::{mpsc, oneshot};
use tokio::sync::{mpsc, oneshot, RwLock};

type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

Expand Down Expand Up @@ -103,23 +102,23 @@ impl ExecutorProcess {
/// asynchronous code are translated to an abort trap by wasm-bindgen, which kills the node process.
#[derive(Clone)]
pub(crate) struct RestartableExecutorProcess {
process: Arc<Mutex<ExecutorProcess>>,
process: Arc<RwLock<ExecutorProcess>>,
}

impl RestartableExecutorProcess {
fn new() -> Self {
Self {
process: Arc::new(Mutex::new(ExecutorProcess::spawn())),
process: Arc::new(RwLock::new(ExecutorProcess::spawn())),
}
}

async fn restart(&self) {
let mut process = self.process.lock().await;
_ = std::mem::replace(&mut *process, ExecutorProcess::spawn());
let mut process = self.process.write().await;
*process = ExecutorProcess::spawn();
}

pub(crate) async fn request<T: DeserializeOwned>(&self, method: &str, params: serde_json::Value) -> Result<T> {
let p = self.process.lock().await;
let p = self.process.read().await;
p.request(method, params).await
}
}
Expand Down

0 comments on commit 7b31e86

Please sign in to comment.