[net.sources] timer module

valke@ark.UUCP (02/22/87)

The shell archive below contains a library for manipulating timers.
If you have some real time application this package might come in handy.
It uses ftime(2), so if your system lacks this, you'll have to modify the
source.  However, the module should run without modification on BSD 4.1,
2.9 and old Version 7 UNIX.

Extract everything below and feed it to /bin/sh.  Then read "README".
-------------------------------------------------------------------------------
#!/bin/sh
echo 'Start of pack.out, part 01 of 01:'
echo 'x - Makefile'
sed 's/^X//' > Makefile << '/'
XTARGET=timer.a
XCFLAGS=-O
XLINT=lint -uhbaxpc
XCTAGS=ctags
XPRINT=@pr -t
X
XCFILES=\
X	timer.c
XHFILES=\
X	timer.h\
X	/usr/include/sys/timeb.h\
X	/usr/include/sys/types.h
XOBJECTS=\
X	timer.o
X
X.SUFFIXES: .i
X
X$(TARGET):	$(OBJECTS)
X	ar rv $@ $?
X	ranlib $@
X
Xall:	$(TARGET) timer.man
X	
Xlint:
X	$(LINT) $(PREFLAGS) $(CFILES) -lc
X
Xtags:	$(HFILES) $(CFILES)
X	$(CTAGS) $(HFILES) $(CFILES)
X
Xprint:
X	$(PRINT) $(HFILES) $(FFILES) $(CFILES)
X
X.c.o:
X	$(CC) $(CFLAGS) -c $<
X
X.c.i:
X	$(CC) $(CFLAGS) -P $<
X
X.c.s:
X	$(CC) $(CFLAGS) -S $<
X
Xtimer.o:\
X	/usr/include/sys/types.h\
X	/usr/include/sys/timeb.h\
X	timer.h
X
Xtimer.man:
X	nroff -man timer.3 > timer.man
/
echo 'x - README'
sed 's/^X//' > README << '/'
XTimer module.
XShould run on BSD 4.1, 2.9 and old V7.  To make it run on other systems,
Xyou might have to modify fct. sec_time ("timer.c"), and constant NO_TIME
X("timer.h").
X
XType "make all" to create library "timer.a" and manpage "timer.man"
/
echo 'x - timer.3'
sed 's/^X//' > timer.3 << '/'
X.TH TIMER 3
X.SH NAME
Xtimer, time_left, set_tvec, tvec_timeout, tvec_oldest \- functions for precision timers
X.SH SYNOPSIS
X#include "timer.h"
X
XTIMER timer (sec)
X.br
Xdouble sec;
X
Xdouble time_left (t)
X.br
XTIMER t;
X
Xset_tvec (tvec, n, sec)
X.br
XTIMER tvec [];
X.br
Xunsigned n;
X.br
Xdouble sec;
X
Xunsigned tvec_timeout (tvec, n, si)
X.br
XTIMER tvec [];
X.br
Xunsigned n, si;
X
Xunsigned tvec_oldest (tvec, n)
X.br
XTIMER tvec [];
X.br
Xunsigned n;
X
X.SH DESCRIPTION
XTimer returns a TIMER that will timeout after sec seconds.
XA negative or non-discrete sec is allowed.
X.br
XIf sec is constant NO_TIME (larger than any practical sec),
Xthe resulting TIMER will not timeout at all.
XThis can be used to initialize and turn off timers.
X
XTime_left returns the number of seconds left before t has its timeout.
XThus, if t was last set on sec seconds,
Xthen time_left (t) will return with a value
Xgreater than 0 before sec seconds have elapsed,
Xand less than or equal to 0 afterwards.
XFor value NO_TIME, see timer.
X
XThe functions below deal with multiple timers.
X
XSet_tvec sets the first n timers in TIMER array tvec to timeout after sec
Xseconds.
XFor sec is NO_TIME, see timer.
X
XTvec_timeout does a circular search in tvec from indices si up to n,
Xand then from 0 up to si, and returns the index of the first TIMER that has
Xhad a timeout.
X
XTvec_oldest returns the index of the oldest (and among these the first)
Xof the n timers in tvec.
XNote that this TIMER need not have had a timeout yet.
X.SH DIAGNOSTICS
XTime_left returns constant NO_TIME if t was turned off.
X.br
XTvec_timeout returns n if si >= n, or none of the timers in tvec had a timeout.
X.br
XTvec_oldest returns n if all timers in tvec were turned off.
X.SH BUGS
XAccuracy is limited to whatever the granularity of ftime(2) offers
X(about 60 Hz).
X.br
XYou must call a function to notice a timeout.
XThere is no asynchronous (i.e. signaling) mechanism.
X.br
XThe functions don't notice changes in Daylight Saving or time zone.
X.SH "SEE ALSO"
Xtime(2).
/
echo 'x - timer.c'
sed 's/^X//' > timer.c << '/'
X/*
X * "timer.c", zie timer(3).
X */
X
X#include	<sys/types.h>
X#include	<sys/timeb.h>
X#include	"timer.h"
X
X/*
X * Geef huidige tijd in sekonden.
X * Deze fct. is implemetatie-afhankelijk van ftime(2).
X */
Xstatic double sec_time ()
X{
X	struct timeb ft;
X
X	ftime (&ft);
X	return (double) ft.time + (double) ft.millitm / (double) 1000;
X}
X
X/*
X * Geef TIMER met timeout na sec sekonden (mag < 0 zijn).
X * Cleart als sec == NO_TIME.
X */
XTIMER timer (sec)
Xdouble sec;
X{
X	if (sec == NO_TIME)	/* ge-clear-de timer */
X		return (TIMER) NO_TIME;
X	else
X		return (TIMER) (sec_time () + sec);
X}
X
X/*
X * Geef # sekonden tot timeout van t (kan negatief zijn).
X * Geeft NO_TIME voor ge-clear-de TIMER.
X */
Xdouble time_left (t)
XTIMER t;
X{
X	if (t == NO_TIME)
X		return NO_TIME;
X	else
X		return (double) t - sec_time ();
X}
X
X/*
X * Set tvec [0] .. tvec [n] op timeout na sec sekonden.
X * Als sec is NO_TIME, worden alle tvec [i] afgezet.
X */
Xset_tvec (tvec, n, sec)
XTIMER *tvec;
Xunsigned n;
Xdouble sec;
X{
X	if (sec != NO_TIME)
X		sec += sec_time ();
X
X	while (n-- != 0)
X		tvec [n] = (TIMER) sec;
X}
X
X/*
X * Geef index van eerste timer met een timeout in circular search
X * si .. n - 1, 0 .. si - 1.
X * Levert n als si >= n, of als geen timeout is opgetreden.
X */
Xunsigned tvec_timeout (tvec, n, si)
XTIMER *tvec;
Xunsigned n, si;
X{
X	double now;
X	unsigned i;
X
X	if (si >= n)
X		return n;
X
X	now = sec_time ();
X
X	for (i = si; i < n; i++)
X		if ((double) tvec [i] <= now)
X			return i;
X	for (i = 0; i < si; i++)
X		if ((double) tvec [i] <= now)
X			return i;
X
X	return n;
X}
X
X/*
X * Geef index in tvec van de (eerste) oudste timer.
X * Levert n als alle timers afstaan.
X */
Xunsigned tvec_oldest (tvec, n)
XTIMER *tvec;
Xunsigned n;
X{
X	unsigned i, oldest = 0;
X
X	for (i = 1; i < n; i++)
X		if (tvec [i] < tvec [oldest])
X			oldest = i;
X
X	if (n != 0 && tvec [oldest] != (TIMER) NO_TIME)
X		return oldest;
X	else
X		return n;
X}
/
echo 'x - timer.h'
sed 's/^X//' > timer.h << '/'
X/*
X * "timer.h", zie timer(3).
X */
X
X#ifndef		INCL_TIMER
X#define		INCL_TIMER
X
Xtypedef double	TIMER;		/* een timer */
X
X/*
X * Waarde van time_left (timer (NO_TIME)).
X * Dit is in de buurt van max double (implementatie afhankelijk).
X */
X#define		NO_TIME		((double) 1.7e+38)
X
Xextern TIMER timer (/*double*/);
Xextern double time_left (/*TIMER*/);
X
Xextern set_tvec (/*TIMER [], unsigned, double*/);
Xextern unsigned tvec_timeout (/*TIMER [], unsigned, unsigned*/);
Xextern unsigned tvec_oldest (/*TIMER [], unsigned*/);
X
X#endif
/
echo 'Part 01 of pack.out complete.'
exit