andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** File: pralarm.h andre@0: ** Description: API to periodic alarms. andre@0: ** andre@0: ** andre@0: ** Alarms are defined to invoke some client specified function at andre@0: ** a time in the future. The notification may be a one time event andre@0: ** or repeated at a fixed interval. The interval at which the next andre@0: ** notification takes place may be modified by the client code only andre@0: ** during the respective notification. andre@0: ** andre@0: ** The notification is delivered on a thread that is part of the andre@0: ** alarm context (PRAlarm). The thread will inherit the priority andre@0: ** of the Alarm creator. andre@0: ** andre@0: ** Any number of periodic alarms (PRAlarmID) may be created within andre@0: ** the context of a single alarm (PRAlarm). The notifications will be andre@0: ** scheduled as close to the desired time as possible. andre@0: ** andre@0: ** Repeating periodic notifies are expected to run at a fixed rate. andre@0: ** That rate is expressed as some number of notifies per period where andre@0: ** the period is much larger than a PRIntervalTime (see prinrval.h). andre@0: */ andre@0: andre@0: #if !defined(pralarm_h) andre@0: #define pralarm_h andre@0: andre@0: #include "prtypes.h" andre@0: #include "prinrval.h" andre@0: andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /**********************************************************************/ andre@0: /************************* TYPES AND CONSTANTS ************************/ andre@0: /**********************************************************************/ andre@0: andre@0: typedef struct PRAlarm PRAlarm; andre@0: typedef struct PRAlarmID PRAlarmID; andre@0: andre@0: typedef PRBool (PR_CALLBACK *PRPeriodicAlarmFn)( andre@0: PRAlarmID *id, void *clientData, PRUint32 late); andre@0: andre@0: /**********************************************************************/ andre@0: /****************************** FUNCTIONS *****************************/ andre@0: /**********************************************************************/ andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_CreateAlarm andre@0: ** DESCRIPTION: andre@0: ** Create an alarm context. andre@0: ** INPUTS: void andre@0: ** OUTPUTS: None andre@0: ** RETURN: PRAlarm* andre@0: ** andre@0: ** SIDE EFFECTS: andre@0: ** This creates an alarm context, which is an object used for subsequent andre@0: ** notification creations. It also creates a thread that will be used to andre@0: ** deliver the notifications that are expected to be defined. The client andre@0: ** is resposible for destroying the context when appropriate. andre@0: ** RESTRICTIONS: andre@0: ** None. andre@0: ** MEMORY: The object (PRAlarm) and a thread to support notifications. andre@0: ** ALGORITHM: N/A andre@0: ***********************************************************************/ andre@0: NSPR_API(PRAlarm*) PR_CreateAlarm(void); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_DestroyAlarm andre@0: ** DESCRIPTION: andre@0: ** Destroys the context created by PR_CreateAlarm(). andre@0: ** INPUTS: PRAlarm* andre@0: ** OUTPUTS: None andre@0: ** RETURN: PRStatus andre@0: ** andre@0: ** SIDE EFFECTS: andre@0: ** This destroys the context that was created by PR_CreateAlarm(). andre@0: ** If there are any active alarms (PRAlarmID), they will be cancelled. andre@0: ** Once that is done, the thread that was used to deliver the alarms andre@0: ** will be joined. andre@0: ** RESTRICTIONS: andre@0: ** None. andre@0: ** MEMORY: N/A andre@0: ** ALGORITHM: N/A andre@0: ***********************************************************************/ andre@0: NSPR_API(PRStatus) PR_DestroyAlarm(PRAlarm *alarm); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_SetAlarm andre@0: ** DESCRIPTION: andre@0: ** Creates a periodic notifier that is to be delivered to a specified andre@0: ** function at some fixed interval. andre@0: ** INPUTS: PRAlarm *alarm Parent alarm context andre@0: ** PRIntervalTime period Interval over which the notifies andre@0: ** are delivered. andre@0: ** PRUint32 rate The rate within the interval that andre@0: ** the notifies will be delivered. andre@0: ** PRPeriodicAlarmFn function Entry point where the notifies andre@0: ** will be delivered. andre@0: ** OUTPUTS: None andre@0: ** RETURN: PRAlarmID* Handle to the notifier just created andre@0: ** or NULL if the request failed. andre@0: ** andre@0: ** SIDE EFFECTS: andre@0: ** A periodic notifier is created. The notifications will be delivered andre@0: ** by the alarm's internal thread at a fixed interval whose rate is the andre@0: ** number of interrupts per interval specified. The first notification andre@0: ** will be delivered as soon as possible, and they will continue until andre@0: ** the notifier routine indicates that they should cease of the alarm andre@0: ** context is destroyed (PR_DestroyAlarm). andre@0: ** RESTRICTIONS: andre@0: ** None. andre@0: ** MEMORY: Memory for the notifier object. andre@0: ** ALGORITHM: The rate at which notifications are delivered are stated andre@0: ** to be "'rate' notifies per 'interval'". The exact time of andre@0: ** the notification is computed based on a epoch established andre@0: ** when the notifier was set. Each notification is delivered andre@0: ** not ealier than the epoch plus the fixed rate times the andre@0: ** notification sequence number. Such notifications have the andre@0: ** potential to be late by not more than 'interval'/'rate'. andre@0: ** The amount of lateness of one notification is taken into andre@0: ** account on the next in an attempt to avoid long term slew. andre@0: ***********************************************************************/ andre@0: NSPR_API(PRAlarmID*) PR_SetAlarm( andre@0: PRAlarm *alarm, PRIntervalTime period, PRUint32 rate, andre@0: PRPeriodicAlarmFn function, void *clientData); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: PR_ResetAlarm andre@0: ** DESCRIPTION: andre@0: ** Resets an existing alarm. andre@0: ** INPUTS: PRAlarmID *id Identify of the notifier. andre@0: ** PRIntervalTime period Interval over which the notifies andre@0: ** are delivered. andre@0: ** PRUint32 rate The rate within the interval that andre@0: ** the notifies will be delivered. andre@0: ** OUTPUTS: None andre@0: ** RETURN: PRStatus Indication of completion. andre@0: ** andre@0: ** SIDE EFFECTS: andre@0: ** An existing alarm may have its period and rate redefined. The andre@0: ** additional side effect is that the notifier's epoch is recomputed. andre@0: ** The first notification delivered by the newly refreshed alarm is andre@0: ** defined to be 'interval'/'rate' from the time of the reset. andre@0: ** RESTRICTIONS: andre@0: ** This function may only be called in the notifier for that alarm. andre@0: ** MEMORY: N/A. andre@0: ** ALGORITHM: See PR_SetAlarm(). andre@0: ***********************************************************************/ andre@0: NSPR_API(PRStatus) PR_ResetAlarm( andre@0: PRAlarmID *id, PRIntervalTime period, PRUint32 rate); andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* !defined(pralarm_h) */ andre@0: andre@0: /* prinrval.h */