Skip to content

Commit

Permalink
refactor: remove parking-lot dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 12, 2024
1 parent d9fb470 commit 11fec02
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 77 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion iroh-dns-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ http = "1.0.0"
humantime-serde = "1.1.1"
iroh-metrics = { version = "0.29.0" }
lru = "0.12.3"
parking_lot = "0.12.1"
pkarr = { version = "2.2.0", features = [ "async", "relay", "dht"], default-features = false }
rcgen = "0.13"
redb = "2.0.0"
Expand Down
1 change: 0 additions & 1 deletion iroh-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ iroh-metrics = { version = "0.29.0", default-features = false }
libc = "0.2.139"
num_enum = "0.7"
once_cell = "1.18.0"
parking_lot = "0.12.1"
pin-project = "1"
postcard = { version = "1", default-features = false, features = [
"alloc",
Expand Down
1 change: 0 additions & 1 deletion iroh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ netdev = "0.31.0"
netwatch = { version = "0.2.0" }
num_enum = "0.7"
once_cell = "1.18.0"
parking_lot = "0.12.1"
pin-project = "1"
pkarr = { version = "2", default-features = false, features = [
"async",
Expand Down
6 changes: 3 additions & 3 deletions iroh/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,11 @@ mod tests {
use std::{
collections::{BTreeSet, HashMap},
net::SocketAddr,
sync::Arc,
sync::{Arc, Mutex},
time::SystemTime,
};

use iroh_base::SecretKey;
use parking_lot::Mutex;
use rand::Rng;
use tokio_util::task::AbortOnDropHandle;

Expand Down Expand Up @@ -499,6 +498,7 @@ mod tests {
self.shared
.nodes
.lock()
.unwrap()
.insert(self.node_id, (url.cloned(), addrs.clone(), now));
}

Expand All @@ -508,7 +508,7 @@ mod tests {
node_id: NodeId,
) -> Option<BoxStream<Result<DiscoveryItem>>> {
let addr_info = match self.resolve_wrong {
false => self.shared.nodes.lock().get(&node_id).cloned(),
false => self.shared.nodes.lock().unwrap().get(&node_id).cloned(),
true => {
let ts = system_time_now() - 100_000;
let port: u16 = rand::thread_rng().gen_range(10_000..20_000);
Expand Down
51 changes: 27 additions & 24 deletions iroh/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pub(crate) struct MagicSock {
///
/// This waker is used by [`IoPoller`] and the [`RelayActor`] to signal when more
/// datagrams can be sent to the relays.
relay_send_waker: Arc<parking_lot::Mutex<Option<Waker>>>,
relay_send_waker: Arc<std::sync::Mutex<Option<Waker>>>,
/// Counter for ordering of [`MagicSock::poll_recv`] polling order.
poll_recv_counter: AtomicUsize,

Expand Down Expand Up @@ -236,7 +236,7 @@ pub(crate) struct MagicSock {

/// List of CallMeMaybe disco messages that should be sent out after the next endpoint update
/// completes
pending_call_me_maybes: parking_lot::Mutex<HashMap<PublicKey, RelayUrl>>,
pending_call_me_maybes: std::sync::Mutex<HashMap<PublicKey, RelayUrl>>,

/// Indicates the direct addr update state.
direct_addr_update_state: DirectAddrUpdateState,
Expand Down Expand Up @@ -1342,7 +1342,7 @@ impl MagicSock {
fn send_queued_call_me_maybes(&self) {
let msg = self.direct_addrs.to_call_me_maybe_message();
let msg = disco::Message::CallMeMaybe(msg);
for (public_key, url) in self.pending_call_me_maybes.lock().drain() {
for (public_key, url) in self.pending_call_me_maybes.lock().unwrap().drain() {
if !self.send_disco_message_relay(&url, public_key, msg.clone()) {
warn!(node = %public_key.fmt_short(), "relay channel full, dropping call-me-maybe");
}
Expand All @@ -1369,6 +1369,7 @@ impl MagicSock {
Err(last_refresh_ago) => {
self.pending_call_me_maybes
.lock()
.unwrap()
.insert(dst_node, url.clone());
debug!(
?last_refresh_ago,
Expand Down Expand Up @@ -1447,7 +1448,7 @@ struct DirectAddrUpdateState {
/// If running, set to the reason for the currently the update.
running: sync::watch::Sender<Option<&'static str>>,
/// If set, start a new update as soon as the current one is finished.
want_update: parking_lot::Mutex<Option<&'static str>>,
want_update: std::sync::Mutex<Option<&'static str>>,
}

impl DirectAddrUpdateState {
Expand All @@ -1463,7 +1464,7 @@ impl DirectAddrUpdateState {
/// scheduling it for later.
fn schedule_run(&self, why: &'static str) {
if self.is_running() {
let _ = self.want_update.lock().insert(why);
let _ = self.want_update.lock().unwrap().insert(why);
} else {
self.run(why);
}
Expand All @@ -1486,7 +1487,7 @@ impl DirectAddrUpdateState {

/// Returns the next update, if one is set.
fn next_update(&self) -> Option<&'static str> {
self.want_update.lock().take()
self.want_update.lock().unwrap().take()
}
}

Expand Down Expand Up @@ -1559,7 +1560,7 @@ impl Handle {
closing: AtomicBool::new(false),
closed: AtomicBool::new(false),
relay_datagrams_queue: relay_datagrams_queue.clone(),
relay_send_waker: Arc::new(parking_lot::Mutex::new(None)),
relay_send_waker: Arc::new(std::sync::Mutex::new(None)),
poll_recv_counter: AtomicUsize::new(0),
actor_sender: actor_sender.clone(),
ipv6_reported: Arc::new(AtomicBool::new(false)),
Expand Down Expand Up @@ -1676,19 +1677,18 @@ impl Handle {
}

#[derive(Debug, Default)]
struct DiscoSecrets(parking_lot::Mutex<HashMap<PublicKey, SharedSecret>>);
struct DiscoSecrets(std::sync::Mutex<HashMap<PublicKey, SharedSecret>>);

impl DiscoSecrets {
fn get(
&self,
secret: &SecretKey,
node_id: PublicKey,
) -> parking_lot::MappedMutexGuard<SharedSecret> {
parking_lot::MutexGuard::map(self.0.lock(), |inner| {
inner
.entry(node_id)
.or_insert_with(|| secret.shared(&node_id))
})
fn get<F, T>(&self, secret: &SecretKey, node_id: PublicKey, cb: F) -> T
where
F: FnOnce(&mut SharedSecret) -> T,
{
let mut inner = self.0.lock().unwrap();
let x = inner
.entry(node_id)
.or_insert_with(|| secret.shared(&node_id));
cb(x)
}

pub fn encode_and_seal(
Expand All @@ -1698,7 +1698,7 @@ impl DiscoSecrets {
msg: &disco::Message,
) -> Bytes {
let mut seal = msg.as_bytes();
self.get(secret_key, node_id).seal(&mut seal);
self.get(secret_key, node_id, |secret| secret.seal(&mut seal));
disco::encode_message(&secret_key.public(), seal).into()
}

Expand All @@ -1708,9 +1708,9 @@ impl DiscoSecrets {
node_id: PublicKey,
mut sealed_box: Vec<u8>,
) -> Result<disco::Message, DiscoBoxError> {
self.get(secret, node_id)
.open(&mut sealed_box)
.map_err(DiscoBoxError::Open)?;
self.get(secret, node_id, |secret| {
secret.open(&mut sealed_box).map_err(DiscoBoxError::Open)
})?;
disco::Message::from_bytes(&sealed_box).map_err(DiscoBoxError::Parse)
}
}
Expand Down Expand Up @@ -1871,7 +1871,7 @@ struct IoPoller {
ipv4_poller: Pin<Box<dyn quinn::UdpPoller>>,
ipv6_poller: Option<Pin<Box<dyn quinn::UdpPoller>>>,
relay_sender: mpsc::Sender<RelayActorMessage>,
relay_send_waker: Arc<parking_lot::Mutex<Option<Waker>>>,
relay_send_waker: Arc<std::sync::Mutex<Option<Waker>>>,
}

impl quinn::UdpPoller for IoPoller {
Expand All @@ -1890,7 +1890,10 @@ impl quinn::UdpPoller for IoPoller {
}
match this.relay_sender.capacity() {
0 => {
self.relay_send_waker.lock().replace(cx.waker().clone());
self.relay_send_waker
.lock()
.unwrap()
.replace(cx.waker().clone());
Poll::Pending
}
_ => Poll::Ready(Ok(())),
Expand Down
Loading

0 comments on commit 11fec02

Please sign in to comment.