Skip to content

Commit

Permalink
Use parking_lot Mutex where possible
Browse files Browse the repository at this point in the history
parking_lot is a faster and more convenient alternative to std::sync::Mutex.
We can remove the annoying expect calls, as we don't care about it being poisoned.

It is used by many 3rd-party crates we use already, so it's not an additional dependency.
In addition, we can enable deadlock detection with this library
(done in a follow-up commit, just in case it adds too much overhead).

rust-lightning needs std::sync::Mutex, so leave these instances unchanged.
  • Loading branch information
klochowicz committed Jun 8, 2023
1 parent ce8fd73 commit 238ac9d
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 21 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coordinator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ local-ip-address = "0.5.1"
# adding this as explicit dependency as we need the "vendored" flag for cross compilation
openssl = { version = "0.10.45", features = ["vendored"] }
orderbook-commons = { path = "../crates/orderbook-commons" }
parking_lot = "0.12.1"
rand = "0.8.5"
rust_decimal = { version = "1", features = ["serde-with-float"] }
serde = "1.0.147"
Expand Down
11 changes: 2 additions & 9 deletions coordinator/src/orderbook/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ pub async fn post_order(
let matched_orders = match_order(order.clone(), all_non_expired_orders)
.map_err(|e| AppError::InternalServerError(format!("Failed to match order: {e:#}")))?;

let authenticated_users = state
.authenticated_users
.lock()
.expect("mutex not to be poisoned")
.clone();
let authenticated_users = state.authenticated_users.lock().clone();
match matched_orders {
Some(matched_orders) => {
let mut orders_to_set_taken = vec![matched_orders.taker_matches.filled_with.order_id];
Expand Down Expand Up @@ -321,10 +317,7 @@ async fn websocket(stream: WebSocket, state: Arc<AppState>) {
return;
}

let mut authenticated_users = state
.authenticated_users
.lock()
.expect("Mutex not to be poisoned");
let mut authenticated_users = state.authenticated_users.lock();
authenticated_users.insert(pubkey, local_sender.clone());
}
Err(err) => {
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ use diesel::PgConnection;
use ln_dlc_node::node::NodeInfo;
use orderbook_commons::FakeScidResponse;
use orderbook_commons::OrderbookMsg;
use parking_lot::Mutex;
use serde::Deserialize;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use std::sync::Mutex;
use tokio::sync::broadcast;
use tokio::sync::mpsc;
use tokio::sync::RwLock;
Expand Down
1 change: 1 addition & 0 deletions crates/ln-dlc-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lightning-persister = { version = "0.0.114" }
lightning-transaction-sync = { version = "0.0.114", features = ["esplora-blocking"] }
log = "0.4.17"
p2pd-oracle-client = { version = "0.1.0" }
parking_lot = "0.12.1"
rand = "0.8.5"
reqwest = { version = "0.11", default-features = false, features = ["json"] }
rust-bitcoin-coin-selection = { version = "0.1.0", features = ["rand"] }
Expand Down
8 changes: 3 additions & 5 deletions crates/ln-dlc-node/src/dlc_custom_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ use lightning::ln::chan_utils::ChannelPublicKeys;
use lightning::ln::msgs::DecodeError;
use lightning::ln::script::ShutdownScript;
use lightning::util::ser::Writeable;
use parking_lot::Mutex;
use parking_lot::MutexGuard;
use secp256k1_zkp::ecdsa::RecoverableSignature;
use secp256k1_zkp::Secp256k1;
use secp256k1_zkp::SecretKey;
use secp256k1_zkp::Signing;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;

pub struct CustomSigner {
in_memory_signer: Arc<Mutex<InMemorySigner>>,
Expand All @@ -46,9 +46,7 @@ impl CustomSigner {
}

fn in_memory_signer_lock(&self) -> MutexGuard<InMemorySigner> {
self.in_memory_signer
.lock()
.expect("Mutex to not be poisoned")
self.in_memory_signer.lock()
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/ln-dlc-node/src/ldk_node_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use lightning::chain::transaction::OutPoint;
use lightning::chain::Filter;
use lightning::chain::WatchedOutput;
use lightning_transaction_sync::EsploraSyncClient;
use parking_lot::Mutex;
use parking_lot::MutexGuard;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use tokio::sync::RwLock;

pub struct Wallet<D>
Expand Down Expand Up @@ -70,7 +70,7 @@ where
}

fn bdk_lock(&self) -> MutexGuard<bdk::Wallet<D>> {
self.inner.lock().expect("mutex not to be poisoned")
self.inner.lock()
}

pub async fn update_settings(&self, settings: WalletSettings) {
Expand Down
1 change: 1 addition & 0 deletions mobile/native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ln-dlc-node = { path = "../../crates/ln-dlc-node" }
openssl = { version = "0.10.45", features = ["vendored"] }
orderbook-client = { path = "../../crates/orderbook-client" }
orderbook-commons = { path = "../../crates/orderbook-commons" }
parking_lot = "0.12.1"
reqwest = { version = "0.11", default-features = false, features = ["json"] }
rust_decimal = { version = "1", features = ["serde-with-float"] }
serde = { version = "1.0.152", features = ["serde_derive"] }
Expand Down
5 changes: 2 additions & 3 deletions mobile/native/src/event/event_hub.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::event::subscriber::Subscriber;
use crate::event::EventInternal;
use crate::event::EventType;
use parking_lot::Mutex;
use parking_lot::MutexGuard;
use state::Storage;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use std::vec;

static EVENT_HUB: Storage<Arc<Mutex<EventHub>>> = Storage::new();
Expand All @@ -19,7 +19,6 @@ pub(crate) fn get() -> MutexGuard<'static, EventHub> {
}))
})
.lock()
.expect("failed to get lock on event hub")
}

pub struct EventHub {
Expand Down

0 comments on commit 238ac9d

Please sign in to comment.