CHeartbeat Class Reference

class CHeartbeat : public CTimer

Heatbeat timer.

This class generates regular heartbeat events on a fixed fraction of a second. It is more accurate than a CPeriodic timer, because it provides a function to restore timer accuracy if it gets out of synchronisation with the system clock.

The protected RunL() function is called when the timer completes. The RunL() function in turn calls either the MBeating::Beat() or the MBeating::Synchronize() functions; MBeating is specified as a parameter to the Start() function used to start the heartbeat timer.

The relevant MBeating function may not be called immediately after the signal from the timer request has been generated, for the following reasons:

1. the RunL() of another active object may be running at the time of the signal

2. other active objects may have a higher priority than the CHeartbeat

If no heartbeat is missed, then the Beat() function is called.

If one or more heartbeats are missed then the Synchronize() function is called. It is important to bear in mind that the machine might be switched off after a few beats of the heart, and then Synchronize() will be called several days later. It is therefore essential that synchronisation is achieved as quickly as possible, rather than trying to catch up a tick at a time. In the context of an analogue clock, for instance, the clock should just redraw itself with the current time - rather than moving the hands round in steps until the time is correct.

CHeartbeat is an active object, derived from CActive (via CTimer). You should be familiar with CActive in order to understand CHeartbeat behaviour, but not necessarily with CTimer.

MBeating

Inherits from

Constructor & Destructor Documentation

CHeartbeat(TInt)

IMPORT_CCHeartbeat(TIntaPriority)[protected]

Protected constructor with a priority. Use this constructor to set the priority of the active object.

Classes derived from CHeartbeat must define and provide a constructor through which the priority of the active object can be passed. Such a constructor can call CHeartbeat's constructor in its constructor initialisation list.

Parameters

TInt aPriorityThe priority of the timer.

~CHeartbeat()

IMPORT_C~CHeartbeat()

Destructor.

Frees resources prior to destruction.

Member Functions Documentation

New(TInt)

IMPORT_C CHeartbeat *New(TIntaPriority)[static]

Allocates and constructs a CHeartbeat object - non-leaving.

Specify a high priority so the callback function is scheduled as soon as possible after the timer events complete.

Parameters

TInt aPriorityThe priority of the active object. If timing is critical, it should be higher than that of all other active objects owned by the scheduler.

NewL(TInt)

IMPORT_C CHeartbeat *NewL(TIntaPriority)[static]

Allocates and constructs a CHeartbeat object - leaving.

Specify a high priority so the callback function is scheduled as soon as possible after the timer events complete.

Parameters

TInt aPriorityThe priority of the active object. If timing is critical, it should be higher than that of all other active objects owned by the scheduler.

RunL()

IMPORT_C voidRunL()[protected, virtual]

Handles an active object's request completion event.

A derived class must provide an implementation to handle the completed request. If appropriate, it may issue another request.

The function is called by the active scheduler when a request completion event occurs, i.e. after the active scheduler's WaitForAnyRequest() function completes.

Before calling this active object's RunL() function, the active scheduler has:

1. decided that this is the highest priority active object with a completed request

2. marked this active object's request as complete (i.e. the request is no longer outstanding)

RunL() runs under a trap harness in the active scheduler. If it leaves, then the active scheduler calls RunError() to handle the leave.

Note that once the active scheduler's Start() function has been called, all user code is run under one of the program's active object's RunL() or RunError() functions.

CActiveScheduler::Start CActiveScheduler::Error CActiveScheduler::WaitForAnyRequest TRAPD

Start(TTimerLockSpec, MBeating *)

IMPORT_C voidStart(TTimerLockSpecaLock,
MBeating *aBeating
)

Starts generating heartbeat events. The event results in calls to the Beat() and Synchronize() functions specified by aBeating.

The first event is generated on the first fraction of a second corresponding to aLock that occurs after Start() has returned; subsequent events are generated regularly thereafter at one second intervals on the second fraction specified by aLock.

The aBeating mixin must be written by the user. Most of the time, its Beat() function is called which trivially updates the tick count. Occasionally, synchronisation is lost, and the Synchronize() function is called instead: this must find out from the system time how many ticks should have been counted, and update things accordingly.

Once started, heartbeat events are generated until the CHeartbeat object is destroyed.

Parameters

TTimerLockSpec aLockThe fraction of a second at which the timer completes.
MBeating * aBeatingProvides the Beat() and Synchronize() functions.

Member Data Documentation

MBeating * iBeating

MBeating *iBeating[private]

TTimerLockSpec iLock

TTimerLockSpec iLock[private]