Use unsafe blocks in pthread.h implementation

This commit is contained in:
sourceturner
2026-01-18 21:09:17 +01:00
parent e57ef36d3c
commit ac449a4338
9 changed files with 168 additions and 131 deletions
+26 -23
View File
@@ -1,3 +1,6 @@
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use core::{ptr, sync::atomic::Ordering};
use super::*;
@@ -36,7 +39,7 @@ impl Default for RlctAttr {
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_attr_destroy(attr: *mut pthread_attr_t) -> c_int {
core::ptr::drop_in_place(attr);
unsafe { core::ptr::drop_in_place(attr) };
0
}
@@ -45,7 +48,7 @@ pub unsafe extern "C" fn pthread_attr_getdetachstate(
attr: *const pthread_attr_t,
detachstate: *mut c_int,
) -> c_int {
core::ptr::write(detachstate, (*attr.cast::<RlctAttr>()).detachstate as _);
unsafe { core::ptr::write(detachstate, (*attr.cast::<RlctAttr>()).detachstate as _) };
0
}
@@ -54,7 +57,7 @@ pub unsafe extern "C" fn pthread_attr_getguardsize(
attr: *const pthread_attr_t,
size: *mut size_t,
) -> c_int {
core::ptr::write(size, (*attr.cast::<RlctAttr>()).guardsize);
unsafe { core::ptr::write(size, (*attr.cast::<RlctAttr>()).guardsize) };
0
}
@@ -63,7 +66,7 @@ pub unsafe extern "C" fn pthread_attr_getinheritsched(
attr: *const pthread_attr_t,
inheritsched: *mut c_int,
) -> c_int {
core::ptr::write(inheritsched, (*attr.cast::<RlctAttr>()).inheritsched as _);
unsafe { core::ptr::write(inheritsched, (*attr.cast::<RlctAttr>()).inheritsched as _) };
0
}
@@ -72,7 +75,7 @@ pub unsafe extern "C" fn pthread_attr_getschedparam(
attr: *const pthread_attr_t,
param: *mut sched_param,
) -> c_int {
param.write((*attr.cast::<RlctAttr>()).param);
unsafe { param.write((*attr.cast::<RlctAttr>()).param) };
0
}
@@ -81,7 +84,7 @@ pub unsafe extern "C" fn pthread_attr_getschedpolicy(
attr: *const pthread_attr_t,
policy: *mut c_int,
) -> c_int {
core::ptr::write(policy, (*attr.cast::<RlctAttr>()).schedpolicy as _);
unsafe { core::ptr::write(policy, (*attr.cast::<RlctAttr>()).schedpolicy as _) };
0
}
@@ -90,7 +93,7 @@ pub unsafe extern "C" fn pthread_attr_getscope(
attr: *const pthread_attr_t,
scope: *mut c_int,
) -> c_int {
core::ptr::write(scope, (*attr.cast::<RlctAttr>()).scope as _);
unsafe { core::ptr::write(scope, (*attr.cast::<RlctAttr>()).scope as _) };
0
}
@@ -100,8 +103,8 @@ pub unsafe extern "C" fn pthread_attr_getstack(
stackaddr: *mut *mut c_void,
stacksize: *mut size_t,
) -> c_int {
core::ptr::write(stackaddr, (*attr.cast::<RlctAttr>()).stack as _);
core::ptr::write(stacksize, (*attr.cast::<RlctAttr>()).stacksize as _);
unsafe { core::ptr::write(stackaddr, (*attr.cast::<RlctAttr>()).stack as _) };
unsafe { core::ptr::write(stacksize, (*attr.cast::<RlctAttr>()).stacksize as _) };
0
}
@@ -110,13 +113,13 @@ pub unsafe extern "C" fn pthread_attr_getstacksize(
attr: *const pthread_attr_t,
stacksize: *mut size_t,
) -> c_int {
core::ptr::write(stacksize, (*attr.cast::<RlctAttr>()).stacksize as _);
unsafe { core::ptr::write(stacksize, (*attr.cast::<RlctAttr>()).stacksize as _) };
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_attr_init(attr: *mut pthread_attr_t) -> c_int {
core::ptr::write(attr.cast::<RlctAttr>(), RlctAttr::default());
unsafe { core::ptr::write(attr.cast::<RlctAttr>(), RlctAttr::default()) };
0
}
@@ -125,7 +128,7 @@ pub unsafe extern "C" fn pthread_attr_setdetachstate(
attr: *mut pthread_attr_t,
detachstate: c_int,
) -> c_int {
(*attr.cast::<RlctAttr>()).detachstate = detachstate as _;
(unsafe { *attr.cast::<RlctAttr>() }).detachstate = detachstate as _;
0
}
@@ -134,7 +137,7 @@ pub unsafe extern "C" fn pthread_attr_setguardsize(
attr: *mut pthread_attr_t,
guardsize: c_int,
) -> c_int {
(*attr.cast::<RlctAttr>()).guardsize = guardsize as _;
(unsafe { *attr.cast::<RlctAttr>() }).guardsize = guardsize as _;
0
}
@@ -143,7 +146,7 @@ pub unsafe extern "C" fn pthread_attr_setinheritsched(
attr: *mut pthread_attr_t,
inheritsched: c_int,
) -> c_int {
(*attr.cast::<RlctAttr>()).inheritsched = inheritsched as _;
(unsafe { *attr.cast::<RlctAttr>() }).inheritsched = inheritsched as _;
0
}
@@ -152,7 +155,7 @@ pub unsafe extern "C" fn pthread_attr_setschedparam(
attr: *mut pthread_attr_t,
param: *const sched_param,
) -> c_int {
(*attr.cast::<RlctAttr>()).param = param.read();
(unsafe { *attr.cast::<RlctAttr>() }).param = unsafe { param.read() };
0
}
@@ -161,13 +164,13 @@ pub unsafe extern "C" fn pthread_attr_setschedpolicy(
attr: *mut pthread_attr_t,
policy: c_int,
) -> c_int {
(*attr.cast::<RlctAttr>()).schedpolicy = policy as u8;
(unsafe { *attr.cast::<RlctAttr>() }).schedpolicy = policy as u8;
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_attr_setscope(attr: *mut pthread_attr_t, scope: c_int) -> c_int {
(*attr.cast::<RlctAttr>()).scope = scope as u8;
(unsafe { *attr.cast::<RlctAttr>() }).scope = scope as u8;
0
}
@@ -177,8 +180,8 @@ pub unsafe extern "C" fn pthread_attr_setstack(
stackaddr: *mut c_void,
stacksize: size_t,
) -> c_int {
(*attr.cast::<RlctAttr>()).stack = stackaddr as usize;
(*attr.cast::<RlctAttr>()).stacksize = stacksize;
(unsafe { *attr.cast::<RlctAttr>() }).stack = stackaddr as usize;
(unsafe { *attr.cast::<RlctAttr>() }).stacksize = stacksize;
0
}
@@ -187,7 +190,7 @@ pub unsafe extern "C" fn pthread_attr_setstacksize(
attr: *mut pthread_attr_t,
stacksize: size_t,
) -> c_int {
(*attr.cast::<RlctAttr>()).stacksize = stacksize;
(unsafe { *attr.cast::<RlctAttr>() }).stacksize = stacksize;
0
}
@@ -196,10 +199,10 @@ pub unsafe extern "C" fn pthread_getattr_np(
thread_ptr: pthread_t,
attr_ptr: *mut pthread_attr_t,
) -> c_int {
let thread = &*thread_ptr.cast::<Pthread>();
let thread = unsafe { &*thread_ptr.cast::<Pthread>() };
let attr_ptr = attr_ptr.cast::<RlctAttr>();
ptr::write(attr_ptr, RlctAttr::default());
let attr = &mut *attr_ptr;
unsafe { ptr::write(attr_ptr, RlctAttr::default()) };
let attr = unsafe { &mut *attr_ptr };
if thread.flags.load(Ordering::Acquire) & PthreadFlags::DETACHED.bits() != 0 {
attr.detachstate = PTHREAD_CREATE_DETACHED as _;
}
+11 -10
View File
@@ -1,3 +1,6 @@
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use crate::header::errno::*;
use core::num::NonZeroU32;
@@ -27,7 +30,7 @@ pub unsafe extern "C" fn pthread_barrier_destroy(barrier: *mut pthread_barrier_t
// Behavior is undefined if any thread is currently waiting when this is called.
// No-op, currently.
core::ptr::drop_in_place(barrier.cast::<RlctBarrier>());
unsafe { core::ptr::drop_in_place(barrier.cast::<RlctBarrier>()) };
0
}
@@ -39,9 +42,7 @@ pub unsafe extern "C" fn pthread_barrier_init(
attr: *const pthread_barrierattr_t,
count: c_uint,
) -> c_int {
let attr = attr
.cast::<RlctBarrierAttr>()
.as_ref()
let attr = unsafe { attr.cast::<RlctBarrierAttr>().as_ref() }
.copied()
.unwrap_or_default();
@@ -49,7 +50,7 @@ pub unsafe extern "C" fn pthread_barrier_init(
return EINVAL;
};
barrier.cast::<RlctBarrier>().write(RlctBarrier::new(count));
unsafe { barrier.cast::<RlctBarrier>().write(RlctBarrier::new(count)) };
0
}
@@ -60,7 +61,7 @@ fn unlikely(condition: bool) -> bool {
// Not async-signal-safe.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_barrier_wait(barrier: *mut pthread_barrier_t) -> c_int {
let barrier = &*barrier.cast::<RlctBarrier>();
let barrier = unsafe { &*barrier.cast::<RlctBarrier>() };
match barrier.wait() {
WaitResult::NotifiedAll => PTHREAD_BARRIER_SERIAL_THREAD,
@@ -71,7 +72,7 @@ pub unsafe extern "C" fn pthread_barrier_wait(barrier: *mut pthread_barrier_t) -
// Not async-signal-safe.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_barrierattr_init(attr: *mut pthread_barrierattr_t) -> c_int {
core::ptr::write(attr.cast::<RlctBarrierAttr>(), RlctBarrierAttr::default());
unsafe { core::ptr::write(attr.cast::<RlctBarrierAttr>(), RlctBarrierAttr::default()) };
0
}
@@ -82,7 +83,7 @@ pub unsafe extern "C" fn pthread_barrierattr_setpshared(
attr: *mut pthread_barrierattr_t,
pshared: c_int,
) -> c_int {
(*attr.cast::<RlctBarrierAttr>()).pshared = pshared;
(unsafe { *attr.cast::<RlctBarrierAttr>() }).pshared = pshared;
0
}
@@ -92,13 +93,13 @@ pub unsafe extern "C" fn pthread_barrierattr_getpshared(
attr: *const pthread_barrierattr_t,
pshared: *mut c_int,
) -> c_int {
core::ptr::write(pshared, (*attr.cast::<RlctBarrierAttr>()).pshared);
unsafe { core::ptr::write(pshared, (*attr.cast::<RlctBarrierAttr>()).pshared) };
0
}
// Not async-signal-safe.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_barrierattr_destroy(attr: *mut pthread_barrierattr_t) -> c_int {
core::ptr::drop_in_place(attr);
unsafe { core::ptr::drop_in_place(attr) };
0
}
+26 -19
View File
@@ -1,5 +1,8 @@
// Used design from https://www.remlab.net/op/futex-condvar.shtml
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use crate::header::time::CLOCK_REALTIME;
use super::*;
@@ -8,13 +11,13 @@ use super::*;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_cond_broadcast(cond: *mut pthread_cond_t) -> c_int {
e((&*cond.cast::<RlctCond>()).broadcast())
e((unsafe { &*cond.cast::<RlctCond>() }).broadcast())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_cond_destroy(cond: *mut pthread_cond_t) -> c_int {
// No-op
core::ptr::drop_in_place(cond.cast::<RlctCond>());
unsafe { core::ptr::drop_in_place(cond.cast::<RlctCond>()) };
0
}
@@ -23,9 +26,7 @@ pub unsafe extern "C" fn pthread_cond_init(
cond: *mut pthread_cond_t,
attr: *const pthread_condattr_t,
) -> c_int {
let attr = attr
.cast::<RlctCondAttr>()
.as_ref()
let attr = unsafe { attr.cast::<RlctCondAttr>().as_ref() }
.copied()
.unwrap_or_default();
@@ -34,14 +35,14 @@ pub unsafe extern "C" fn pthread_cond_init(
println!("TODO: pthread_cond_init with monotonic clock");
}
cond.cast::<RlctCond>().write(RlctCond::new());
unsafe { cond.cast::<RlctCond>().write(RlctCond::new()) };
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_cond_signal(cond: *mut pthread_cond_t) -> c_int {
e((&*cond.cast::<RlctCond>()).signal())
e((unsafe { &*cond.cast::<RlctCond>() }).signal())
}
#[unsafe(no_mangle)]
@@ -50,7 +51,8 @@ pub unsafe extern "C" fn pthread_cond_timedwait(
mutex: *mut pthread_mutex_t,
timeout: *const timespec,
) -> c_int {
e((&*cond.cast::<RlctCond>()).timedwait(&*mutex.cast::<RlctMutex>(), &*timeout))
e((unsafe { &*cond.cast::<RlctCond>() })
.timedwait(unsafe { &*mutex.cast::<RlctMutex>() }, unsafe { &*timeout }))
}
#[unsafe(no_mangle)]
@@ -60,7 +62,11 @@ pub unsafe extern "C" fn pthread_cond_clockwait(
clock_id: clockid_t,
timeout: *const timespec,
) -> c_int {
e((&*cond.cast::<RlctCond>()).clockwait(&*mutex.cast::<RlctMutex>(), &*timeout, clock_id))
e((unsafe { &*cond.cast::<RlctCond>() }).clockwait(
unsafe { &*mutex.cast::<RlctMutex>() },
unsafe { &*timeout },
clock_id,
))
}
#[unsafe(no_mangle)]
@@ -68,12 +74,12 @@ pub unsafe extern "C" fn pthread_cond_wait(
cond: *mut pthread_cond_t,
mutex: *mut pthread_mutex_t,
) -> c_int {
e((&*cond.cast::<RlctCond>()).wait(&*mutex.cast::<RlctMutex>()))
e((unsafe { &*cond.cast::<RlctCond>() }).wait(unsafe { &*mutex.cast::<RlctMutex>() }))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_condattr_destroy(condattr: *mut pthread_condattr_t) -> c_int {
core::ptr::drop_in_place(condattr.cast::<RlctCondAttr>());
unsafe { core::ptr::drop_in_place(condattr.cast::<RlctCondAttr>()) };
// No-op
0
}
@@ -83,7 +89,7 @@ pub unsafe extern "C" fn pthread_condattr_getclock(
condattr: *const pthread_condattr_t,
clock: *mut clockid_t,
) -> c_int {
core::ptr::write(clock, (*condattr.cast::<RlctCondAttr>()).clock);
unsafe { core::ptr::write(clock, (*condattr.cast::<RlctCondAttr>()).clock) };
0
}
@@ -92,16 +98,17 @@ pub unsafe extern "C" fn pthread_condattr_getpshared(
condattr: *const pthread_condattr_t,
pshared: *mut c_int,
) -> c_int {
core::ptr::write(pshared, (*condattr.cast::<RlctCondAttr>()).pshared);
unsafe { core::ptr::write(pshared, (*condattr.cast::<RlctCondAttr>()).pshared) };
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_condattr_init(condattr: *mut pthread_condattr_t) -> c_int {
condattr
.cast::<RlctCondAttr>()
.write(RlctCondAttr::default());
unsafe {
condattr
.cast::<RlctCondAttr>()
.write(RlctCondAttr::default())
};
0
}
@@ -110,7 +117,7 @@ pub unsafe extern "C" fn pthread_condattr_setclock(
condattr: *mut pthread_condattr_t,
clock: clockid_t,
) -> c_int {
(*condattr.cast::<RlctCondAttr>()).clock = clock;
(unsafe { *condattr.cast::<RlctCondAttr>() }).clock = clock;
0
}
@@ -119,7 +126,7 @@ pub unsafe extern "C" fn pthread_condattr_setpshared(
condattr: *mut pthread_condattr_t,
pshared: c_int,
) -> c_int {
(*condattr.cast::<RlctCondAttr>()).pshared = pshared;
(unsafe { *condattr.cast::<RlctCondAttr>() }).pshared = pshared;
0
}
+32 -23
View File
@@ -2,6 +2,9 @@
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/pthread.h.html>.
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use alloc::collections::LinkedList;
use core::{cell::Cell, ptr::NonNull};
@@ -92,7 +95,7 @@ pub static mut fork_hooks: [LinkedList<extern "C" fn()>; 3] = [const { LinkedLis
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_cancel.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_cancel(thread: pthread_t) -> c_int {
match pthread::cancel(&*thread.cast()) {
match unsafe { pthread::cancel(&*thread.cast()) } {
Ok(()) => 0,
Err(Errno(error)) => error,
}
@@ -106,11 +109,11 @@ pub unsafe extern "C" fn pthread_create(
start_routine: extern "C" fn(arg: *mut c_void) -> *mut c_void,
arg: *mut c_void,
) -> c_int {
let attr = attr.cast::<RlctAttr>().as_ref();
let attr = unsafe { attr.cast::<RlctAttr>().as_ref() };
match pthread::create(attr, start_routine, arg) {
match unsafe { pthread::create(attr, start_routine, arg) } {
Ok(ptr) => {
core::ptr::write(pthread, ptr);
unsafe { core::ptr::write(pthread, ptr) };
0
}
Err(Errno(code)) => code,
@@ -120,7 +123,7 @@ pub unsafe extern "C" fn pthread_create(
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_detach.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_detach(pthread: pthread_t) -> c_int {
match pthread::detach(&*pthread.cast()) {
match unsafe { pthread::detach(&*pthread.cast()) } {
Ok(()) => 0,
Err(Errno(errno)) => errno,
}
@@ -160,7 +163,7 @@ pub extern "C" fn pthread_atfork(
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_exit.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_exit(retval: *mut c_void) -> ! {
pthread::exit_current_thread(pthread::Retval(retval))
unsafe { pthread::exit_current_thread(pthread::Retval(retval)) }
}
// Not in latest POSIX, mark as depreciated?
@@ -177,9 +180,9 @@ pub unsafe extern "C" fn pthread_getcpuclockid(
thread: pthread_t,
clock_out: *mut clockid_t,
) -> c_int {
match pthread::get_cpu_clkid(&*thread.cast()) {
match pthread::get_cpu_clkid(unsafe { &*thread.cast() }) {
Ok(clock) => {
clock_out.write(clock);
unsafe { clock_out.write(clock) };
0
}
Err(Errno(error)) => error,
@@ -193,11 +196,10 @@ pub unsafe extern "C" fn pthread_getschedparam(
policy_out: *mut c_int,
param_out: *mut sched_param,
) -> c_int {
match pthread::get_sched_param(&*thread.cast()) {
match pthread::get_sched_param(unsafe { &*thread.cast() }) {
Ok((policy, param)) => {
policy_out.write(policy);
param_out.write(param);
unsafe { policy_out.write(policy) };
unsafe { param_out.write(param) };
0
}
Err(Errno(error)) => error,
@@ -210,10 +212,10 @@ pub use tls::*;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_join.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_join(thread: pthread_t, retval: *mut *mut c_void) -> c_int {
match pthread::join(&*thread.cast()) {
match unsafe { pthread::join(&*thread.cast()) } {
Ok(pthread::Retval(ret)) => {
if !retval.is_null() {
core::ptr::write(retval, ret);
unsafe { core::ptr::write(retval, ret) };
}
0
}
@@ -233,7 +235,7 @@ pub use self::rwlock::*;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_self.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_self() -> pthread_t {
pthread::current_thread().unwrap_unchecked() as *const _ as *mut _
(unsafe { pthread::current_thread().unwrap_unchecked() }) as *const _ as *mut _
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_setcancelstate.html>.
@@ -244,7 +246,7 @@ pub unsafe extern "C" fn pthread_setcancelstate(state: c_int, oldstate: *mut c_i
// POSIX doesn't imply oldstate can be NULL anywhere, but a lot of C code probably
// relies on it...
if let Some(oldstate) = NonNull::new(oldstate) {
oldstate.write(old);
unsafe { oldstate.write(old) };
}
0
}
@@ -260,7 +262,7 @@ pub unsafe extern "C" fn pthread_setcanceltype(ty: c_int, oldty: *mut c_int) ->
// POSIX doesn't imply oldty can be NULL anywhere, but a lot of C code probably relies
// on it...
if let Some(oldty) = NonNull::new(oldty) {
oldty.write(old);
unsafe { oldty.write(old) };
}
0
}
@@ -283,13 +285,20 @@ pub unsafe extern "C" fn pthread_setschedparam(
policy: c_int,
param: *const sched_param,
) -> c_int {
e(pthread::set_sched_param(&*thread.cast(), policy, &*param))
e(pthread::set_sched_param(
unsafe { &*thread.cast() },
policy,
unsafe { &*param },
))
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_setschedprio.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_setschedprio(thread: pthread_t, prio: c_int) -> c_int {
e(pthread::set_sched_priority(&*thread.cast(), prio))
e(pthread::set_sched_priority(
unsafe { &*thread.cast() },
prio,
))
}
pub mod spin;
@@ -298,7 +307,7 @@ pub use self::spin::*;
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/pthread_setcancelstate.html>.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_testcancel() {
pthread::testcancel();
unsafe { pthread::testcancel() };
}
// Must be the same struct as defined in the pthread_cleanup_push macro.
@@ -317,14 +326,14 @@ pub(crate) static CLEANUP_LL_HEAD: Cell<*const CleanupLinkedListEntry> =
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __relibc_internal_pthread_cleanup_push(new_entry: *mut c_void) {
let new_entry = &mut *new_entry.cast::<CleanupLinkedListEntry>();
let new_entry = unsafe { &mut *new_entry.cast::<CleanupLinkedListEntry>() };
new_entry.prev = CLEANUP_LL_HEAD.get().cast();
CLEANUP_LL_HEAD.set(new_entry);
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __relibc_internal_pthread_cleanup_pop(execute: c_int) {
let prev_head = CLEANUP_LL_HEAD.get().read();
let prev_head = unsafe { CLEANUP_LL_HEAD.get().read() };
CLEANUP_LL_HEAD.set(prev_head.prev.cast());
if execute != 0 {
@@ -338,7 +347,7 @@ pub(crate) unsafe fn run_destructor_stack() {
let mut ptr = CLEANUP_LL_HEAD.get();
while !ptr.is_null() {
let entry = ptr.read();
let entry = unsafe { ptr.read() };
ptr = entry.prev.cast();
(entry.routine)(entry.arg);
+28 -27
View File
@@ -1,3 +1,6 @@
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use super::*;
use crate::{
@@ -12,12 +15,12 @@ use crate::{
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> c_int {
e((&*mutex.cast::<RlctMutex>()).make_consistent())
e((unsafe { &*mutex.cast::<RlctMutex>() }).make_consistent())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_mutex_destroy(mutex: *mut pthread_mutex_t) -> c_int {
// No-op
core::ptr::drop_in_place(mutex.cast::<RlctMutex>());
unsafe { core::ptr::drop_in_place(mutex.cast::<RlctMutex>()) };
0
}
@@ -26,9 +29,9 @@ pub unsafe extern "C" fn pthread_mutex_getprioceiling(
mutex: *const pthread_mutex_t,
prioceiling: *mut c_int,
) -> c_int {
match (&*mutex.cast::<RlctMutex>()).prioceiling() {
match (unsafe { &*mutex.cast::<RlctMutex>() }).prioceiling() {
Ok(value) => {
prioceiling.write(value);
unsafe { prioceiling.write(value) };
0
}
Err(Errno(errno)) => errno,
@@ -40,15 +43,13 @@ pub unsafe extern "C" fn pthread_mutex_init(
mutex: *mut pthread_mutex_t,
attr: *const pthread_mutexattr_t,
) -> c_int {
let attr = attr
.cast::<RlctMutexAttr>()
.as_ref()
let attr = unsafe { attr.cast::<RlctMutexAttr>().as_ref() }
.copied()
.unwrap_or_default();
match RlctMutex::new(&attr) {
Ok(new) => {
mutex.cast::<RlctMutex>().write(new);
unsafe { mutex.cast::<RlctMutex>().write(new) };
0
}
@@ -57,7 +58,7 @@ pub unsafe extern "C" fn pthread_mutex_init(
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_mutex_lock(mutex: *mut pthread_mutex_t) -> c_int {
e((&*mutex.cast::<RlctMutex>()).lock())
e((unsafe { &*mutex.cast::<RlctMutex>() }).lock())
}
#[unsafe(no_mangle)]
@@ -66,9 +67,9 @@ pub unsafe extern "C" fn pthread_mutex_setprioceiling(
prioceiling: c_int,
old_prioceiling: *mut c_int,
) -> c_int {
match (&*mutex.cast::<RlctMutex>()).replace_prioceiling(prioceiling) {
match (unsafe { &*mutex.cast::<RlctMutex>() }).replace_prioceiling(prioceiling) {
Ok(old) => {
old_prioceiling.write(old);
unsafe { old_prioceiling.write(old) };
0
}
Err(Errno(errno)) => errno,
@@ -80,27 +81,27 @@ pub unsafe extern "C" fn pthread_mutex_timedlock(
mutex: *mut pthread_mutex_t,
abstime: *const timespec,
) -> c_int {
let relative = match timespec_realtime_to_monotonic(*abstime) {
let relative = match timespec_realtime_to_monotonic(unsafe { *abstime }) {
Ok(relative) => relative,
Err(err) => return e(Err(err)),
};
e((&*mutex.cast::<RlctMutex>()).lock_with_timeout(&relative))
e((unsafe { &*mutex.cast::<RlctMutex>() }).lock_with_timeout(&relative))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_mutex_trylock(mutex: *mut pthread_mutex_t) -> c_int {
e((&*mutex.cast::<RlctMutex>()).try_lock())
e((unsafe { &*mutex.cast::<RlctMutex>() }).try_lock())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_mutex_unlock(mutex: *mut pthread_mutex_t) -> c_int {
e((&*mutex.cast::<RlctMutex>()).unlock())
e((unsafe { &*mutex.cast::<RlctMutex>() }).unlock())
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_mutexattr_destroy(attr: *mut pthread_mutexattr_t) -> c_int {
// No-op
core::ptr::drop_in_place(attr.cast::<RlctMutexAttr>());
unsafe { core::ptr::drop_in_place(attr.cast::<RlctMutexAttr>()) };
0
}
@@ -109,7 +110,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getprioceiling(
attr: *const pthread_mutexattr_t,
prioceiling: *mut c_int,
) -> c_int {
prioceiling.write((*attr.cast::<RlctMutexAttr>()).prioceiling);
unsafe { prioceiling.write((*attr.cast::<RlctMutexAttr>()).prioceiling) };
0
}
@@ -118,7 +119,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getprotocol(
attr: *const pthread_mutexattr_t,
protocol: *mut c_int,
) -> c_int {
protocol.write((*attr.cast::<RlctMutexAttr>()).protocol);
unsafe { protocol.write((*attr.cast::<RlctMutexAttr>()).protocol) };
0
}
@@ -127,7 +128,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getpshared(
attr: *const pthread_mutexattr_t,
pshared: *mut c_int,
) -> c_int {
pshared.write((*attr.cast::<RlctMutexAttr>()).pshared);
unsafe { pshared.write((*attr.cast::<RlctMutexAttr>()).pshared) };
0
}
@@ -136,7 +137,7 @@ pub unsafe extern "C" fn pthread_mutexattr_getrobust(
attr: *const pthread_mutexattr_t,
robust: *mut c_int,
) -> c_int {
robust.write((*attr.cast::<RlctMutexAttr>()).robust);
unsafe { robust.write((*attr.cast::<RlctMutexAttr>()).robust) };
0
}
#[unsafe(no_mangle)]
@@ -144,12 +145,12 @@ pub unsafe extern "C" fn pthread_mutexattr_gettype(
attr: *const pthread_mutexattr_t,
ty: *mut c_int,
) -> c_int {
ty.write((*attr.cast::<RlctMutexAttr>()).ty);
unsafe { ty.write((*attr.cast::<RlctMutexAttr>()).ty) };
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_mutexattr_init(attr: *mut pthread_mutexattr_t) -> c_int {
attr.cast::<RlctMutexAttr>().write(RlctMutexAttr::default());
unsafe { attr.cast::<RlctMutexAttr>().write(RlctMutexAttr::default()) };
0
}
@@ -158,7 +159,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setprioceiling(
attr: *mut pthread_mutexattr_t,
prioceiling: c_int,
) -> c_int {
(*attr.cast::<RlctMutexAttr>()).prioceiling = prioceiling;
(unsafe { *attr.cast::<RlctMutexAttr>() }).prioceiling = prioceiling;
0
}
@@ -167,7 +168,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setprotocol(
attr: *mut pthread_mutexattr_t,
protocol: c_int,
) -> c_int {
(*attr.cast::<RlctMutexAttr>()).protocol = protocol;
(unsafe { *attr.cast::<RlctMutexAttr>() }).protocol = protocol;
0
}
@@ -176,7 +177,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setpshared(
attr: *mut pthread_mutexattr_t,
pshared: c_int,
) -> c_int {
(*attr.cast::<RlctMutexAttr>()).pshared = pshared;
(unsafe { *attr.cast::<RlctMutexAttr>() }).pshared = pshared;
0
}
@@ -185,7 +186,7 @@ pub unsafe extern "C" fn pthread_mutexattr_setrobust(
attr: *mut pthread_mutexattr_t,
robust: c_int,
) -> c_int {
(*attr.cast::<RlctMutexAttr>()).robust = robust;
(unsafe { *attr.cast::<RlctMutexAttr>() }).robust = robust;
0
}
#[unsafe(no_mangle)]
@@ -193,7 +194,7 @@ pub unsafe extern "C" fn pthread_mutexattr_settype(
attr: *mut pthread_mutexattr_t,
ty: c_int,
) -> c_int {
(*attr.cast::<RlctMutexAttr>()).ty = ty;
(unsafe { *attr.cast::<RlctMutexAttr>() }).ty = ty;
0
}
+4 -1
View File
@@ -1,3 +1,6 @@
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use super::*;
// PTHREAD_ONCE_INIT
@@ -7,7 +10,7 @@ pub unsafe extern "C" fn pthread_once(
once: *mut pthread_once_t,
constructor: extern "C" fn(),
) -> c_int {
let once = &*once.cast::<RlctOnce>();
let once = unsafe { &*once.cast::<RlctOnce>() };
// TODO: Cancellation points
+25 -20
View File
@@ -1,3 +1,6 @@
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use super::*;
use crate::header::errno::EBUSY;
@@ -9,21 +12,21 @@ pub unsafe extern "C" fn pthread_rwlock_init(
rwlock: *mut pthread_rwlock_t,
attr: *const pthread_rwlockattr_t,
) -> c_int {
let attr = attr
.cast::<RlctRwlockAttr>()
.as_ref()
let attr = unsafe { attr.cast::<RlctRwlockAttr>().as_ref() }
.copied()
.unwrap_or_default();
rwlock
.cast::<RlctRwlock>()
.write(RlctRwlock::new(attr.pshared));
unsafe {
rwlock
.cast::<RlctRwlock>()
.write(RlctRwlock::new(attr.pshared))
};
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlock_rdlock(rwlock: *mut pthread_rwlock_t) -> c_int {
get(rwlock).acquire_read_lock(None);
unsafe { get(rwlock) }.acquire_read_lock(None);
0
}
@@ -32,7 +35,7 @@ pub unsafe extern "C" fn pthread_rwlock_timedrdlock(
rwlock: *mut pthread_rwlock_t,
timeout: *const timespec,
) -> c_int {
get(rwlock).acquire_read_lock(Some(&*timeout));
unsafe { get(rwlock) }.acquire_read_lock(Some(unsafe { &*timeout }));
0
}
@@ -41,41 +44,43 @@ pub unsafe extern "C" fn pthread_rwlock_timedwrlock(
rwlock: *mut pthread_rwlock_t,
timeout: *const timespec,
) -> c_int {
get(rwlock).acquire_write_lock(Some(&*timeout));
unsafe { get(rwlock) }.acquire_write_lock(Some(unsafe { &*timeout }));
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlock_tryrdlock(rwlock: *mut pthread_rwlock_t) -> c_int {
match get(rwlock).try_acquire_read_lock() {
match unsafe { get(rwlock) }.try_acquire_read_lock() {
Ok(()) => 0,
Err(_) => EBUSY,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlock_trywrlock(rwlock: *mut pthread_rwlock_t) -> c_int {
match get(rwlock).try_acquire_write_lock() {
match unsafe { get(rwlock) }.try_acquire_write_lock() {
Ok(()) => 0,
Err(_) => EBUSY,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlock_unlock(rwlock: *mut pthread_rwlock_t) -> c_int {
get(rwlock).unlock();
unsafe { get(rwlock) }.unlock();
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlock_wrlock(rwlock: *mut pthread_rwlock_t) -> c_int {
get(rwlock).acquire_write_lock(None);
unsafe { get(rwlock) }.acquire_write_lock(None);
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlockattr_init(attr: *mut pthread_rwlockattr_t) -> c_int {
attr.cast::<RlctRwlockAttr>()
.write(RlctRwlockAttr::default());
unsafe {
attr.cast::<RlctRwlockAttr>()
.write(RlctRwlockAttr::default())
};
0
}
@@ -85,7 +90,7 @@ pub unsafe extern "C" fn pthread_rwlockattr_getpshared(
attr: *const pthread_rwlockattr_t,
pshared_out: *mut c_int,
) -> c_int {
core::ptr::write(pshared_out, (*attr.cast::<RlctRwlockAttr>()).pshared.raw());
unsafe { core::ptr::write(pshared_out, (*attr.cast::<RlctRwlockAttr>()).pshared.raw()) };
0
}
@@ -95,7 +100,7 @@ pub unsafe extern "C" fn pthread_rwlockattr_setpshared(
attr: *mut pthread_rwlockattr_t,
pshared: c_int,
) -> c_int {
(*attr.cast::<RlctRwlockAttr>()).pshared =
(unsafe { *attr.cast::<RlctRwlockAttr>() }).pshared =
Pshared::from_raw(pshared).expect("invalid pshared in pthread_rwlockattr_setpshared");
0
@@ -103,13 +108,13 @@ pub unsafe extern "C" fn pthread_rwlockattr_setpshared(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlockattr_destroy(attr: *mut pthread_rwlockattr_t) -> c_int {
core::ptr::drop_in_place(attr);
unsafe { core::ptr::drop_in_place(attr) };
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_rwlock_destroy(rwlock: *mut pthread_rwlock_t) -> c_int {
core::ptr::drop_in_place(rwlock);
unsafe { core::ptr::drop_in_place(rwlock) };
0
}
@@ -122,5 +127,5 @@ pub(crate) struct RlctRwlockAttr {
}
#[inline]
unsafe fn get<'a>(ptr: *mut pthread_rwlock_t) -> &'a RlctRwlock {
&*ptr.cast()
unsafe { &*ptr.cast() }
}
+12 -7
View File
@@ -1,3 +1,6 @@
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use core::sync::atomic::{AtomicI32 as AtomicInt, Ordering};
use crate::header::errno::EBUSY;
@@ -9,7 +12,7 @@ const LOCKED: c_int = 1;
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_spin_destroy(spinlock: *mut pthread_spinlock_t) -> c_int {
let _spinlock = &mut *spinlock.cast::<RlctSpinlock>();
let _spinlock = unsafe { &mut *spinlock.cast::<RlctSpinlock>() };
// No-op
0
@@ -22,15 +25,17 @@ pub unsafe extern "C" fn pthread_spin_init(
// TODO: pshared doesn't matter in most situations, as memory is just memory, but this may be
// different on some architectures...
spinlock.cast::<RlctSpinlock>().write(RlctSpinlock {
inner: AtomicInt::new(UNLOCKED),
});
unsafe {
spinlock.cast::<RlctSpinlock>().write(RlctSpinlock {
inner: AtomicInt::new(UNLOCKED),
})
};
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_spin_lock(spinlock: *mut pthread_spinlock_t) -> c_int {
let spinlock = &*spinlock.cast::<RlctSpinlock>();
let spinlock = unsafe { &*spinlock.cast::<RlctSpinlock>() };
loop {
match spinlock.inner.compare_exchange_weak(
@@ -48,7 +53,7 @@ pub unsafe extern "C" fn pthread_spin_lock(spinlock: *mut pthread_spinlock_t) ->
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_spin_trylock(spinlock: *mut pthread_spinlock_t) -> c_int {
let spinlock = &*spinlock.cast::<RlctSpinlock>();
let spinlock = unsafe { &*spinlock.cast::<RlctSpinlock>() };
match spinlock
.inner
@@ -62,7 +67,7 @@ pub unsafe extern "C" fn pthread_spin_trylock(spinlock: *mut pthread_spinlock_t)
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pthread_spin_unlock(spinlock: *mut pthread_spinlock_t) -> c_int {
let spinlock = &*spinlock.cast::<RlctSpinlock>();
let spinlock = unsafe { &*spinlock.cast::<RlctSpinlock>() };
spinlock.inner.store(UNLOCKED, Ordering::Release);
+4 -1
View File
@@ -1,3 +1,6 @@
// TODO: set this for entire crate when possible
#![deny(unsafe_op_in_unsafe_fn)]
use super::*;
// TODO: Hashmap?
@@ -52,7 +55,7 @@ pub unsafe extern "C" fn pthread_key_create(
},
);
key_ptr.write(key);
unsafe { key_ptr.write(key) };
0
}