Implementing a time source

Classes derived from TTimeSource provide standard models of the time flow. There are two ways to implement a time source: implement a driven time source using TDrivenTimeSource, or derive a new class from TTimeSource. TDrivenTimeSource models the flow of time by continually setting the time that it represents.


Using a driven- time source

To implement a driven time source, you instantiate a TDrivenTimeSource class and set its time for every update--time moves or changes only when it is set to a new time.

For example, a TDrivenTimeSource might be driven by an animation application that uses a time base of its own to control its frame rate. For animation updates, tasks would delay on syncable clocks connected to the TDrivenTimeSource. You can control relative rates by changing the dependencies of the clocks instead of recalculating each delay or timing dependency. The animation application can then step through, trace, or run the animation at any rate by updating the TDrivenTimeSource. The whole animation could also be run in real time or in sync with audio, video, or MIDI by synchronizing to another time source. The MIDI clock is an example of a clock that gets its time from a driven time source.

Deriving a time source from TTimeSource

You can derive a new time source from TTimeSource if TDrivenTimeSource does not provide an appropriate representation of your time source. For example, you might derive a new time source to:

For example, a hardware clock might have some built-in timing services. Using a driven-time source to represent the hardware clock would be impractical because calling SetTime every time the time changed would take up most of the CPU processing time. Instead, you can derive a new time source from TTimeSource.

When you implement a new time source, you must override TTimeSource::GetNextTime and TTimeSource::Now:

    GetNextTime fills in nextTime with the next time that the TickleWakeUps method should be called because a wakeup needs to be fired. GetNextTime is called by the time source's thread, so when you override GetNextTime, the implementation needs to be fast, should not block, and cannot call TickleWakeUps. GetNextTime is called by the system with an internal semaphore.
You might also provide a surrogate for the time source to improve performance for TSyncableClock objects in the sources clock hierarchy. To do this, you derive from TTimeSourceSurrogate and override its Now function so that it is equivalent to the TTimeSource::Now function.

Similarly, you can implement a ramp clock. A ramp clock is a time source that is set too infrequently to provide good resolution. A ramp clock guesses when the time source is going to set its time and what that time will be. The ramp clock can be used to slowly ramp up the time until it reaches the new time or the time that is set on the time source.

TTimeSource::Now and TTimeSourceSurrogate::Now must be compatible; otherwise, a TSyncableClock could be out of sync with the time source for that clock hierarchy.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker