minix/minix/kernel/system/do_setalarm.c
David van Moolenbroek e10ce184e4 libsys: make tickdelay(3) more reliable
Previously, there was a tiny chance that tickdelay(3) would return
early or that it would fail to reinstate a previous alarm.

- sys_setalarm(2) now returns TMR_NEVER instead of 0 for the time
  left if no previous alarm was set;
- sys_setalarm(2) now also returns the current time, to allow the
  caller to determine whether it got an alarm notification for the
  alarm it set or for a previous alarm that has just gone off;
- tickdelay(3) now makes use of these facilities.

Change-Id: Id4f8fe19a61ca8574f43131964e6f0317f613f49
2015-08-08 16:55:23 +00:00

77 lines
2.8 KiB
C

/* The kernel call implemented in this file:
* m_type: SYS_SETALARM
*
* The parameters for this kernel call are:
* m_lsys_krn_sys_setalarm.exp_time (alarm's expiration time)
* m_lsys_krn_sys_setalarm.abs_time (expiration time is absolute?)
* m_lsys_krn_sys_setalarm.time_left (return seconds left of previous)
*/
#include "kernel/system.h"
#include <minix/endpoint.h>
#include <assert.h>
#if USE_SETALARM
static void cause_alarm(minix_timer_t *tp);
/*===========================================================================*
* do_setalarm *
*===========================================================================*/
int do_setalarm(struct proc * caller, message * m_ptr)
{
/* A process requests a synchronous alarm, or wants to cancel its alarm. */
long exp_time; /* expiration time for this alarm */
int use_abs_time; /* use absolute or relative time */
minix_timer_t *tp; /* the process' timer structure */
clock_t uptime; /* placeholder for current uptime */
/* Extract shared parameters from the request message. */
exp_time = m_ptr->m_lsys_krn_sys_setalarm.exp_time; /* alarm's expiration time */
use_abs_time = m_ptr->m_lsys_krn_sys_setalarm.abs_time; /* flag for absolute time */
if (! (priv(caller)->s_flags & SYS_PROC)) return(EPERM);
/* Get the timer structure and set the parameters for this alarm. */
tp = &(priv(caller)->s_alarm_timer);
tmr_arg(tp)->ta_int = caller->p_endpoint;
tp->tmr_func = cause_alarm;
/* Return the ticks left on the previous alarm. */
uptime = get_monotonic();
if (tp->tmr_exp_time == TMR_NEVER) {
m_ptr->m_lsys_krn_sys_setalarm.time_left = TMR_NEVER;
} else if (uptime < tp->tmr_exp_time) {
m_ptr->m_lsys_krn_sys_setalarm.time_left = (tp->tmr_exp_time - uptime);
} else {
m_ptr->m_lsys_krn_sys_setalarm.time_left = 0;
}
/* For the caller's convenience, also return the current time. */
m_ptr->m_lsys_krn_sys_setalarm.uptime = uptime;
/* Finally, (re)set the timer depending on the expiration time. */
if (exp_time == 0) {
reset_kernel_timer(tp);
} else {
tp->tmr_exp_time = (use_abs_time) ? exp_time : exp_time + get_monotonic();
set_kernel_timer(tp, tp->tmr_exp_time, tp->tmr_func);
}
return(OK);
}
/*===========================================================================*
* cause_alarm *
*===========================================================================*/
static void cause_alarm(minix_timer_t *tp)
{
/* Routine called if a timer goes off and the process requested a synchronous
* alarm. The process number is stored in timer argument 'ta_int'. Notify that
* process with a notification message from CLOCK.
*/
endpoint_t proc_nr_e = tmr_arg(tp)->ta_int; /* get process number */
mini_notify(proc_addr(CLOCK), proc_nr_e); /* notify process */
}
#endif /* USE_SETALARM */