Misc improvements, move barrier to safe module.

This commit is contained in:
4lDO2
2023-04-12 12:27:01 +02:00
parent 316203102a
commit b455e2e374
6 changed files with 348 additions and 96 deletions
+55 -17
View File
@@ -1,18 +1,27 @@
use super::*;
use crate::header::errno::EBUSY;
use crate::header::errno::*;
use core::sync::atomic::AtomicI32 as AtomicInt;
// PTHREAD_MUTEX_INITIALIZER
// PTHREAD_MUTEX_INITIALIZER is defined in bits_pthread/cbindgen.toml
#[repr(u8)]
enum State {
Unlocked,
Locked,
Waiting,
}
// #[no_mangle]
pub extern "C" fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> c_int {
todo!();
pub unsafe extern "C" fn pthread_mutex_consistent(mutex: *mut pthread_mutex_t) -> c_int {
let mutex = &*mutex.cast::<RlctMutex>();
todo!()
}
#[no_mangle]
pub unsafe extern "C" fn pthread_mutex_destroy(mutex: *mut pthread_mutex_t) -> c_int {
let _mutex: &pthread_mutex_t = &*mutex;
let _mutex = &mut *mutex.cast::<RlctMutex>();
0
}
@@ -23,11 +32,20 @@ pub extern "C" fn pthread_mutex_getprioceiling(mutex: *const pthread_mutex_t, pr
#[no_mangle]
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 = attr.cast::<RlctMutexAttr>().as_ref().copied().unwrap_or_default();
// TODO: attr
mutex.cast::<RlctMutex>().write(RlctMutex {
inner: crate::sync::mutex::UNLOCKED.into(),
/*robust: attr.robust != 0,
ty: match attr.ty {
PTHREAD_MUTEX_DEFAULT => Ty::Def,
PTHREAD_MUTEX_ERRORCHECK => Ty::Errck,
PTHREAD_MUTEX_RECURSIVE => Ty::Recursive,
PTHREAD_MUTEX_NORMAL => Ty::Normal,
_ => return EINVAL,
}*/
});
0
}
@@ -68,7 +86,10 @@ pub unsafe extern "C" fn pthread_mutex_unlock(mutex: *mut pthread_mutex_t) -> c_
}
#[no_mangle]
pub extern "C" fn pthread_mutexattr_destroy(_attr: *mut pthread_mutexattr_t) -> c_int {
pub unsafe extern "C" fn pthread_mutexattr_destroy(attr: *mut pthread_mutexattr_t) -> c_int {
// No-op
core::ptr::drop_in_place(attr);
0
}
@@ -102,14 +123,7 @@ pub unsafe extern "C" fn pthread_mutexattr_gettype(attr: *const pthread_mutexatt
}
#[no_mangle]
pub unsafe extern "C" fn pthread_mutexattr_init(attr: *mut pthread_mutexattr_t) -> c_int {
attr.cast::<RlctMutexAttr>().write(RlctMutexAttr {
robust: PTHREAD_MUTEX_STALLED,
pshared: PTHREAD_PROCESS_PRIVATE,
protocol: PTHREAD_PRIO_NONE,
// TODO
prioceiling: 0,
ty: PTHREAD_MUTEX_DEFAULT,
});
attr.cast::<RlctMutexAttr>().write(RlctMutexAttr::default());
0
}
@@ -144,10 +158,23 @@ pub unsafe extern "C" fn pthread_mutexattr_settype(attr: *mut pthread_mutexattr_
#[repr(C)]
pub(crate) struct RlctMutex {
pub inner: AtomicInt,
// Actual locking word. Allows the states UNLOCKED, LOCKED, and WAITING, a substate of LOCKED.
inner: AtomicInt,
/*robust: bool,
ty: Ty,*/
// TODO: Robust mutexes
}
enum Ty {
Normal,
Def,
Errck,
Recursive,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub(crate) struct RlctMutexAttr {
pub prioceiling: c_int,
pub protocol: c_int,
@@ -155,4 +182,15 @@ pub(crate) struct RlctMutexAttr {
pub robust: c_int,
pub ty: c_int,
}
impl Default for RlctMutexAttr {
fn default() -> Self {
Self {
robust: PTHREAD_MUTEX_STALLED,
pshared: PTHREAD_PROCESS_PRIVATE,
protocol: PTHREAD_PRIO_NONE,
// TODO
prioceiling: 0,
ty: PTHREAD_MUTEX_DEFAULT,
}
}
}