Skip to content

Commit

Permalink
Loom RwLock requires Sized
Browse files Browse the repository at this point in the history
  • Loading branch information
MattHalpinParity committed Oct 29, 2023
1 parent 08ae7d1 commit 3d263d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
8 changes: 4 additions & 4 deletions admin/src/multitree_bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ fn read_value(
}

struct TreeReaderRef<'d> {
reader_ref: Arc<RwLock<dyn TreeReader + 'd>>,
reader_ref: Arc<RwLock<Box<dyn TreeReader + Send + Sync + 'd>>>,
}

impl<'d> TreeReaderRef<'d> {
Expand All @@ -794,11 +794,11 @@ impl<'d> TreeReaderRef<'d> {
}

struct TreeReaderGuard<'s, 'd: 's> {
lock_guard: RwLockReadGuard<'s, dyn TreeReader + 'd>,
lock_guard: RwLockReadGuard<'s, Box<dyn TreeReader + Send + Sync + 'd>>,
}

impl<'s, 'd: 's> Deref for TreeReaderGuard<'s, 'd> {
type Target = dyn TreeReader + 'd;
type Target = Box<dyn TreeReader + Send + Sync + 'd>;

fn deref(&self) -> &Self::Target {
self.lock_guard.deref()
Expand Down Expand Up @@ -987,7 +987,7 @@ fn iter_children<'a>(
depth: u32,
generated_children: &mut Vec<NodeSpec>,
database_children: &mut Vec<u64>,
reader: &RwLockReadGuard<'a, dyn TreeReader>,
reader: &RwLockReadGuard<'a, Box<dyn TreeReader + Send + Sync>>,
chain_generator: &ChainGenerator,
) -> Result<(), String> {
for i in 0..generated_children.len() {
Expand Down
17 changes: 11 additions & 6 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct CommitQueue {

#[derive(Debug)]
struct Trees {
readers: HashMap<Key, Weak<RwLock<DbTreeReader>>, IdentityBuildHasher>,
readers: HashMap<Key, Weak<RwLock<Box<dyn TreeReader + Send + Sync>>>, IdentityBuildHasher>,
/// Number of queued dereferences for each tree
to_dereference: HashMap<Key, usize>,
}
Expand Down Expand Up @@ -376,7 +376,7 @@ impl DbInner {
col: ColId,
key: &[u8],
check_existence: bool,
) -> Result<Option<Arc<RwLock<dyn TreeReader>>>> {
) -> Result<Option<Arc<RwLock<Box<dyn TreeReader + Send + Sync>>>>> {
if !self.options.columns[col as usize].multitree {
return Err(Error::InvalidConfiguration("Not a multitree column.".to_string()))
}
Expand Down Expand Up @@ -411,8 +411,9 @@ impl DbInner {
to_dereference: Default::default(),
});

let reader =
Arc::new(RwLock::new(DbTreeReader { db: db.clone(), col, key: hash_key }));
let reader: Box<dyn TreeReader + Send + Sync> =
Box::new(DbTreeReader { db: db.clone(), col, key: hash_key });
let reader = Arc::new(RwLock::new(reader));

column_trees.readers.insert(hash_key, Arc::downgrade(&reader));

Expand Down Expand Up @@ -1470,7 +1471,11 @@ impl Db {
self.inner.btree_iter(col)
}

pub fn get_tree(&self, col: ColId, key: &[u8]) -> Result<Option<Arc<RwLock<dyn TreeReader>>>> {
pub fn get_tree(
&self,
col: ColId,
key: &[u8],
) -> Result<Option<Arc<RwLock<Box<dyn TreeReader + Send + Sync>>>>> {
self.inner.get_tree(&self.inner, col, key, true)
}

Expand Down Expand Up @@ -2178,7 +2183,7 @@ impl IndexedChangeSet {
fn write_dereference_children_plan(
&self,
column: &HashColumn,
guard: &RwLockWriteGuard<'_, dyn TreeReader>,
guard: &RwLockWriteGuard<'_, Box<dyn TreeReader + Send + Sync>>,
children: &Vec<u64>,
num_removed: &mut u64,
writer: &mut crate::log::LogWriter,
Expand Down
4 changes: 4 additions & 0 deletions src/parking_lot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ mod with_loom {
pub fn write(&self) -> RwLockWriteGuard<T> {
RwLockWriteGuard(self.0.write().unwrap())
}

pub fn is_locked(&self) -> bool {
!self.0.try_write().is_ok()
}
}

#[derive(Debug)]
Expand Down

0 comments on commit 3d263d7

Please sign in to comment.