From 87874745c054a872226f00d9b7fe1756ec2c01ab Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Fri, 24 Nov 2023 11:06:00 +0200 Subject: [PATCH] Mutexes and threads --- src/alloc.rs | 13 ++++++++++--- src/process.rs | 3 ++- src/sys/mod.rs | 28 ++++++++++++++++++++++++++-- src/time.rs | 5 +---- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/alloc.rs b/src/alloc.rs index f74ad3e0..780a1e15 100644 --- a/src/alloc.rs +++ b/src/alloc.rs @@ -25,9 +25,16 @@ pub unsafe fn malloc(layout: Layout) -> *mut u8 { /// # Safety /// /// Unsafe. -pub unsafe fn malloc_zeroed(_layout: Layout) -> *mut u8 { - sys::debug_trace("rt::malloc_zeroed()"); - loop {} +pub unsafe fn malloc_zeroed(layout: Layout) -> *mut u8 { + let ptr = malloc(layout); + + if ptr.is_null() { + return ptr; + } + + crate::memset(ptr as *mut _, 0, layout.size()); + + ptr } /// Frees the memory pointed to by `ptr`. diff --git a/src/process.rs b/src/process.rs index e7891faa..29a2085c 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,5 +1,6 @@ //! Process management data types pub use abi::process::{ - ExitCode, ProgramArgumentInner, Signal, SignalEntryData, SpawnOption, SpawnOptions, + ExitCode, MutexOperation, ProgramArgumentInner, Signal, SignalEntryData, SpawnOption, + SpawnOptions, ThreadSpawnOptions, }; diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 05a76d56..590cd95d 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -1,6 +1,6 @@ //! System call implementations -use core::{mem::MaybeUninit, time::Duration}; +use core::{mem::MaybeUninit, sync::atomic::AtomicU32, time::Duration}; use abi::{ error::{Error, SyscallResult}, @@ -8,7 +8,9 @@ use abi::{ DeviceRequest, DirectoryEntry, FileAttr, FileMode, MountOptions, OpenOptions, RawFd, SeekFrom, UnmountOptions, }, - process::{ExitCode, Signal, SignalEntryData, SpawnOptions}, + process::{ + ExitCode, MutexOperation, Signal, SignalEntryData, SpawnOptions, ThreadSpawnOptions, + }, syscall::SyscallFunction, }; @@ -302,6 +304,28 @@ pub unsafe fn spawn_process(options: &SpawnOptions<'_>) -> Result { u32::from_syscall_result(syscall!(SyscallFunction::SpawnProcess, argp!(options as *const _))) } +/// System call: spawn a new thread within the caller process. +/// +/// # Safety +/// +/// Unsafe: direct system call. +pub unsafe fn spawn_thread(options: &ThreadSpawnOptions) -> Result { + u32::from_syscall_result(syscall!(SyscallFunction::SpawnThread, argp!(options as *const _))) +} + +/// System call: perform a userspace mutex wait or wake operation. +/// +/// # Safety +/// +/// Unsafe: direct system call. +pub unsafe fn mutex(mutex: &AtomicU32, op: &MutexOperation) -> Result<(), Error> { + <()>::from_syscall_result(syscall!( + SyscallFunction::Mutex, + argp!(mutex as *const _), + argp!(op as *const _) + )) +} + /// System call: wait for a process to finish /// /// # Safety diff --git a/src/time.rs b/src/time.rs index 542770d1..5816068d 100644 --- a/src/time.rs +++ b/src/time.rs @@ -13,9 +13,6 @@ pub struct Timespec { impl Timespec { /// Constructs a [Timespec] with all components set to zero pub const fn zero() -> Self { - Self { - seconds: 0, - nanoseconds: 0, - } + Self { seconds: 0, nanoseconds: 0 } } }