solana/runtime/src/accounts_index_storage.rs

72 lines
2.0 KiB
Rust
Raw Normal View History

use crate::accounts_index::IndexValue;
2021-09-12 15:14:59 -07:00
use crate::bucket_map_holder::BucketMapHolder;
use crate::waitable_condvar::WaitableCondvar;
use std::fmt::Debug;
2021-09-12 15:14:59 -07:00
use std::{
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread::{Builder, JoinHandle},
};
// eventually hold the bucket map
// Also manages the lifetime of the background processing threads.
// When this instance is dropped, it will drop the bucket map and cleanup
// and it will stop all the background threads and join them.
pub struct AccountsIndexStorage<T: IndexValue> {
2021-09-12 15:14:59 -07:00
// for managing the bg threads
exit: Arc<AtomicBool>,
wait: Arc<WaitableCondvar>,
handle: Option<JoinHandle<()>>,
// eventually the backing storage
storage: Arc<BucketMapHolder<T>>,
2021-09-12 15:14:59 -07:00
}
impl<T: IndexValue> Debug for AccountsIndexStorage<T> {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Ok(())
}
}
impl<T: IndexValue> Drop for AccountsIndexStorage<T> {
2021-09-12 15:14:59 -07:00
fn drop(&mut self) {
self.exit.store(true, Ordering::Relaxed);
self.wait.notify_all();
if let Some(x) = self.handle.take() {
x.join().unwrap()
}
}
}
impl<T: IndexValue> AccountsIndexStorage<T> {
pub fn new(bins: usize) -> AccountsIndexStorage<T> {
let storage = Arc::new(BucketMapHolder::new(bins));
2021-09-12 15:14:59 -07:00
let storage_ = storage.clone();
let exit = Arc::new(AtomicBool::default());
let exit_ = exit.clone();
let wait = Arc::new(WaitableCondvar::default());
let wait_ = wait.clone();
let handle = Some(
Builder::new()
.name("solana-index-flusher".to_string())
.spawn(move || {
storage_.background(exit_, wait_);
})
.unwrap(),
);
Self {
exit,
wait,
handle,
storage,
}
}
pub fn storage(&self) -> &Arc<BucketMapHolder<T>> {
2021-09-12 15:14:59 -07:00
&self.storage
}
}