Struct Task

Source
pub struct Task {
Show 24 fields id: usize, pub name: String, pub priority: u32, pub vcpu: Vcpu, pub state: TaskState, pub task_type: TaskType, pub entry: usize, pub brk: Option<usize>, pub stack_size: usize, pub data_size: usize, pub text_size: usize, pub max_stack_size: usize, pub max_data_size: usize, pub max_text_size: usize, pub vm_manager: VirtualMemoryManager, pub managed_pages: Vec<ManagedPage>, parent_id: Option<usize>, children: Vec<usize>, exit_status: Option<i32>, pub abi: Option<Box<dyn AbiModule>>, fd_table: Vec<usize>, files: [Option<File>; 256], pub cwd: Option<String>, pub vfs: Option<Arc<VfsManager>>,
}

Fields§

§id: usize§name: String§priority: u32§vcpu: Vcpu§state: TaskState§task_type: TaskType§entry: usize§brk: Option<usize>§stack_size: usize§data_size: usize§text_size: usize§max_stack_size: usize§max_data_size: usize§max_text_size: usize§vm_manager: VirtualMemoryManager§managed_pages: Vec<ManagedPage>

Managed pages

Managed pages are freed automatically when the task is terminated.

§parent_id: Option<usize>§children: Vec<usize>§exit_status: Option<i32>§abi: Option<Box<dyn AbiModule>>

Dynamic ABI

§fd_table: Vec<usize>§files: [Option<File>; 256]§cwd: Option<String>§vfs: Option<Arc<VfsManager>>

Virtual File System Manager

Each task can have its own isolated VfsManager instance for containerization and namespace isolation. The VfsManager provides:

  • Filesystem Isolation: Independent mount point namespaces allowing complete filesystem isolation between tasks or containers
  • Selective Sharing: Arc-based filesystem object sharing enables controlled resource sharing while maintaining namespace independence
  • Bind Mount Support: Advanced bind mount capabilities for flexible directory mapping and container orchestration scenarios
  • Security: Path normalization and validation preventing directory traversal attacks and unauthorized filesystem access

§Usage Patterns

  • None: Task uses global filesystem namespace (traditional Unix-like behavior)
  • Some(Arc<VfsManager>): Task has isolated filesystem namespace (container-like behavior)

§Thread Safety

VfsManager is thread-safe and can be shared between tasks using Arc. All internal operations use RwLock for concurrent access protection.

Implementations§

Source§

impl Task

Source

pub fn new(name: String, priority: u32, task_type: TaskType) -> Self

Source

pub fn init(&mut self)

Source

pub fn get_id(&self) -> usize

Source

pub fn set_state(&mut self, state: TaskState)

Set the task state

§Arguments
  • state - The new task state
Source

pub fn get_state(&self) -> TaskState

Get the task state

§Returns

The task state

Source

pub fn get_size(&self) -> usize

Get the size of the task.

§Returns

The size of the task in bytes.

Source

pub fn get_brk(&self) -> usize

Get the program break (NOT work in Kernel task)

§Returns

The program break address

Source

pub fn set_brk(&mut self, brk: usize) -> Result<(), &'static str>

Set the program break (NOT work in Kernel task)

§Arguments
  • brk - The new program break address
§Returns

If successful, returns Ok(()), otherwise returns an error.

Source

pub fn allocate_pages( &mut self, vaddr: usize, num_of_pages: usize, permissions: usize, ) -> Result<VirtualMemoryMap, &'static str>

Allocate pages for the task.

§Arguments
  • vaddr - The virtual address to allocate pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to allocate
  • segment - The segment type to allocate pages
§Returns

The memory map of the allocated pages, if successful.

§Errors

If the address is not page aligned, or if the pages cannot be allocated.

§Note

This function don’t increment the size of the task. You must increment the size of the task manually.

Source

pub fn free_pages(&mut self, vaddr: usize, num_of_pages: usize)

Free pages for the task.

§Arguments
  • vaddr - The virtual address to free pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to free
Source

pub fn allocate_text_pages( &mut self, vaddr: usize, num_of_pages: usize, ) -> Result<VirtualMemoryMap, &'static str>

Allocate text pages for the task. And increment the size of the task.

§Arguments
  • vaddr - The virtual address to allocate pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to allocate
§Returns

The memory map of the allocated pages, if successful.

§Errors

If the address is not page aligned, or if the pages cannot be allocated.

Source

pub fn free_text_pages(&mut self, vaddr: usize, num_of_pages: usize)

Free text pages for the task. And decrement the size of the task.

§Arguments
  • vaddr - The virtual address to free pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to free
Source

pub fn allocate_stack_pages( &mut self, vaddr: usize, num_of_pages: usize, ) -> Result<VirtualMemoryMap, &'static str>

Allocate stack pages for the task. And increment the size of the task.

§Arguments
  • vaddr - The virtual address to allocate pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to allocate
§Returns

The memory map of the allocated pages, if successful.

§Errors

If the address is not page aligned, or if the pages cannot be allocated.

Source

pub fn free_stack_pages(&mut self, vaddr: usize, num_of_pages: usize)

Free stack pages for the task. And decrement the size of the task.

§Arguments
  • vaddr - The virtual address to free pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to free
Source

pub fn allocate_data_pages( &mut self, vaddr: usize, num_of_pages: usize, ) -> Result<VirtualMemoryMap, &'static str>

Allocate data pages for the task. And increment the size of the task.

§Arguments
  • vaddr - The virtual address to allocate pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to allocate
§Returns

The memory map of the allocated pages, if successful.

§Errors

If the address is not page aligned, or if the pages cannot be allocated.

Source

pub fn free_data_pages(&mut self, vaddr: usize, num_of_pages: usize)

Free data pages for the task. And decrement the size of the task.

§Arguments
  • vaddr - The virtual address to free pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to free
Source

pub fn allocate_guard_pages( &mut self, vaddr: usize, num_of_pages: usize, ) -> Result<VirtualMemoryMap, &'static str>

Allocate guard pages for the task.

§Arguments
  • vaddr - The virtual address to allocate pages (NOTE: The address must be page aligned)
  • num_of_pages - The number of pages to allocate
§Returns

The memory map of the allocated pages, if successful.

§Errors

If the address is not page aligned, or if the pages cannot be allocated.

§Note

Gurad pages are not allocated in the physical memory space. This function only maps the pages to the virtual memory space.

Source

pub fn add_managed_page(&mut self, pages: ManagedPage)

Add pages to the task

§Arguments
  • pages - The managed page to add
§Note

Pages added as ManagedPage of the Task will be automatically freed when the Task is terminated. So, you must not free them by calling free_raw_pages/free_boxed_pages manually.

Source

fn get_managed_page(&self, vaddr: usize) -> Option<&ManagedPage>

Get managed page

§Arguments
  • vaddr - The virtual address of the page
§Returns

The managed page if found, otherwise None

Source

fn remove_managed_page(&mut self, vaddr: usize) -> Option<Box<Page>>

Remove managed page

§Arguments
  • vaddr - The virtual address of the page
§Returns

The removed managed page if found, otherwise None

Source

pub fn set_entry_point(&mut self, entry: usize)

Source

pub fn get_parent_id(&self) -> Option<usize>

Get the parent ID

§Returns

The parent task ID, or None if there is no parent

Source

pub fn set_parent_id(&mut self, parent_id: usize)

Set the parent task

§Arguments
  • parent_id - The ID of the parent task
Source

pub fn add_child(&mut self, child_id: usize)

Add a child task

§Arguments
  • child_id - The ID of the child task
Source

pub fn remove_child(&mut self, child_id: usize) -> bool

Remove a child task

§Arguments
  • child_id - The ID of the child task to remove
§Returns

true if the removal was successful, false if the child task was not found

Source

pub fn get_children(&self) -> &Vec<usize>

Get the list of child tasks

§Returns

A vector of child task IDs

Source

pub fn set_exit_status(&mut self, status: i32)

Set the exit status

§Arguments
  • status - The exit status
Source

pub fn get_exit_status(&self) -> Option<i32>

Get the exit status

§Returns

The exit status, or None if not set

Source

pub fn get_fd_table(&self) -> &Vec<usize>

Get the file descriptor table

§Returns

A reference to the file descriptor table

Source

pub fn get_file(&self, index: usize) -> Option<&File>

Get the file at the specified index

§Arguments
  • index - The index of the file
§Returns

The file at the specified index, or None if not found

Source

pub fn get_mut_file(&mut self, fd: usize) -> Option<&mut File>

Get the mutable file at the specified file descriptor

§Arguments
  • fd - The file descriptor of the file
§Returns

The mutable file at the specified file descriptor, or None if not found

Source

pub fn set_file(&mut self, fd: usize, file: File) -> Result<(), &'static str>

Set the file at the specified file descriptor

§Arguments
  • fd - The file descriptor of the file
  • file - The file to set
§Returns

The result of setting the file, which is Ok(()) if successful or an error message if not.

Source

pub fn add_file(&mut self, file: File) -> Result<usize, &'static str>

Add a file to the task

§Arguments
  • file - The file handle to add
§Returns

The file descriptor of the file, or an error message if the file descriptor table is full

Source

pub fn remove_file(&mut self, fd: usize) -> Result<(), &'static str>

Remove a file from the task

§Arguments
  • fd - The file descriptor to remove
§Returns

The result of removing the file, which is Ok(()) if successful or an error message if not.

Source

fn allocate_fd(&mut self) -> Option<usize>

Allocate a file descriptor

§Returns

The allocated file descriptor, or None if no file descriptors are available

Source

pub fn clone_task(&mut self) -> Result<Task, &'static str>

Clone this task, creating a near-identical copy

§Returns

The cloned task

§Errors

If the task cannot be cloned, an error is returned.

Source

pub fn exit(&mut self, status: i32)

Exit the task

§Arguments
  • status - The exit status
Source

pub fn wait(&mut self, child_id: usize) -> Result<i32, WaitError>

Wait for a child task to exit and collect its status

§Arguments
  • child_id - The ID of the child task to wait for
§Returns

The exit status of the child task, or an error if the child is not found or not in Zombie state

Auto Trait Implementations§

§

impl Freeze for Task

§

impl !RefUnwindSafe for Task

§

impl !Send for Task

§

impl !Sync for Task

§

impl Unpin for Task

§

impl !UnwindSafe for Task

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.