mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-08-02 09:12:16 +00:00
impl sync/send for cache
This commit is contained in:
parent
97dd95abea
commit
b43e8e46f6
1 changed files with 27 additions and 0 deletions
|
@ -137,6 +137,8 @@ pub struct DumbLruPageCache {
|
||||||
head: RefCell<Option<NonNull<PageCacheEntry>>>,
|
head: RefCell<Option<NonNull<PageCacheEntry>>>,
|
||||||
tail: RefCell<Option<NonNull<PageCacheEntry>>>,
|
tail: RefCell<Option<NonNull<PageCacheEntry>>>,
|
||||||
}
|
}
|
||||||
|
unsafe impl Send for DumbLruPageCache {}
|
||||||
|
unsafe impl Sync for DumbLruPageCache {}
|
||||||
|
|
||||||
impl DumbLruPageCache {
|
impl DumbLruPageCache {
|
||||||
pub fn new(capacity: usize) -> Self {
|
pub fn new(capacity: usize) -> Self {
|
||||||
|
@ -675,3 +677,28 @@ pub fn allocate_page(page_id: usize, buffer_pool: &Rc<BufferPool>, offset: usize
|
||||||
}
|
}
|
||||||
page
|
page
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
use super::{DumbLruPageCache, Page};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_shared_cache() {
|
||||||
|
// ensure cache can be shared between threads
|
||||||
|
let cache = Arc::new(RwLock::new(DumbLruPageCache::new(10)));
|
||||||
|
|
||||||
|
let thread = {
|
||||||
|
let cache = cache.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
let mut cache = cache.write().unwrap();
|
||||||
|
cache.insert(1, Arc::new(Page::new(1)));
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let _ = thread.join();
|
||||||
|
let mut cache = cache.write().unwrap();
|
||||||
|
let page = cache.get(&1);
|
||||||
|
assert_eq!(page.unwrap().get().id, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue