phunix/minix/kernel/system/do_setalarm.c
David van Moolenbroek cfd712b424 Various timer improvements
Now that clock_t is an unsigned value, we can also allow the system
uptime to wrap.  Essentially, instead of using (a <= b) to see if time
a occurs no later than time b, we use (b - a <= CLOCK_MAX / 2).  The
latter value does not exist, so instead we add TMRDIFF_MAX for that
purpose.

We must therefore also avoid using values like 0 and LONG_MAX as
special values for absolute times.  This patch extends the libtimers
interface so that it no longer uses 0 to indicate "no timeout".
Similarly, TMR_NEVER is now used as special value only when
otherwise a relative time difference would be used.  A minix_timer
structure is now considered in use when it has a watchdog function set,
rather than when the absolute expiry time is not TMR_NEVER.  A few new
macros in <minix/timers.h> help with timer comparison and obtaining
properties from a minix_timer structure.

This patch also eliminates the union of timer arguments, instead using
the only union element that is only used (the integer).  This prevents
potential problems with e.g. live update.  The watchdog function
prototype is changed to pass in the argument value rather than a
pointer to the timer structure, since obtaining the argument value was
the only current use of the timer structure anyway.  The result is a
somewhat friendlier timers API.

The VFS select code required a few more invasive changes to restrict
the timer value to the new maximum, effectively matching the timer
code in PM.  As a side effect, select(2) has been changed to reject
invalid timeout values.  That required a change to the test set, which
relied on the previous, erroneous behavior.

Finally, while we're rewriting significant chunks of the timer code
anyway, also covert it to KNF and add a few more explanatory comments.

Change-Id: Id43165c3fbb140b32b90be2cca7f68dd646ea72e
2016-08-05 11:12:44 +00:00

79 lines
2.7 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(int proc_nr_e);
/*===========================================================================*
* 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;
use_abs_time = m_ptr->m_lsys_krn_sys_setalarm.abs_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);
/* Return the ticks left on the previous alarm. */
uptime = get_monotonic();
if (!tmr_is_set(tp)) {
m_ptr->m_lsys_krn_sys_setalarm.time_left = TMR_NEVER;
} else if (tmr_is_first(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. Note that
* an absolute time of zero is as valid as any other absolute value, so only
* a relative time value of zero resets the timer.
*/
if (!use_abs_time && exp_time == 0) {
reset_kernel_timer(tp);
} else {
if (!use_abs_time)
exp_time += uptime;
set_kernel_timer(tp, exp_time, cause_alarm, caller->p_endpoint);
}
return(OK);
}
/*===========================================================================*
* cause_alarm *
*===========================================================================*/
static void cause_alarm(int proc_nr_e)
{
/* Routine called if a timer goes off and the process requested a synchronous
* alarm. The process number is stored as the timer argument. Notify that
* process with a notification message from CLOCK.
*/
mini_notify(proc_addr(CLOCK), proc_nr_e); /* notify process */
}
#endif /* USE_SETALARM */