Mutexes and threads

This commit is contained in:
Mark Poliakov 2023-11-24 11:06:00 +02:00
parent f00ecdb926
commit 87874745c0
4 changed files with 39 additions and 10 deletions

View File

@ -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`.

View File

@ -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,
};

View File

@ -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, Error> {
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, Error> {
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

View File

@ -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 }
}
}