Move pthread::Errno to separate module.

This commit is contained in:
4lDO2
2024-09-07 12:46:16 +02:00
parent e73b891e77
commit 4bd0d2a1ef
21 changed files with 77 additions and 80 deletions
+34
View File
@@ -0,0 +1,34 @@
use crate::platform::types::c_int;
/// Positive error codes (EINVAL, not -EINVAL).
#[derive(Debug, Eq, PartialEq)]
// TODO: Move to a more generic place.
pub struct Errno(pub c_int);
#[cfg(target_os = "redox")]
impl From<syscall::Error> for Errno {
fn from(value: syscall::Error) -> Self {
Errno(value.errno)
}
}
#[cfg(target_os = "redox")]
impl From<Errno> for syscall::Error {
fn from(value: Errno) -> Self {
syscall::Error::new(value.0)
}
}
pub trait ResultExt<T> {
fn or_minus_one_errno(self) -> T;
}
impl<T: From<i8>> ResultExt<T> for Result<T, Errno> {
fn or_minus_one_errno(self) -> T {
match self {
Self::Ok(v) => v,
Self::Err(Errno(errno)) => unsafe {
crate::platform::ERRNO.set(errno);
T::from(-1)
},
}
}
}
+1 -1
View File
@@ -1,12 +1,12 @@
use crate::{
c_str::CStr,
error::ResultExt,
header::{
fcntl::O_CREAT,
unistd::{SEEK_CUR, SEEK_END, SEEK_SET},
},
io,
platform::{types::*, Pal, Sys},
pthread::ResultExt,
};
use core::ops::Deref;
+11 -10
View File
@@ -3,15 +3,16 @@
use core::{cell::Cell, ptr::NonNull};
use crate::{
error::Errno,
header::{sched::*, time::timespec},
platform::{types::*, Pal, Sys},
pthread,
};
pub fn e(result: Result<(), pthread::Errno>) -> i32 {
pub fn e(result: Result<(), Errno>) -> i32 {
match result {
Ok(()) => 0,
Err(pthread::Errno(error)) => error,
Err(Errno(error)) => error,
}
}
@@ -75,7 +76,7 @@ pub use self::barrier::*;
pub unsafe extern "C" fn pthread_cancel(thread: pthread_t) -> c_int {
match pthread::cancel(&*thread.cast()) {
Ok(()) => 0,
Err(pthread::Errno(error)) => error,
Err(Errno(error)) => error,
}
}
@@ -96,7 +97,7 @@ pub unsafe extern "C" fn pthread_create(
core::ptr::write(pthread, ptr);
0
}
Err(pthread::Errno(code)) => code,
Err(Errno(code)) => code,
}
}
@@ -104,7 +105,7 @@ pub unsafe extern "C" fn pthread_create(
pub unsafe extern "C" fn pthread_detach(pthread: pthread_t) -> c_int {
match pthread::detach(&*pthread.cast()) {
Ok(()) => 0,
Err(pthread::Errno(errno)) => errno,
Err(Errno(errno)) => errno,
}
}
@@ -134,7 +135,7 @@ pub unsafe extern "C" fn pthread_getcpuclockid(
clock_out.write(clock);
0
}
Err(pthread::Errno(error)) => error,
Err(Errno(error)) => error,
}
}
@@ -151,7 +152,7 @@ pub unsafe extern "C" fn pthread_getschedparam(
0
}
Err(pthread::Errno(error)) => error,
Err(Errno(error)) => error,
}
}
@@ -167,7 +168,7 @@ pub unsafe extern "C" fn pthread_join(thread: pthread_t, retval: *mut *mut c_voi
}
0
}
Err(pthread::Errno(error)) => error,
Err(Errno(error)) => error,
}
}
@@ -195,7 +196,7 @@ pub unsafe extern "C" fn pthread_setcancelstate(state: c_int, oldstate: *mut c_i
}
0
}
Err(pthread::Errno(error)) => error,
Err(Errno(error)) => error,
}
}
#[no_mangle]
@@ -209,7 +210,7 @@ pub unsafe extern "C" fn pthread_setcanceltype(ty: c_int, oldty: *mut c_int) ->
}
0
}
Err(pthread::Errno(error)) => error,
Err(Errno(error)) => error,
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
use super::*;
use crate::pthread::Errno;
use crate::error::Errno;
// PTHREAD_MUTEX_INITIALIZER is defined in bits_pthread/cbindgen.toml
+2 -2
View File
@@ -5,9 +5,9 @@ use core::{mem, ptr};
use cbitset::BitSet;
use crate::{
error::{self, Errno, ResultExt},
header::{errno, time::timespec},
platform::{self, types::*, Pal, PalSignal, Sys},
pthread::{self, Errno, ResultExt},
};
pub use self::sys::*;
@@ -81,7 +81,7 @@ pub extern "C" fn killpg(pgrp: pid_t, sig: c_int) -> c_int {
#[no_mangle]
pub unsafe extern "C" fn pthread_kill(thread: pthread_t, sig: c_int) -> c_int {
let os_tid = {
let pthread = &*(thread as *const pthread::Pthread);
let pthread = &*(thread as *const crate::pthread::Pthread);
pthread.os_tid.get().read()
};
crate::header::pthread::e(Sys::rlct_kill(os_tid, sig as usize))
+1 -1
View File
@@ -9,6 +9,7 @@ use core::{
use crate::{
c_str::CStr,
error::ResultExt,
header::{
crypt::{crypt_data, crypt_r},
errno, fcntl, limits,
@@ -17,7 +18,6 @@ use crate::{
time::timespec,
},
platform::{self, types::*, Pal, Sys},
pthread::ResultExt,
};
use alloc::collections::LinkedList;
+1
View File
@@ -47,6 +47,7 @@ pub mod c_str;
pub mod c_vec;
pub mod cxa;
pub mod db;
pub mod error;
pub mod fs;
pub mod header;
pub mod io;
+2 -2
View File
@@ -15,8 +15,8 @@ use crate::header::{
};
// use header::sys_times::tms;
use crate::{
error::Errno,
header::{sys_utsname::utsname, time::timespec},
pthread::Errno,
};
mod epoll;
@@ -243,7 +243,7 @@ impl Pal for Sys {
addr: *mut u32,
val: u32,
deadline: Option<&timespec>,
) -> Result<(), crate::pthread::Errno> {
) -> Result<(), Errno> {
let deadline = deadline.map_or(0, |d| d as *const _ as usize);
e_raw(unsafe {
syscall!(
+1 -1
View File
@@ -5,12 +5,12 @@ use super::{
e, e_raw, Sys,
};
use crate::{
error::Errno,
header::{
signal::{sigaction, siginfo_t, sigset_t, stack_t, NSIG, SA_RESTORER},
sys_time::itimerval,
time::timespec,
},
pthread::Errno,
};
impl PalSignal for Sys {
+1 -1
View File
@@ -1,6 +1,6 @@
use crate::{
error::ResultExt,
io::{self, Read, Write},
pthread::ResultExt,
};
use alloc::{boxed::Box, vec::Vec};
use core::{cell::Cell, fmt, ptr};
+6 -9
View File
@@ -1,6 +1,7 @@
use super::types::*;
use crate::{
c_str::CStr,
error::Errno,
header::{
dirent::dirent,
sys_resource::rlimit,
@@ -10,7 +11,7 @@ use crate::{
sys_utsname::utsname,
time::timespec,
},
pthread::{self, Errno},
pthread,
};
pub use self::epoll::PalEpoll;
@@ -83,8 +84,8 @@ pub trait Pal {
addr: *mut u32,
val: u32,
deadline: Option<&timespec>,
) -> Result<(), pthread::Errno>;
unsafe fn futex_wake(addr: *mut u32, num: u32) -> Result<u32, pthread::Errno>;
) -> Result<(), Errno>;
unsafe fn futex_wake(addr: *mut u32, num: u32) -> Result<u32, Errno>;
fn futimens(fd: c_int, times: *const timespec) -> c_int;
@@ -182,12 +183,8 @@ pub trait Pal {
fn pipe2(fildes: &mut [c_int], flags: c_int) -> c_int;
unsafe fn rlct_clone(stack: *mut usize)
-> Result<crate::pthread::OsTid, crate::pthread::Errno>;
unsafe fn rlct_kill(
os_tid: crate::pthread::OsTid,
signal: usize,
) -> Result<(), crate::pthread::Errno>;
unsafe fn rlct_clone(stack: *mut usize) -> Result<crate::pthread::OsTid, Errno>;
unsafe fn rlct_kill(os_tid: crate::pthread::OsTid, signal: usize) -> Result<(), Errno>;
fn current_os_tid() -> crate::pthread::OsTid;
fn read(fildes: c_int, buf: &mut [u8]) -> Result<ssize_t, Errno>;
+1 -1
View File
@@ -1,11 +1,11 @@
use super::super::{types::*, Pal};
use crate::{
error::Errno,
header::{
signal::{sigaction, siginfo_t, sigset_t, stack_t},
sys_time::itimerval,
time::timespec,
},
pthread::Errno,
};
pub trait PalSignal: Pal {
+1 -1
View File
@@ -4,11 +4,11 @@ use super::{
};
use crate::{
error::ResultExt,
fs::File,
header::{errno::*, fcntl::*, signal::sigset_t, sys_epoll::*},
io::prelude::*,
platform,
pthread::ResultExt,
};
use core::{mem, slice};
use syscall::{
+6 -11
View File
@@ -8,6 +8,7 @@ use syscall::{
use crate::{
c_str::{CStr, CString},
error::{self, Errno, ResultExt},
fs::File,
header::{
dirent::dirent,
@@ -25,7 +26,6 @@ use crate::{
unistd::{F_OK, R_OK, W_OK, X_OK},
},
io::{self, prelude::*, BufReader},
pthread::{self, Errno, ResultExt},
};
pub use redox_rt::proc::FdGuard;
@@ -307,7 +307,7 @@ impl Pal for Sys {
addr: *mut u32,
val: u32,
deadline: Option<&timespec>,
) -> Result<(), pthread::Errno> {
) -> Result<(), Errno> {
let deadline = deadline.map(|d| syscall::TimeSpec {
tv_sec: d.tv_sec,
tv_nsec: d.tv_nsec as i32,
@@ -316,7 +316,7 @@ impl Pal for Sys {
Ok(())
}
#[inline]
unsafe fn futex_wake(addr: *mut u32, num: u32) -> Result<u32, pthread::Errno> {
unsafe fn futex_wake(addr: *mut u32, num: u32) -> Result<u32, Errno> {
Ok(redox_rt::sys::sys_futex_wake(addr, num)?)
}
@@ -787,21 +787,16 @@ impl Pal for Sys {
e(extra::pipe2(fds, flags as usize).map(|()| 0)) as c_int
}
unsafe fn rlct_clone(
stack: *mut usize,
) -> Result<crate::pthread::OsTid, crate::pthread::Errno> {
unsafe fn rlct_clone(stack: *mut usize) -> Result<crate::pthread::OsTid, Errno> {
let _guard = clone::rdlock();
let res = clone::rlct_clone_impl(stack);
res.map(|mut fd| crate::pthread::OsTid {
thread_fd: fd.take(),
})
.map_err(|error| crate::pthread::Errno(error.errno))
.map_err(|error| Errno(error.errno))
}
unsafe fn rlct_kill(
os_tid: crate::pthread::OsTid,
signal: usize,
) -> Result<(), crate::pthread::Errno> {
unsafe fn rlct_kill(os_tid: crate::pthread::OsTid, signal: usize) -> Result<(), Errno> {
redox_rt::sys::posix_kill_thread(os_tid.thread_fd, signal as u32)?;
Ok(())
}
+1 -1
View File
@@ -7,6 +7,7 @@ use super::{
e, Sys,
};
use crate::{
error::Errno,
header::{
errno::{EINVAL, ENOSYS},
signal::{
@@ -17,7 +18,6 @@ use crate::{
time::timespec,
},
platform::ERRNO,
pthread::Errno,
};
impl PalSignal for Sys {
+1 -1
View File
@@ -7,6 +7,7 @@ use super::{
e, Sys,
};
use crate::{
error::ResultExt,
header::{
arpa_inet::inet_aton,
netinet_in::{in_addr, in_port_t, sockaddr_in},
@@ -15,7 +16,6 @@ use crate::{
sys_time::timeval,
sys_un::sockaddr_un,
},
pthread::ResultExt,
};
macro_rules! bind_or_connect {
+1 -1
View File
@@ -3,8 +3,8 @@ use alloc::vec::Vec;
use crate::platform::{types::*, Pal, Sys};
use crate::{
error::ResultExt,
header::unistd::{lseek, SEEK_SET},
pthread::ResultExt,
};
/// Implements an `Iterator` which returns on either newline or EOF.
#[derive(Clone)]
+1 -33
View File
@@ -10,6 +10,7 @@ use core::{
use alloc::{boxed::Box, collections::BTreeMap};
use crate::{
error::Errno,
header::{errno::*, pthread as header, sched::sched_param, sys_mman},
ld_so::{
linker::Linker,
@@ -82,39 +83,6 @@ pub struct OsTid {
unsafe impl Send for Pthread {}
unsafe impl Sync for Pthread {}
/// Positive error codes (EINVAL, not -EINVAL).
#[derive(Debug, Eq, PartialEq)]
// TODO: Move to a more generic place.
pub struct Errno(pub c_int);
#[cfg(target_os = "redox")]
impl From<syscall::Error> for Errno {
fn from(value: syscall::Error) -> Self {
Errno(value.errno)
}
}
#[cfg(target_os = "redox")]
impl From<Errno> for syscall::Error {
fn from(value: Errno) -> Self {
syscall::Error::new(value.0)
}
}
pub trait ResultExt<T> {
fn or_minus_one_errno(self) -> T;
}
impl<T: From<i8>> ResultExt<T> for Result<T, Errno> {
fn or_minus_one_errno(self) -> T {
match self {
Self::Ok(v) => v,
Self::Err(Errno(errno)) => unsafe {
crate::platform::ERRNO.set(errno);
T::from(-1)
},
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Retval(pub *mut c_void);
+2 -2
View File
@@ -1,8 +1,8 @@
// Used design from https://www.remlab.net/op/futex-condvar.shtml
use crate::{
error::Errno,
header::{pthread::*, time::timespec},
pthread::Errno,
};
use core::sync::atomic::{AtomicU32 as AtomicUint, Ordering};
@@ -12,7 +12,7 @@ pub struct Cond {
prev: AtomicUint,
}
type Result<T, E = crate::pthread::Errno> = core::result::Result<T, E>;
type Result<T, E = Errno> = core::result::Result<T, E>;
impl Cond {
pub fn new() -> Self {
+1 -1
View File
@@ -19,12 +19,12 @@ pub use self::{
};
use crate::{
error::Errno,
header::{
errno::{EAGAIN, ETIMEDOUT},
time::timespec,
},
platform::{types::*, Pal, Sys},
pthread::Errno,
};
use core::{
mem::MaybeUninit,
+1
View File
@@ -4,6 +4,7 @@ use core::{
};
use crate::{
error::Errno,
header::{errno::*, pthread::*, time::timespec},
pthread::*,
};