Преглед на файлове

Migrate to parking_lot::Mutex.

Jing Yang преди 5 години
родител
ревизия
2dab9de729
променени са 2 файла, в които са добавени 9 реда и са изтрити 13 реда
  1. 1 0
      Cargo.toml
  2. 8 13
      src/network.rs

+ 1 - 0
Cargo.toml

@@ -7,5 +7,6 @@ edition = "2018"
 [dependencies]
 bytes = "0.5.6"
 futures = { version = "0.3.5", features = ["thread-pool"] }
+parking_lot = "0.11.0"
 rand = "0.7.3"
 tokio = { version = "0.2.22", features = ["rt-threaded", "time"] }

+ 8 - 13
src/network.rs

@@ -2,10 +2,11 @@ use std::collections::HashMap;
 use std::sync::mpsc::{channel, Receiver, Sender, TryRecvError};
 use std::sync::{
     atomic::{AtomicBool, Ordering},
-    Arc, Mutex,
+    Arc,
 };
 use std::time::{Duration, Instant};
 
+use parking_lot::Mutex;
 use rand::{thread_rng, Rng};
 
 use crate::{
@@ -149,9 +150,7 @@ impl Network {
 
     async fn serve_rpc(network: Arc<Mutex<Self>>, rpc: RpcOnWire) {
         let (server_result, reliable, long_reordering, long_delays) = {
-            let network = network
-                .lock()
-                .expect("Network mutex should not be poisoned");
+            let network = network.lock();
             network.increase_rpc_count();
 
             (
@@ -266,10 +265,7 @@ impl Network {
                 // trying to add / remove RPC servers, or change settings.
                 // Having a shutdown delay helps minimise lock holding.
                 if stop_timer.elapsed() >= Self::SHUTDOWN_DELAY {
-                    let locked_network = network
-                        .lock()
-                        .expect("Network mutex should not be poisoned");
-                    if !locked_network.keep_running {
+                    if !network.lock().keep_running {
                         break;
                     }
                     stop_timer = Instant::now();
@@ -298,7 +294,6 @@ impl Network {
 
             network
                 .lock()
-                .expect("Network mutex should not be poisoned")
                 .stopped
                 .store(true, Ordering::Release);
         });
@@ -333,7 +328,9 @@ impl Network {
 
 #[cfg(test)]
 mod tests {
-    use std::sync::{Barrier, MutexGuard};
+    use std::sync::Barrier;
+
+    use parking_lot::MutexGuard;
 
     use crate::test_utils::{
         junk_server::{
@@ -360,9 +357,7 @@ mod tests {
     }
 
     fn unlock<T>(network: &Arc<Mutex<T>>) -> MutexGuard<T> {
-        network
-            .lock()
-            .expect("Network mutex should not be poisoned")
+        network.lock()
     }
 
     #[test]