Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a send_guard feature defaulting to enabled. #179

Merged
merged 3 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ keywords = ["atomic", "concurrent", "hashmap"]
categories = ["concurrency", "algorithms", "data-structures"]

[features]
default = []
default = ["send_guard"]
raw-api = []
send_guard = ["parking_lot/send_guard"]

[dependencies]
num_cpus = "1.13.0"
parking_lot = { version = "0.11.2", features = ["send_guard"] }
parking_lot = "0.11.2"
serde = { version = "1.0.131", optional = true, features = ["derive"] }
cfg-if = "1.0.0"
rayon = { version = "1.5.1", optional = true }

[package.metadata.docs.rs]
features = ["rayon", "raw-api", "serde"]
features = ["rayon", "raw-api", "serde", "send_guard"]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ If you have any suggestions or tips do not hesitate to open an issue or a PR.

- `rayon` - Enables rayon support.

- `send_guard` - Enables the `send_guard` feature of `parking_lot`, making `Ref*` guards `Send`. This is on by default.

## Contributing

DashMap gladly accepts contributions!
Expand Down
18 changes: 14 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,19 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
self.shards.get_unchecked(i).write()
}

unsafe fn _try_yield_read_shard(&'a self, i: usize) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>> {
unsafe fn _try_yield_read_shard(
&'a self,
i: usize,
) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>> {
debug_assert!(i < self.shards.len());

self.shards.get_unchecked(i).try_read()
}

unsafe fn _try_yield_write_shard(&'a self, i: usize) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>> {
unsafe fn _try_yield_write_shard(
&'a self,
i: usize,
) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>> {
debug_assert!(i < self.shards.len());

self.shards.get_unchecked(i).try_write()
Expand Down Expand Up @@ -906,7 +912,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
fn _try_get_mut<Q>(&'a self, key: &Q) -> TryResult<RefMut<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);

Expand Down Expand Up @@ -1013,7 +1019,11 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>

let vptr = &mut *vptr.as_ptr();

Some(Entry::Occupied(OccupiedEntry::new(shard, key, (kptr, vptr))))
Some(Entry::Occupied(OccupiedEntry::new(
shard,
key,
(kptr, vptr),
)))
}
} else {
Some(Entry::Vacant(VacantEntry::new(shard, key)))
Expand Down
10 changes: 8 additions & 2 deletions src/t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ pub trait Map<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + Clone + BuildHasher> {
/// # Safety
///
/// The index must not be out of bounds.
unsafe fn _try_yield_read_shard(&'a self, i: usize) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>>;
unsafe fn _try_yield_read_shard(
&'a self,
i: usize,
) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>>;

/// # Safety
///
/// The index must not be out of bounds.
unsafe fn _try_yield_write_shard(&'a self, i: usize) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>>;
unsafe fn _try_yield_write_shard(
&'a self,
i: usize,
) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>>;

fn _insert(&self, key: K, value: V) -> Option<V>;

Expand Down
19 changes: 3 additions & 16 deletions src/try_result.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/// Represents the result of a non-blocking read from a [DashMap](crate::DashMap).
#[derive(Debug)]
pub enum TryResult<R> {
Expand All @@ -12,30 +11,18 @@ pub enum TryResult<R> {

impl<R> TryResult<R> {
/// Returns `true` if the value was present in the map, and the lock for the shard was successfully obtained.
#[inline]
pub fn is_present(&self) -> bool {
match self {
TryResult::Present(_) => true,
_ => false,
}
matches!(self, TryResult::Present(_))
}

/// Returns `true` if the shard wasn't locked, and the value wasn't present in the map.
#[inline]
pub fn is_absent(&self) -> bool {
match self {
TryResult::Absent => true,
_ => false,
}
matches!(self, TryResult::Absent)
}

/// Returns `true` if the shard was locked.
#[inline]
pub fn is_locked(&self) -> bool {
match self {
TryResult::Locked => true,
_ => false,
}
matches!(self, TryResult::Locked)
}

/// If `self` is [Present](TryResult::Present), returns the reference to the value in the map.
Expand Down