FileLockManager

Struct FileLockManager 

Source
pub struct FileLockManager {
    locks: SpinLock<BTreeMap<FileId, Vec<FileLockEntry>>>,
}
Expand description

全局文件锁管理器

Fields§

§locks: SpinLock<BTreeMap<FileId, Vec<FileLockEntry>>>

文件锁表:FileId -> 锁列表

Implementations§

Source§

impl FileLockManager

Source

pub const fn new() -> Self

创建新的文件锁管理器

Source

pub fn test_lock( &self, dev: u64, ino: u64, start: usize, len: usize, flock: &mut Flock, pid: i32, ) -> Result<(), FsError>

测试锁(F_GETLK)

检查是否有锁会阻塞请求的锁。如果有冲突,返回冲突锁的信息。

Source

pub fn set_lock( &self, dev: u64, ino: u64, start: usize, len: usize, lock_type: LockType, pid: i32, _blocking: bool, ) -> Result<(), FsError>

设置锁(F_SETLK / F_SETLKW)

§参数
  • blocking: true 表示阻塞(F_SETLKW),false 表示非阻塞(F_SETLK)
§TODO: 实现 F_SETLKW 阻塞等待

当前实现在锁冲突时立即返回 WouldBlock,即使 blocking=true。

完整的 F_SETLKW 实现需要:

  1. 在 FileLockManager 中为每个文件维护一个 WaitQueue
  2. 锁冲突时,如果 blocking=true:
    • 将当前任务加入该文件的等待队列
    • 调用 yield_task() 让出 CPU
    • 被唤醒后重新检查并尝试获取锁(可能需要循环)
  3. 释放锁时(包括进程退出),唤醒等待队列中的所有任务
  4. 需要处理信号中断(返回 EINTR)

参考实现:

loop {
    if can_acquire_lock() {
        acquire_and_break();
    }
    if !blocking {
        return Err(WouldBlock);
    }
    // 检查信号
    if has_pending_signal() {
        return Err(Interrupted);
    }
    wait_queue.sleep(current_task());
}
Source

pub fn release_all_locks(&self, pid: i32)

释放进程持有的所有锁(进程退出时调用)

Auto Trait Implementations§

§

impl !Freeze for FileLockManager

§

impl !RefUnwindSafe for FileLockManager

§

impl Send for FileLockManager

§

impl Sync for FileLockManager

§

impl Unpin for FileLockManager

§

impl UnwindSafe for FileLockManager

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.