Skip to content

Commit

Permalink
Avoid a lock around replace_if
Browse files Browse the repository at this point in the history
  • Loading branch information
Swatinem committed Oct 3, 2023
1 parent d1aa41f commit 9950704
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
8 changes: 2 additions & 6 deletions src/future/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{
#[cfg(feature = "unstable-debug-counters")]
use crate::common::concurrent::debug_counters::CacheDebugStats;

use async_lock::Mutex;
use async_trait::async_trait;
use std::{
borrow::Borrow,
Expand Down Expand Up @@ -1678,7 +1677,6 @@ where
None
};

let replace_if = Arc::new(Mutex::new(replace_if));
let type_id = ValueInitializer::<K, V, S>::type_id_for_get_with();
let post_init = ValueInitializer::<K, V, S>::post_init_for_get_with;

Expand Down Expand Up @@ -1806,13 +1804,12 @@ where
None
};

let ignore_if = Arc::new(Mutex::new(never_ignore()));
let type_id = ValueInitializer::<K, V, S>::type_id_for_optionally_get_with();
let post_init = ValueInitializer::<K, V, S>::post_init_for_optionally_get_with;

match self
.value_initializer
.try_init_or_read(&key, hash, type_id, self, ignore_if, init, post_init)
.try_init_or_read(&key, hash, type_id, self, never_ignore(), init, post_init)
.await
{
InitResult::Initialized(v) => {
Expand Down Expand Up @@ -1889,13 +1886,12 @@ where
None
};

let ignore_if = Arc::new(Mutex::new(never_ignore()));
let type_id = ValueInitializer::<K, V, S>::type_id_for_try_get_with::<E>();
let post_init = ValueInitializer::<K, V, S>::post_init_for_try_get_with;

match self
.value_initializer
.try_init_or_read(&key, hash, type_id, self, ignore_if, init, post_init)
.try_init_or_read(&key, hash, type_id, self, never_ignore(), init, post_init)
.await
{
InitResult::Initialized(v) => {
Expand Down
6 changes: 3 additions & 3 deletions src/future/value_initializer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use async_lock::{Mutex, RwLock, RwLockWriteGuard};
use async_lock::{RwLock, RwLockWriteGuard};
use async_trait::async_trait;
use futures_util::FutureExt;
use std::{
Expand Down Expand Up @@ -150,7 +150,7 @@ where
c_hash: u64,
type_id: TypeId,
cache: &C,
ignore_if: Arc<Mutex<Option<I>>>,
mut ignore_if: Option<I>,
// Future to initialize a new value.
init: Pin<&mut impl Future<Output = O>>,
// Function to convert a value O, returned from the init future, into
Expand Down Expand Up @@ -219,7 +219,7 @@ where

// Check if the value has already been inserted by other thread.
if let Some(value) = cache
.get_without_recording(c_key, c_hash, ignore_if.lock().await.as_mut())
.get_without_recording(c_key, c_hash, ignore_if.as_mut())
.await
{
// Yes. Set the waiter value, remove our waiter, and return
Expand Down

0 comments on commit 9950704

Please sign in to comment.