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