Commit fe742f15 authored by Benjamin Woodruff's avatar Benjamin Woodruff

Reorder generation bump and lock, rename QueueState to QueuePair

parent 749c94eb
...@@ -33,12 +33,12 @@ impl<'tx> Deref for SendRoTransaction<'tx> { ...@@ -33,12 +33,12 @@ impl<'tx> Deref for SendRoTransaction<'tx> {
// allows moving ownership between threads when queue internals move transactions across threads. // allows moving ownership between threads when queue internals move transactions across threads.
unsafe impl<'tx> Send for SendRoTransaction<'tx> {} unsafe impl<'tx> Send for SendRoTransaction<'tx> {}
struct QueueState { struct QueuePair {
sender: SyncSender<SendRoTransaction<'static>>, sender: SyncSender<SendRoTransaction<'static>>,
receiver: Arc<Mutex<Receiver<SendRoTransaction<'static>>>>, receiver: Arc<Mutex<Receiver<SendRoTransaction<'static>>>>,
} }
impl QueueState { impl QueuePair {
fn new(env: &Environment, capacity: usize, generation: u64) -> Self { fn new(env: &Environment, capacity: usize, generation: u64) -> Self {
let (sender, receiver) = sync_channel(capacity); let (sender, receiver) = sync_channel(capacity);
for _ in 0..capacity { for _ in 0..capacity {
...@@ -126,7 +126,7 @@ impl RwTransactionGuard<'_> { ...@@ -126,7 +126,7 @@ impl RwTransactionGuard<'_> {
} }
struct RebuildQueueOnDrop<'db> { struct RebuildQueueOnDrop<'db> {
queue: RwLockWriteGuard<'db, QueueState>, queue: RwLockWriteGuard<'db, QueuePair>,
env: &'db Environment, env: &'db Environment,
generation: u64, generation: u64,
capacity: usize, capacity: usize,
...@@ -137,13 +137,13 @@ impl Drop for RebuildQueueOnDrop<'_> { ...@@ -137,13 +137,13 @@ impl Drop for RebuildQueueOnDrop<'_> {
// We might avoid some allocation overhead if we used RoTransaction::reset and // We might avoid some allocation overhead if we used RoTransaction::reset and
// InactiveTransaction::renew, but we don't care much about perf here. // InactiveTransaction::renew, but we don't care much about perf here.
// https://docs.rs/lmdb/latest/lmdb/struct.RoTransaction.html#method.reset // https://docs.rs/lmdb/latest/lmdb/struct.RoTransaction.html#method.reset
*self.queue = QueueState::new(self.env, self.capacity, self.generation); *self.queue = QueuePair::new(self.env, self.capacity, self.generation);
} }
} }
pub struct TxCache { pub struct TxCache {
generation: AtomicU64, generation: AtomicU64,
queue: RwLock<QueueState>, queue: RwLock<QueuePair>,
capacity: usize, capacity: usize,
} }
...@@ -151,14 +151,14 @@ impl TxCache { ...@@ -151,14 +151,14 @@ impl TxCache {
pub fn new(env: &Environment, capacity: usize) -> Self { pub fn new(env: &Environment, capacity: usize) -> Self {
Self { Self {
generation: AtomicU64::new(0), generation: AtomicU64::new(0),
queue: RwLock::new(QueueState::new(env, capacity, 0)), queue: RwLock::new(QueuePair::new(env, capacity, 0)),
capacity, capacity,
} }
} }
pub fn get_write_tx<'db>(&'db self, env: &'db Environment) -> Result<RwTransactionGuard<'db>> { pub fn get_write_tx<'db>(&'db self, env: &'db Environment) -> Result<RwTransactionGuard<'db>> {
let generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
let queue = self.queue.write().unwrap_or_else(PoisonError::into_inner); let queue = self.queue.write().unwrap_or_else(PoisonError::into_inner);
let generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
let tx = env.begin_rw_txn()?; let tx = env.begin_rw_txn()?;
Ok(RwTransactionGuard { Ok(RwTransactionGuard {
tx, tx,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment