Mercurial > lbo > hg > sstable
changeset 90:5301c7548da3
Force implementation of Send and Sync for Cache
author | Thomas Krause <krauseto@hu-berlin.de> |
---|---|
date | Mon, 17 Feb 2020 19:36:18 +0100 |
parents | e7266b1e9bde |
children | 1fa0d7075169 |
files | src/cache.rs src/types.rs |
diffstat | 2 files changed, 13 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/cache.rs Mon Feb 17 18:38:05 2020 +0100 +++ b/src/cache.rs Mon Feb 17 19:36:18 2020 +0100 @@ -152,6 +152,7 @@ } } + pub type CacheKey = [u8; 16]; pub type CacheID = u64; type CacheEntry<T> = (T, LRUHandle<CacheKey>); @@ -237,6 +238,16 @@ } } + +// The compiler does not automatically derive Send and Sync for Cache because it contains +// raw pointers. +// These raw pointers are only pointing to the elements hold in the same cache and insertion +// clones the values. It is therefore safe to implement Send for Cache. +// Since all functions that access these raw pointers are mutable member functions, it is also safe to implement Sync +// (Sync is defined as "if &T is Send-able") +unsafe impl<T: Send> Send for Cache<T> {} +unsafe impl<T: Sync> Sync for Cache<T> {} + #[cfg(test)] mod tests { use super::LRUList;
--- a/src/types.rs Mon Feb 17 18:38:05 2020 +0100 +++ b/src/types.rs Mon Feb 17 19:36:18 2020 +0100 @@ -10,7 +10,7 @@ use std::sync::Arc; use std::sync::RwLock; -pub trait RandomAccess : Send { +pub trait RandomAccess : Send + Sync { fn read_at(&self, off: usize, dst: &mut [u8]) -> Result<usize>; } @@ -48,7 +48,7 @@ } } -/// A shared thingy with interior mutability. +/// A shared thingy with guarded by a lock. pub type Shared<T> = Arc<RwLock<T>>; pub fn share<T>(t: T) -> Arc<RwLock<T>> {