Put timespec convert into common fn
This commit is contained in:
@@ -2,7 +2,10 @@ use super::*;
|
||||
|
||||
use crate::{
|
||||
error::Errno,
|
||||
header::errno::{EINVAL, ENOMEM, ETIMEDOUT},
|
||||
header::{
|
||||
errno::{EINVAL, ENOMEM, ETIMEDOUT},
|
||||
time::{CLOCK_MONOTONIC, CLOCK_REALTIME, timespec_realtime_to_monotonic},
|
||||
},
|
||||
};
|
||||
|
||||
// PTHREAD_MUTEX_INITIALIZER is defined in bits_pthread/cbindgen.toml
|
||||
@@ -77,24 +80,14 @@ pub unsafe extern "C" fn pthread_mutex_timedlock(
|
||||
mutex: *mut pthread_mutex_t,
|
||||
abstime: *const timespec,
|
||||
) -> c_int {
|
||||
// convert REALTIME_CLOCK to MONOTONIC_CLOCK
|
||||
|
||||
let relative = {
|
||||
let mut realtime = timespec::default();
|
||||
unsafe { clock_gettime(CLOCK_REALTIME, &mut realtime) };
|
||||
let mut monotonic = timespec::default();
|
||||
unsafe { clock_gettime(CLOCK_MONOTONIC, &mut monotonic) };
|
||||
let Some(delta) = timespec::subtract(*abstime.clone(), realtime) else {
|
||||
return e(Err(Errno(ETIMEDOUT)));
|
||||
};
|
||||
let Some(relative) = timespec::add(monotonic, delta) else {
|
||||
return e(Err(Errno(ENOMEM)));
|
||||
};
|
||||
relative
|
||||
let relative = match timespec_realtime_to_monotonic(*abstime) {
|
||||
Ok(relative) => relative,
|
||||
Err(err) => return e(Err(err)),
|
||||
};
|
||||
|
||||
e((&*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())
|
||||
|
||||
Reference in New Issue
Block a user