fix lock order, take snapshot after taking lock

Signed-off-by: Alex Chi Z <chi@neon.tech>
This commit is contained in:
Alex Chi Z
2023-07-21 15:23:28 -04:00
parent f726063d2f
commit 09c33fd0a4

View File

@@ -68,23 +68,31 @@ impl LayerManager {
}
}
/// Take the snapshot of the layer map and return a read guard. The read guard will prevent
/// the layer map from being modified.
pub async fn read(&self) -> LayerManagerReadGuard {
// take the lock before taking snapshot
let pseudo_lock = self.pseudo_lock.clone().read_owned().await;
LayerManagerReadGuard {
snapshot: LayerSnapshot {
layer_map: self.layer_map.load_full(),
layer_fmgr: Arc::clone(&self.layer_fmgr),
},
pseudo_lock: self.pseudo_lock.clone().read_owned().await,
pseudo_lock,
}
}
/// Take the snapshot of the layer map and return a write guard. The read guard will prevent
/// the layer map from being read.
pub async fn write(self: &Arc<Self>) -> LayerManagerWriteGuard {
// take the lock before taking snapshot
let pseudo_lock = self.pseudo_lock.clone().write_owned().await;
LayerManagerWriteGuard {
snapshot: LayerSnapshot {
layer_map: self.layer_map.load_full(),
layer_fmgr: Arc::clone(&self.layer_fmgr),
},
pseudo_lock: self.pseudo_lock.clone().write_owned().await,
pseudo_lock,
layer_manager: self.clone(),
}
}