chore(signal): Move alarm function and adding the todo
This commit is contained in:
@@ -6,7 +6,7 @@ use core::{
|
||||
use super::{
|
||||
super::{
|
||||
PalSignal,
|
||||
types::{c_int, pid_t},
|
||||
types::{c_int, c_uint, pid_t, time_t},
|
||||
},
|
||||
Sys, e_raw,
|
||||
};
|
||||
@@ -15,8 +15,10 @@ use crate::{
|
||||
header::{
|
||||
bits_time::timespec,
|
||||
signal::{SA_RESTORER, SI_QUEUE, sigaction, siginfo_t, sigset_t, sigval, stack_t},
|
||||
sys_time::itimerval,
|
||||
sys_select::timeval,
|
||||
sys_time::{self, itimerval},
|
||||
},
|
||||
platform,
|
||||
};
|
||||
|
||||
impl PalSignal for Sys {
|
||||
@@ -68,6 +70,26 @@ impl PalSignal for Sys {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/alarm.html>.
|
||||
fn alarm(seconds: c_uint) -> c_uint {
|
||||
let timer = itimerval {
|
||||
it_value: timeval {
|
||||
tv_sec: time_t::from(seconds),
|
||||
tv_usec: 0,
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
let mut otimer = itimerval::default();
|
||||
let errno_backup = platform::ERRNO.get();
|
||||
let secs = if Self::setitimer(sys_time::ITIMER_REAL, &timer, Some(&mut otimer)).is_err() {
|
||||
0
|
||||
} else {
|
||||
otimer.it_value.tv_sec as c_uint + if otimer.it_value.tv_usec > 0 { 1 } else { 0 }
|
||||
};
|
||||
platform::ERRNO.set(errno_backup);
|
||||
secs
|
||||
}
|
||||
|
||||
fn sigaction(
|
||||
sig: c_int,
|
||||
act: Option<&sigaction>,
|
||||
|
||||
@@ -4,9 +4,8 @@ use crate::{
|
||||
header::{
|
||||
bits_time::timespec,
|
||||
signal::{sigaction, siginfo_t, sigset_t, sigval, stack_t},
|
||||
sys_time::{self, itimerval},
|
||||
sys_time::itimerval,
|
||||
},
|
||||
platform,
|
||||
};
|
||||
|
||||
pub trait PalSignal: Pal {
|
||||
@@ -22,31 +21,7 @@ pub trait PalSignal: Pal {
|
||||
|
||||
fn setitimer(which: c_int, new: &itimerval, old: Option<&mut itimerval>) -> Result<()>;
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/alarm.html>.
|
||||
///
|
||||
/// Default implementation uses setitimer(ITIMER_REAL). Platforms that do not
|
||||
/// support setitimer (e.g. Redox) must override this.
|
||||
fn alarm(seconds: c_uint) -> c_uint {
|
||||
use crate::header::sys_select::timeval;
|
||||
|
||||
let timer = itimerval {
|
||||
it_value: timeval {
|
||||
tv_sec: time_t::from(seconds),
|
||||
tv_usec: 0,
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
let mut otimer = itimerval::default();
|
||||
|
||||
let errno_backup = platform::ERRNO.get();
|
||||
let secs = if Self::setitimer(sys_time::ITIMER_REAL, &timer, Some(&mut otimer)).is_err() {
|
||||
0
|
||||
} else {
|
||||
otimer.it_value.tv_sec as c_uint + if otimer.it_value.tv_usec > 0 { 1 } else { 0 }
|
||||
};
|
||||
platform::ERRNO.set(errno_backup);
|
||||
secs
|
||||
}
|
||||
fn alarm(seconds: c_uint) -> c_uint;
|
||||
|
||||
fn sigaction(sig: c_int, act: Option<&sigaction>, oact: Option<&mut sigaction>) -> Result<()>;
|
||||
|
||||
|
||||
@@ -278,6 +278,10 @@ impl PalSignal for Sys {
|
||||
/// Accepts a full timespec so sub-second timers (ualarm) can reuse this later.
|
||||
/// Returns the number of seconds remaining on the previous alarm (rounded up),
|
||||
/// or 0 if there was no previous alarm.
|
||||
///
|
||||
/// TODO: This implementation does not survive `exec()`. POSIX requires that a
|
||||
/// pending alarm be preserved across exec (the timer continues counting down
|
||||
/// in the new process image as i understand).
|
||||
fn alarm_timespec(duration: timespec) -> c_uint {
|
||||
let mut guard = ALARM_TIMER.lock();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user