1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
use crate::mapref::interface::{RefInterface, RefMutInterface}; use crate::t::Map; use crate::util; use crate::DashMap; use dashmap_shard::HashMap as ShardHashMap; use fxhash::FxBuildHasher; use parking_lot::RwLockWriteGuard; use std::borrow::Borrow; use std::cell::RefCell; use std::collections::HashMap; use std::hash::Hash; fn tt_aq_shared(map: &RefCell<HashMap<u64, BorrowStatus>>, hash: u64) -> bool { let mut map = map.borrow_mut(); if let Some(status) = map.get_mut(&hash) { match status { BorrowStatus::Shared(c) => { *c += 1; true } BorrowStatus::Exclusive => false, } } else { map.insert(hash, BorrowStatus::Shared(1)); true } } pub(crate) fn tt_rl_shared(map: &RefCell<HashMap<u64, BorrowStatus>>, hash: u64) { let mut map = map.borrow_mut(); if let Some(status) = map.get_mut(&hash) { match status { BorrowStatus::Shared(c) => { *c -= 1; if *c == 0 { map.remove(&hash); } } BorrowStatus::Exclusive => unreachable!(), } } } pub(crate) fn tt_rl_exclusive(map: &RefCell<HashMap<u64, BorrowStatus>>, hash: u64) { let mut map = map.borrow_mut(); map.remove(&hash); } fn tt_aq_exclusive(map: &RefCell<HashMap<u64, BorrowStatus>>, hash: u64) -> bool { let mut map = map.borrow_mut(); if let Some(_) = map.get(&hash) { false } else { map.insert(hash, BorrowStatus::Exclusive); true } } pub(crate) enum BorrowStatus { Shared(usize), Exclusive, } pub struct Interface<'a, K: Eq + Hash, V> { base: &'a DashMap<K, V>, iic: RefCell<HashMap<usize, RwLockWriteGuard<'a, ShardHashMap<K, V, FxBuildHasher>>>>, borrows: RefCell<HashMap<u64, BorrowStatus>>, } impl<'a, K: Eq + Hash, V> Interface<'a, K, V> { pub(crate) fn new(base: &'a DashMap<K, V>) -> Self { Self { base, iic: RefCell::new(HashMap::new()), borrows: RefCell::new(HashMap::new()), } } fn ensure_has(&self, i: usize) { let mut iic = self.iic.borrow_mut(); if !iic.contains_key(&i) { let guard = unsafe { self.base._yield_write_shard(i) }; iic.insert(i, guard); } } #[inline] pub fn get<Q>(&'a self, key: &Q) -> Option<RefInterface<'a, K, V>> where K: Borrow<Q>, Q: Hash + Eq + ?Sized, { let (i, hash) = self.base.determine_map(key); if tt_aq_shared(&self.borrows, hash) { self.ensure_has(i); let iic = self.iic.borrow(); let shard = &iic[&i]; if let Some((kptr, vptr)) = shard.get_hash_nocheck_key_value(hash, key) { unsafe { let kptr = util::change_lifetime_const(kptr); let vptr = util::change_lifetime_const(vptr); Some(RefInterface::new(hash, &self.borrows, kptr, vptr)) } } else { None } } else { None } } #[inline] pub fn get_mut<Q>(&'a self, key: &Q) -> Option<RefMutInterface<'a, K, V>> where K: Borrow<Q>, Q: Hash + Eq + ?Sized, { let (i, hash) = self.base.determine_map(key); if tt_aq_exclusive(&self.borrows, hash) { self.ensure_has(i); let iic = self.iic.borrow(); let shard = &iic[&i]; if let Some((kptr, vptr)) = shard.get_hash_nocheck_key_value(hash, key) { unsafe { let kptr = util::change_lifetime_const(kptr); let vptr = util::change_lifetime_mut(util::to_mut(vptr)); Some(RefMutInterface::new(hash, &self.borrows, kptr, vptr)) } } else { None } } else { None } } }