Skip to main content

pagers_core/ops/
lock.rs

1use std::sync::Arc;
2
3use memmap2::Mmap;
4
5use crate::mincore::PageMap;
6
7use super::touch::Touch;
8use super::{FileContext, Op, ResidencyEffect};
9use crate::mlock;
10
11/// Touch pages into cache, then lock them in physical memory with mlock(2).
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct Lock;
15
16/// Holds the mmap alive after mlock — dropping this unmaps and unlocks.
17#[derive(Debug)]
18pub struct LockedFile {
19    _mmap: Arc<Mmap>,
20    pub pages_touched: usize,
21}
22
23impl Op for Lock {
24    const LABEL: &str = "locked";
25    const EFFECT: ResidencyEffect = ResidencyEffect::Populate;
26    type Output = LockedFile;
27
28    fn execute<PM: PageMap + Sync>(&self, ctx: &FileContext<'_, PM>) -> crate::Result<LockedFile> {
29        let pages_touched = Touch.execute(ctx)?;
30        mlock::mlock(ctx.mmap(), ctx.len())?;
31        Ok(LockedFile {
32            _mmap: ctx.mapping(),
33            pages_touched,
34        })
35    }
36}