Thread

Struct Thread 

Source
#[repr(transparent)]
pub struct Thread(pub Thread);
Expand description

A thread state of project 5, which contains file and memory state.

Tuple Fields§

§0: Thread

Implementations§

Source§

impl Thread

Source

pub fn from_mm_struct(mm_struct: MmStruct<LazyPager>, tid: u64) -> Self

Source

pub fn from_fs_mm_struct( file_struct: FileStruct, mm_struct: MmStruct<LazyPager>, tid: u64, ) -> Self

Methods from Deref<Target = Thread>§

Source

pub fn with_file_struct_mut<Args, R>( &self, f: impl FnOnce(&mut FileStruct, Args) -> R, args: Args, ) -> R

Executes a closure with mutable access to the underlying file struct (FileStruct).

This method provides a way to access and mutate the file struct associated with the current thread. It accepts a closure f that receives a mutable reference to the FileStruct and an additional argument of type Args.

Source

pub fn with_mm_struct_mut<Args, R>( &self, f: impl FnOnce(&mut MmStruct<LazyPager>, Args) -> R, args: Args, ) -> R

Executes a closure with mutable access to the underlying memory struct (MmStruct).

This method provides a way to access and mutate the memory struct associated with the current thread. It accepts a closure f that receives a mutable reference to the MmStruct<LazyPager> and an additional argument of type Args.

Source

pub fn with_file_mm_struct_mut<Args, R>( &self, f: impl FnOnce(&mut FileStruct, &mut MmStruct<LazyPager>, Args) -> R, args: Args, ) -> R

Executes a closure with mutable access to the underlying file struct (FileStruct) and memory struct (MmStruct).

This method provides a way to access and mutate the file struct associated with the current thread. It accepts a closure f that receives a mutable reference to the FileStruct and an additional argument of type Args.

Source

pub fn exit(&self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>

Exit the current thread.

This function terminates the calling thread, returning the provided exit code to any thread that joins on it.

§Syscall API
void exit(int status);
  • status: The exit code returned to a joining thread.
§Behavior
  • Wakes up any thread waiting via thread_join.
  • Cleans up thread-local resources.
Source

pub fn thread_create(&self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>

Create a new thread in the current process.

This function creates a new thread that begins execution at the given entry point with the specified argument.

§Syscall API
int thread_create(char *name, void *stack, void *(*start_routine)(void *), void *arg);
  • name: Name of the thread.
  • stack: Stack of the thread.
  • start_routine: Pointer to the function to be executed by the thread.
  • arg: Argument to be passed to the thread function.
§Behavior
  • The new thread shares the same address space as the calling thread.
  • The stack for the new thread is allocated automatically.
Source

pub fn thread_join(&self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>

Wait for a thread to finish.

This function blocks the calling thread until the specified thread terminates, and retrieves its exit code.

Note that only a single call can receives the exit code of the dying thread. If multiple thread_join is called on the same thread, return values of others than the first one are InvalidArgument error.

§Syscall API
int thread_join(int thread_id, int *retval);
  • thread_id: ID of the thread to join.
  • retval: Pointer to store the thread’s exit code (optional).
§Behavior
  • If the target thread has already exited, returns immediately with the proper exit code.
  • If retval is non-null, the exit code of the target thread is stored.
Source

pub fn exit_group(&self, abi: &SyscallAbi<'_>) -> Result<usize, KernelError>

Exit a process.

This function terminates all the threads in the current process, including the current caller thread. The exit code is provided as the first argument (arg1) of the system call.

§Syscall API
int exit_group(int status);
  • status: The thread’s exit code.
§Notes
  • This function does not return in normal execution, as it terminates the process.
  • If an error occurs, it returns a KernelError

Trait Implementations§

Source§

impl Default for Thread

Source§

fn default() -> Thread

Returns the “default value” for a type. Read more
Source§

impl Deref for Thread

Source§

type Target = Thread

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Thread

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl Task for Thread

Source§

fn syscall(&mut self, regs: &mut Registers)

Handles a system call request from a user program.

This function is the entry point for system call handling. It retrieves the system call ABI from the provided Registers structure, which includes the system call number and its arguments. Based on the system call number (sysno), it looks up the appropriate handler function in a predefined list. If a handler is found, it is invoked with the ABI, otherwise, an error (KernelError::NoSuchSyscall) is returned.

After the handler function processes the system call, the return value (either a success or error) is set back into the CPU registers via the set_return_value method. The return value is stored in the %rax register as per the x86-64 system call convention.

§Parameters
  • regs: A mutable reference to the Registers struct, which contains the current state of the CPU registers. This structure will be used to retrieve the system call number and its arguments, and also to set the return value.
§Functionality

The function processes the system call as follows:

  1. Extracts the system call number and arguments using the SyscallAbi::from_registers.
  2. Looks up the corresponding handler function from a predefined list of system calls. The handler function is selected based on the system call number (sysno).
  3. If a handler is found, it is executed with the ABI as an argument. If no handler is found, the function returns a KernelError::NoSuchSyscall error.

The result of the system call handler (either success or error) is then returned via the SyscallAbi::set_return_value method, which modifies the CPU registers accordingly.

Source§

fn access_ok(&self, addr: Range<Va>, is_write: bool) -> bool

Validates a given memory address range before use. Read more
Source§

fn page_fault(&mut self, ec: PFErrorCode, cr2: Va)

Handles a page fault that occurs when accessing an unmapped memory page. Read more
Source§

fn with_page_table_pa(&self, f: &fn(Pa))

Run a closure with physical address of the page table.

Auto Trait Implementations§

§

impl Freeze for Thread

§

impl !RefUnwindSafe for Thread

§

impl Send for Thread

§

impl Sync for Thread

§

impl Unpin for Thread

§

impl !UnwindSafe for Thread

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
§

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.