Module: xenomai-forge Branch: next Commit: 2fff60141c221f0065addab35e99e970dfb0e9cc URL: http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=2fff60141c221f0065addab35e99e970dfb0e9cc
Author: Philippe Gerum <r...@xenomai.org> Date: Fri Feb 7 18:33:42 2014 +0100 copperplate/clockobj: add clockobj_get_distance() This helper computes the distance between the current time and the next shot from an interval timer. --- include/copperplate/clockobj.h | 4 ++++ lib/copperplate/clockobj.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/include/copperplate/clockobj.h b/include/copperplate/clockobj.h index bdba790..56d24b4 100644 --- a/include/copperplate/clockobj.h +++ b/include/copperplate/clockobj.h @@ -61,6 +61,10 @@ void clockobj_get_date(struct clockobj *clkobj, ticks_t *pticks); void clockobj_get_time(struct clockobj *clkobj, ticks_t *pticks, ticks_t *ptsc); +void clockobj_get_distance(struct clockobj *clkobj, + const struct itimerspec *itm, + struct timespec *delta); + ticks_t clockobj_get_tsc(void); void clockobj_caltime_to_timeout(struct clockobj *clkobj, const struct tm *tm, diff --git a/lib/copperplate/clockobj.c b/lib/copperplate/clockobj.c index 3fda138..1a22ab2 100644 --- a/lib/copperplate/clockobj.c +++ b/lib/copperplate/clockobj.c @@ -25,6 +25,7 @@ #include <time.h> #include <string.h> #include "boilerplate/lock.h" +#include "boilerplate/time.h" #include "copperplate/clockobj.h" #include "copperplate/debug.h" #include "internal.h" @@ -205,11 +206,12 @@ void clockobj_set_date(struct clockobj *clkobj, ticks_t ticks) } /* - * XXX: clockobj_set_resolution() should be called during the init - * phase, not after. For performance reason, we want to run locklessly - * for common time unit conversions, so the clockobj implementation - * does assume that the clock resolution will not be updated - * on-the-fly. + * CAUTION: clockobj_set_resolution() may be called during the init + * phase only, not after. IOW, the resolution is perceived as a + * constant when the application code executes. For performance + * reason, we want to run locklessly for common time unit conversions, + * so the clockobj implementation does assume that the clock + * resolution will NOT be updated after the init phase. */ int clockobj_set_resolution(struct clockobj *clkobj, unsigned int resolution_ns) { @@ -354,6 +356,27 @@ void clockobj_convert_clocks(struct clockobj *clkobj, timespec_add(out, &delta, &now); } +void clockobj_get_distance(struct clockobj *clkobj, + const struct itimerspec *itm, + struct timespec *delta) +{ + ticks_t now, start, interval, dist; + + clockobj_get_time(clkobj, &now, NULL); + start = timespec_scalar(&itm->it_value); + + if (start >= now) + /* Distance to first shot. */ + dist = start - now; + else { + interval = timespec_scalar(&itm->it_interval); + /* Distance to next shot. */ + dist = interval - ((now - start) % interval); + } + + __clockobj_ticks_to_timespec(clkobj, dist, delta); +} + int clockobj_init(struct clockobj *clkobj, const char *name, unsigned int resolution_ns) { _______________________________________________ Xenomai-git mailing list Xenomai-git@xenomai.org http://www.xenomai.org/mailman/listinfo/xenomai-git