#![allow(non_camel_case_types)]
use core::ffi::{c_char, c_int, c_long, c_void};
use crate::os::types::ThreadHandle;
#[cfg(target_arch = "x86_64")]
const PTHREAD_ATTR_T_SIZE: usize = 56;
#[cfg(target_arch = "x86")]
const PTHREAD_ATTR_T_SIZE: usize = 36;
#[cfg(target_arch = "aarch64")]
const PTHREAD_ATTR_T_SIZE: usize = 64;
#[cfg(target_arch = "arm")]
const PTHREAD_ATTR_T_SIZE: usize = 36;
#[cfg(target_arch = "riscv64")]
const PTHREAD_ATTR_T_SIZE: usize = 56;
#[cfg(target_arch = "riscv32")]
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.rs"
);
#[cfg(any(target_arch = "x86_64", target_arch = "x86", target_arch = "arm", target_arch = "riscv64", target_arch = "riscv32"))]
pub(super) const PTHREAD_STACK_MIN: usize = 16384;
#[cfg(target_arch = "aarch64")]
pub(super) const PTHREAD_STACK_MIN: usize = 131072;
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub(super) struct pthread_attr_t {
_opaque: [u8; PTHREAD_ATTR_T_SIZE],
}
impl Default for pthread_attr_t {
fn default() -> Self {
Self {
_opaque: [0u8; PTHREAD_ATTR_T_SIZE],
}
}
}
#[cfg(target_arch = "x86_64")]
const PTHREAD_MUTEX_T_SIZE: usize = 40;
#[cfg(target_arch = "x86")]
const PTHREAD_MUTEX_T_SIZE: usize = 24;
#[cfg(target_arch = "aarch64")]
const PTHREAD_MUTEX_T_SIZE: usize = 48;
#[cfg(target_arch = "arm")]
const PTHREAD_MUTEX_T_SIZE: usize = 24;
#[cfg(target_arch = "riscv64")]
const PTHREAD_MUTEX_T_SIZE: usize = 40;
#[cfg(target_arch = "riscv32")]
const PTHREAD_MUTEX_T_SIZE: usize = 32;
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub struct pthread_mutex_t {
_opaque: [u8; PTHREAD_MUTEX_T_SIZE],
}
impl Default for pthread_mutex_t {
fn default() -> Self {
Self {
_opaque: [0u8; PTHREAD_MUTEX_T_SIZE],
}
}
}
impl pthread_mutex_t {
pub(super) fn is_empty(&self) -> bool {
self._opaque.iter().all(|&b| b == 0)
}
}
#[cfg(any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "riscv64",
target_arch = "riscv32",
))]
const PTHREAD_MUTEXATTR_T_SIZE: usize = 4;
#[cfg(target_arch = "aarch64")]
const PTHREAD_MUTEXATTR_T_SIZE: usize = 8;
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub(super) struct pthread_mutexattr_t {
_opaque: [u8; PTHREAD_MUTEXATTR_T_SIZE],
}
impl Default for pthread_mutexattr_t {
fn default() -> Self {
Self {
_opaque: [0u8; PTHREAD_MUTEXATTR_T_SIZE],
}
}
}
const PTHREAD_COND_T_SIZE: usize = 48;
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub(super) struct pthread_cond_t {
_opaque: [u8; PTHREAD_COND_T_SIZE],
}
impl Default for pthread_cond_t {
fn default() -> Self {
Self {
_opaque: [0u8; PTHREAD_COND_T_SIZE],
}
}
}
impl pthread_cond_t {
pub(super) fn is_empty(&self) -> bool {
self._opaque.iter().all(|&b| b == 0)
}
}
#[cfg(any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "arm",
target_arch = "riscv64",
target_arch = "riscv32",
))]
const PTHREAD_CONDATTR_T_SIZE: usize = 4;
#[cfg(target_arch = "aarch64")]
const PTHREAD_CONDATTR_T_SIZE: usize = 8;
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub(super) struct pthread_condattr_t {
_opaque: [u8; PTHREAD_CONDATTR_T_SIZE],
}
impl Default for pthread_condattr_t {
fn default() -> Self {
Self {
_opaque: [0u8; PTHREAD_CONDATTR_T_SIZE],
}
}
}
pub(super) type pthread_once_t = c_int;
pub(super) const PTHREAD_ONCE_INIT: pthread_once_t = 0;
pub(super) type PthreadOnceRoutine = unsafe extern "C" fn();
pub(super) type ThreadStartRoutine = unsafe extern "C" fn(arg: *mut c_void) -> *mut c_void;
const SIGSET_T_SIZE: usize = 128;
#[repr(C, align(8))]
#[derive(Copy, Clone)]
pub(super) struct sigset_t {
_opaque: [u8; SIGSET_T_SIZE],
}
impl Default for sigset_t {
fn default() -> Self {
Self {
_opaque: [0u8; SIGSET_T_SIZE],
}
}
}
pub(super) type sighandler_t = usize;
#[cfg(feature = "real_time")]
pub(super) const SCHED_FIFO: c_int = 1;
#[cfg(feature = "real_time")]
pub(super) const PTHREAD_EXPLICIT_SCHED: c_int = 1;
#[cfg(feature = "real_time")]
#[repr(C)]
#[derive(Copy, Clone)]
pub(super) struct sched_param {
pub(super) sched_priority: c_int,
}
pub(super) const _SC_PAGESIZE: c_int = 30;
pub(super) const _SC_AVPHYS_PAGES: c_int = 86;
pub(super) const CLOCK_MONOTONIC: c_int = 1;
pub(super) const ETIMEDOUT: c_int = 110;
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub(super) struct timespec {
pub(super) tv_sec: c_long,
pub(super) tv_nsec: c_long,
}
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub(super) struct itimerspec {
pub(super) it_interval: timespec,
pub(super) it_value: timespec,
}
pub(super) type pid_t = c_int;
pub(super) type timer_t = *mut c_void;
#[cfg(target_pointer_width = "64")]
const SIGEV_PAD_SIZE: usize = 12;
#[cfg(target_pointer_width = "32")]
const SIGEV_PAD_SIZE: usize = 13;
#[repr(C)]
#[derive(Copy, Clone)]
pub(super) union sigval_t {
pub(super) sival_int: c_int,
pub(super) sival_ptr: *mut c_void,
}
impl Default for sigval_t {
fn default() -> Self {
Self { sival_ptr: core::ptr::null_mut() }
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub(super) union sigevent_un {
pub(super) pad: [c_int; SIGEV_PAD_SIZE],
pub(super) tid: pid_t,
}
impl Default for sigevent_un {
fn default() -> Self {
Self { pad: [0; SIGEV_PAD_SIZE] }
}
}
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub(super) struct sigevent {
pub(super) sigev_value: sigval_t,
pub(super) sigev_signo: c_int,
pub(super) sigev_notify: c_int,
pub(super) sigev_un: sigevent_un,
}
pub(super) const SIGEV_THREAD_ID: c_int = 4;
pub(super) const SIGALRM: c_int = 14;
pub(super) const SIG_BLOCK: c_int = 0;
pub(super) const PTHREAD_MUTEX_RECURSIVE: c_int = 1;
pub(super) const PTHREAD_PRIO_INHERIT: c_int = 1;
unsafe extern "C" {
pub(super) fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int;
pub(super) fn pthread_attr_setstacksize(attr: *mut pthread_attr_t, stacksize: usize) -> c_int;
#[cfg(feature = "real_time")]
pub(super) fn pthread_attr_setinheritsched(attr: *mut pthread_attr_t, inheritsched: c_int) -> c_int;
#[cfg(feature = "real_time")]
pub(super) fn pthread_attr_setschedpolicy(attr: *mut pthread_attr_t, policy: c_int) -> c_int;
#[cfg(feature = "real_time")]
pub(super) fn pthread_attr_setschedparam(attr: *mut pthread_attr_t, param: *const sched_param) -> c_int;
pub(super) fn pthread_create(
thread: *mut ThreadHandle,
attr: *const pthread_attr_t,
start_routine: Option<ThreadStartRoutine>,
arg: *mut c_void,
) -> c_int;
pub(super) fn pthread_self() -> ThreadHandle;
pub(super) fn pthread_join(thread: ThreadHandle, retval: *mut *mut c_void) -> c_int;
pub(super) fn pthread_setname_np(thread: ThreadHandle, name: *const c_char) -> c_int;
pub(super) fn pthread_kill(thread: ThreadHandle, sig: c_int) -> c_int;
pub(super) fn __libc_current_sigrtmin() -> c_int;
pub(super) fn sigfillset(set: *mut sigset_t) -> c_int;
pub(super) fn sigdelset(set: *mut sigset_t, signum: c_int) -> c_int;
pub(super) fn sigsuspend(mask: *const sigset_t) -> c_int;
pub(super) fn signal(signum: c_int, handler: sighandler_t) -> sighandler_t;
pub(super) fn sysconf(name: c_int) -> c_long;
pub(super) fn clock_gettime(clock_id: c_int, tp: *mut timespec) -> c_int;
pub(super) fn nanosleep(req: *const timespec, rem: *mut timespec) -> c_int;
pub(super) fn sched_yield() -> c_int;
pub(super) fn pthread_mutexattr_init(attr: *mut pthread_mutexattr_t) -> c_int;
pub(super) fn pthread_mutexattr_settype(attr: *mut pthread_mutexattr_t, kind: c_int) -> c_int;
pub(super) fn pthread_mutexattr_setprotocol(attr: *mut pthread_mutexattr_t, protocol: c_int) -> c_int;
pub(super) fn pthread_mutex_init(mutex: *mut pthread_mutex_t, attr: *const pthread_mutexattr_t) -> c_int;
pub(super) fn pthread_mutex_destroy(mutex: *mut pthread_mutex_t) -> c_int;
pub(super) fn pthread_mutex_lock(mutex: *mut pthread_mutex_t) -> c_int;
pub(super) fn pthread_mutex_trylock(mutex: *mut pthread_mutex_t) -> c_int;
pub(super) fn pthread_mutex_unlock(mutex: *mut pthread_mutex_t) -> c_int;
pub(super) fn pthread_once(once_control: *mut pthread_once_t, init_routine: Option<PthreadOnceRoutine>) -> c_int;
pub(super) fn pthread_condattr_init(attr: *mut pthread_condattr_t) -> c_int;
pub(super) fn pthread_condattr_setclock(attr: *mut pthread_condattr_t, clock_id: c_int) -> c_int;
pub(super) fn pthread_cond_init(cond: *mut pthread_cond_t, attr: *const pthread_condattr_t) -> c_int;
pub(super) fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> c_int;
pub(super) fn pthread_cond_wait(cond: *mut pthread_cond_t, mutex: *mut pthread_mutex_t) -> c_int;
pub(super) fn pthread_cond_timedwait(cond: *mut pthread_cond_t, mutex: *mut pthread_mutex_t, abstime: *const timespec) -> c_int;
pub(super) fn pthread_cond_broadcast(cond: *mut pthread_cond_t) -> c_int;
pub(super) fn sigemptyset(set: *mut sigset_t) -> c_int;
pub(super) fn sigaddset(set: *mut sigset_t, signum: c_int) -> c_int;
pub(super) fn sigprocmask(how: c_int, set: *const sigset_t, oldset: *mut sigset_t) -> c_int;
pub(super) fn sigwait(set: *const sigset_t, sig: *mut c_int) -> c_int;
pub(super) fn gettid() -> pid_t;
pub(super) fn timer_create(clockid: c_int, sevp: *const sigevent, timerid: *mut timer_t) -> c_int;
pub(super) fn timer_settime(timerid: timer_t, flags: c_int, new_value: *const itimerspec, old_value: *mut itimerspec) -> c_int;
pub(super) fn timer_delete(timerid: timer_t) -> c_int;
}