Drop files at the correct time

This commit is contained in:
Ruihan Li
2025-04-05 14:28:15 +08:00
committed by Tate, Hongliang Tian
parent 7e1abc1fbb
commit 8600278a5f
62 changed files with 223 additions and 141 deletions

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
use core::cell::{Cell, RefCell};
use core::cell::{Cell, Ref, RefCell, RefMut};
use aster_rights::Full;
use ostd::{mm::Vaddr, sync::RwArc, task::CurrentTask};
@ -23,7 +23,7 @@ pub struct ThreadLocal {
robust_list: RefCell<Option<RobustListHead>>,
// Files.
file_table: RefCell<RwArc<FileTable>>,
file_table: RefCell<Option<RwArc<FileTable>>>,
// Signal.
/// `ucontext` address for the signal handler.
@ -38,15 +38,15 @@ impl ThreadLocal {
pub(super) fn new(
set_child_tid: Vaddr,
clear_child_tid: Vaddr,
root_vmar: Option<Vmar<Full>>,
root_vmar: Vmar<Full>,
file_table: RwArc<FileTable>,
) -> Self {
Self {
set_child_tid: Cell::new(set_child_tid),
clear_child_tid: Cell::new(clear_child_tid),
root_vmar: RefCell::new(root_vmar),
root_vmar: RefCell::new(Some(root_vmar)),
robust_list: RefCell::new(None),
file_table: RefCell::new(file_table),
file_table: RefCell::new(Some(file_table)),
sig_context: Cell::new(None),
sig_stack: RefCell::new(None),
}
@ -68,8 +68,12 @@ impl ThreadLocal {
&self.robust_list
}
pub fn file_table(&self) -> &RefCell<RwArc<FileTable>> {
&self.file_table
pub fn borrow_file_table(&self) -> FileTableRef {
FileTableRef(self.file_table.borrow())
}
pub fn borrow_file_table_mut(&self) -> FileTableRefMut {
FileTableRefMut(self.file_table.borrow_mut())
}
pub fn sig_context(&self) -> &Cell<Option<Vaddr>> {
@ -81,6 +85,39 @@ impl ThreadLocal {
}
}
/// An immutable, shared reference to the file table in [`ThreadLocal`].
pub struct FileTableRef<'a>(Ref<'a, Option<RwArc<FileTable>>>);
impl FileTableRef<'_> {
/// Unwraps and returns a reference to the file table.
///
/// # Panics
///
/// This method will panic if the thread has exited and the file table has been dropped.
pub fn unwrap(&self) -> &RwArc<FileTable> {
self.0.as_ref().unwrap()
}
}
/// A mutable, exclusive reference to the file table in [`ThreadLocal`].
pub struct FileTableRefMut<'a>(RefMut<'a, Option<RwArc<FileTable>>>);
impl FileTableRefMut<'_> {
/// Unwraps and returns a reference to the file table.
///
/// # Panics
///
/// This method will panic if the thread has exited and the file table has been dropped.
pub fn unwrap(&mut self) -> &mut RwArc<FileTable> {
self.0.as_mut().unwrap()
}
/// Removes the file table and drops it.
pub(super) fn remove(&mut self) {
*self.0 = None;
}
}
/// A trait to provide the `as_thread_local` method for tasks.
pub trait AsThreadLocal {
/// Returns the associated [`ThreadLocal`].