use core::ffi::{c_char, c_int, c_long, c_ulong};
use crate::os::types::ThreadHandle;
use super::{pthread_cond_t, pthread_condattr_t, pthread_mutex_t, timespec};
#[cfg(target_arch = "x86_64")]
pub(in crate::posix) const PTHREAD_ATTR_T_SIZE: usize = 56;
#[cfg(target_arch = "x86")]
pub(in crate::posix) const PTHREAD_ATTR_T_SIZE: usize = 36;
#[cfg(target_arch = "aarch64")]
pub(in crate::posix) const PTHREAD_ATTR_T_SIZE: usize = 64;
#[cfg(target_arch = "arm")]
pub(in crate::posix) const PTHREAD_ATTR_T_SIZE: usize = 36;
#[cfg(target_arch = "riscv64")]
pub(in crate::posix) const PTHREAD_ATTR_T_SIZE: usize = 56;
#[cfg(target_arch = "riscv32")]
pub(in crate::posix) const PTHREAD_ATTR_T_SIZE: usize = 32;
#[cfg(not(any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "riscv64",
target_arch = "riscv32",
)))]
compile_error!(
"osal-rs: pthread_attr_t layout is not known for this target_arch; add its \
bits/pthreadtypes-arch.h __SIZEOF_PTHREAD_ATTR_T value to posix/ffi/linux.rs"
);
#[cfg(any(target_arch = "x86_64", target_arch = "x86", target_arch = "arm", target_arch = "riscv64", target_arch = "riscv32"))]
pub(in crate::posix) const PTHREAD_STACK_MIN: usize = 16384;
#[cfg(target_arch = "aarch64")]
pub(in crate::posix) const PTHREAD_STACK_MIN: usize = 131072;
#[cfg(target_arch = "x86_64")]
pub(in crate::posix) const PTHREAD_MUTEX_T_SIZE: usize = 40;
#[cfg(target_arch = "x86")]
pub(in crate::posix) const PTHREAD_MUTEX_T_SIZE: usize = 24;
#[cfg(target_arch = "aarch64")]
pub(in crate::posix) const PTHREAD_MUTEX_T_SIZE: usize = 48;
#[cfg(target_arch = "arm")]
pub(in crate::posix) const PTHREAD_MUTEX_T_SIZE: usize = 24;
#[cfg(target_arch = "riscv64")]
pub(in crate::posix) const PTHREAD_MUTEX_T_SIZE: usize = 40;
#[cfg(target_arch = "riscv32")]
pub(in crate::posix) const PTHREAD_MUTEX_T_SIZE: usize = 32;
#[cfg(any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "riscv64",
target_arch = "riscv32",
))]
pub(in crate::posix) const PTHREAD_MUTEXATTR_T_SIZE: usize = 4;
#[cfg(target_arch = "aarch64")]
pub(in crate::posix) const PTHREAD_MUTEXATTR_T_SIZE: usize = 8;
pub(in crate::posix) const PTHREAD_COND_T_SIZE: usize = 48;
#[cfg(any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "riscv64",
target_arch = "riscv32",
))]
pub(in crate::posix) const PTHREAD_CONDATTR_T_SIZE: usize = 4;
#[cfg(target_arch = "aarch64")]
pub(in crate::posix) const PTHREAD_CONDATTR_T_SIZE: usize = 8;
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub(in crate::posix) struct sigset_t {
_opaque: [c_ulong; 1024 / (8 * size_of::<c_ulong>())],
}
#[repr(C)]
#[derive(Copy, Clone)]
pub(in crate::posix) struct sigaction {
sa_handler: super::sighandler_t,
sa_mask: sigset_t,
sa_flags: c_int,
sa_restorer: usize,
}
impl sigaction {
pub(in crate::posix) fn new(handler: super::sighandler_t, mask: sigset_t) -> Self {
Self { sa_handler: handler, sa_mask: mask, sa_flags: SA_RESTART, sa_restorer: 0 }
}
}
pub(in crate::posix) const SA_RESTART: c_int = 0x10000000;
pub(in crate::posix) type pthread_once_t = c_int;
pub(in crate::posix) const PTHREAD_ONCE_INIT: pthread_once_t = 0;
#[cfg(feature = "real_time")]
pub(in crate::posix) const SCHED_FIFO: c_int = 1;
#[cfg(feature = "real_time")]
pub(in crate::posix) const PTHREAD_EXPLICIT_SCHED: c_int = 1;
#[cfg(feature = "real_time")]
#[repr(C)]
#[derive(Copy, Clone)]
pub(in crate::posix) struct sched_param {
sched_priority: c_int,
}
#[cfg(feature = "real_time")]
impl sched_param {
pub(in crate::posix) fn new(priority: c_int) -> Self {
Self { sched_priority: priority }
}
}
pub(in crate::posix) const _SC_PAGESIZE: c_int = 30;
pub(in crate::posix) const _SC_AVPHYS_PAGES: c_int = 86;
pub(in crate::posix) const CLOCK_MONOTONIC: c_int = 1;
pub(in crate::posix) const ETIMEDOUT: c_int = 110;
pub(in crate::posix) const SIG_BLOCK: c_int = 0;
pub(in crate::posix) const SIG_SETMASK: c_int = 2;
pub(in crate::posix) const PTHREAD_MUTEX_RECURSIVE: c_int = 1;
unsafe extern "C" {
fn pthread_setname_np(thread: ThreadHandle, name: *const c_char) -> c_int;
fn __libc_current_sigrtmin() -> c_int;
fn pthread_condattr_setclock(attr: *mut pthread_condattr_t, clock_id: c_int) -> c_int;
fn pthread_cond_timedwait(cond: *mut pthread_cond_t, mutex: *mut pthread_mutex_t, abstime: *const timespec) -> c_int;
}
pub(in crate::posix) unsafe fn condattr_set_monotonic(attr: *mut pthread_condattr_t) {
unsafe {
pthread_condattr_setclock(attr, CLOCK_MONOTONIC);
}
}
pub(in crate::posix) unsafe fn cond_timedwait_monotonic(cond: *mut pthread_cond_t, mutex: *mut pthread_mutex_t, deadline: ×pec) -> c_int {
unsafe { pthread_cond_timedwait(cond, mutex, deadline) }
}
pub(in crate::posix) fn suspend_signal() -> c_int {
unsafe { __libc_current_sigrtmin() }
}
pub(in crate::posix) fn resume_signal() -> c_int {
suspend_signal() + 1
}
pub(in crate::posix) unsafe fn set_current_thread_name(name: *const c_char) -> c_int {
unsafe { pthread_setname_np(super::pthread_self(), name) }
}
pub(in crate::posix) fn available_physical_pages() -> c_long {
unsafe { super::sysconf(_SC_AVPHYS_PAGES) }
}