Tycho's Timer Package


TyTimer is a simple extension to Tycho that adds a real-time timer. The timer is designed for use with Tycho's Scheduler class, and should not be used other than in the style documented in the Scheduler documentation. The package has both a C interface for use by other C packages, and a Tcl interface for use by "tasks" in Tcl.

To run a demonstration of the timer as used by the scheduler:

    source $tycho/src/tytest/testtask.tcl
The top number is the counter incremented by the C task; the bottom one is the counter incremented in Tcl. To more clearly see the interleaving of these two tasks:
    ::tycho::timer period 200

Source files:

::tycho::timer

::tycho::timer is a Tcl procedure that manipulates and queries the real-time timer. To ensure that it is in the ::tycho namespace, always load the tyEvent package with ::tycho::loadIfNotPresent. It accepts the following argument formats:

::tycho::timer elapsed
Return 1 if the timer is not running. Note that, if the timer has not been started, 1 will be returned anyway. A Tcl script can use this to find out when the timer has stopped. Note that there is no way for a Tcl script to be notified asynchronously: it must use this procedure to query the timer. The overhead is very low -- lower than reading the value of a Tcl variable, in fact!

::tycho::timer period ?period?
Get or set the timer period. If period is given, the timer period is set to period milli-seconds; if not, the current period is returned.

::tycho::timer start ?period?
Start the timer. If period is given, the timer period is set to period milli-seconds; otherwise, the previously set value if used. When the package is first loaded, the period is set to 20 ms.

::tycho::timer stop
Stop the timer. This should be called by a Tcl task that finishes it processing before its time is up.

C Functions

The C functions are provided to allow a C module to cooperate with the Tycho scheduler. A module that implements a C task must include a callback that performs its processing. For best efficiency, this callback must start the timer, and, each time through its loop, check whether the timer has elapsed. When it has, it must return. See the Scheduler documentation.

void Ty_TimerPeriod ( int period )
Set the timer period, where period is in milli-seconds. When the package is loaded, the period is set to 20 ms. C tasks should not normally set the period.

void Ty_TimerStart ( )
Start the timer running. The timer will elapse with the period previously set.

void Ty_TimerStop ( )
Stop the timer running. This should be called by a task that terminates before its given time is up.

int Ty_TimerElapsed ( )
Return 1 if the timer has elapsed, or 0 if it has not. Tasks should call this once each time through their "loop" to test whether their time is up.

void Ty_DoAllEvents ( )
If the timer has elapsed, process all pending Tk events. This is provided for C programs that are structured as a single loop that runs forever or until completion, without regard to the Tycho scheduler. Such a program should periodically call Ty_DoAllEvents to ensure that Tk events are processed. This style of C module is strongly discouraged, since it will not allow the Tycho scheduler to operate.


Copyright © 1996-1997, The Regents of the University of California. All rights reserved.
Last updated: 05/11/98, comments to: tycho@eecs.berkeley.edu