Skip to content

Commit

Permalink
Merge pull request #179 from xacrimon/acri/secfix
Browse files Browse the repository at this point in the history
Add a send_guard feature defaulting to enabled.
  • Loading branch information
xacrimon authored Feb 6, 2022
2 parents 6587526 + e530e80 commit f4daed9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
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

0 comments on commit f4daed9

Please sign in to comment.