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: #include "primpl.h" andre@0: andre@0: /**********************************************************************/ andre@0: /******************************* PRALARM ******************************/ andre@0: /**********************************************************************/ andre@0: andre@0: #include "obsolete/pralarm.h" andre@0: andre@0: struct PRAlarmID { /* typedef'd in pralarm.h */ andre@0: PRCList list; /* circular list linkage */ andre@0: PRAlarm *alarm; /* back pointer to owning alarm */ andre@0: PRPeriodicAlarmFn function; /* function to call for notify */ andre@0: void *clientData; /* opaque client context */ andre@0: PRIntervalTime period; /* the client defined period */ andre@0: PRUint32 rate; /* rate of notification */ andre@0: andre@0: PRUint32 accumulator; /* keeps track of # notifies */ andre@0: PRIntervalTime epoch; /* when timer was started */ andre@0: PRIntervalTime nextNotify; /* when we'll next do our thing */ andre@0: PRIntervalTime lastNotify; /* when we last did our thing */ andre@0: }; andre@0: andre@0: typedef enum {alarm_active, alarm_inactive} _AlarmState; andre@0: andre@0: struct PRAlarm { /* typedef'd in pralarm.h */ andre@0: PRCList timers; /* base of alarm ids list */ andre@0: PRLock *lock; /* lock used to protect data */ andre@0: PRCondVar *cond; /* condition that used to wait */ andre@0: PRThread *notifier; /* thread to deliver notifies */ andre@0: PRAlarmID *current; /* current alarm being served */ andre@0: _AlarmState state; /* used to delete the alarm */ andre@0: }; andre@0: andre@0: static PRAlarmID *pr_getNextAlarm(PRAlarm *alarm, PRAlarmID *id) andre@0: { andre@0: /* andre@0: * Puts 'id' back into the sorted list iff it's not NULL. andre@0: * Removes the first element from the list and returns it (or NULL). andre@0: * List is "assumed" to be short. andre@0: * andre@0: * NB: Caller is providing locking andre@0: */ andre@0: PRCList *timer; andre@0: PRAlarmID *result = id; andre@0: PRIntervalTime now = PR_IntervalNow(); andre@0: andre@0: if (!PR_CLIST_IS_EMPTY(&alarm->timers)) andre@0: { andre@0: if (id != NULL) /* have to put this id back in */ andre@0: { andre@0: PRIntervalTime idDelta = now - id->nextNotify; andre@0: timer = alarm->timers.next; andre@0: do andre@0: { andre@0: result = (PRAlarmID*)timer; andre@0: if ((PRIntervalTime)(now - result->nextNotify) > idDelta) andre@0: { andre@0: PR_INSERT_BEFORE(&id->list, &alarm->timers); andre@0: break; andre@0: } andre@0: timer = timer->next; andre@0: } while (timer != &alarm->timers); andre@0: } andre@0: result = (PRAlarmID*)(timer = PR_LIST_HEAD(&alarm->timers)); andre@0: PR_REMOVE_LINK(timer); /* remove it from the list */ andre@0: } andre@0: andre@0: return result; andre@0: } /* pr_getNextAlarm */ andre@0: andre@0: static PRIntervalTime pr_PredictNextNotifyTime(PRAlarmID *id) andre@0: { andre@0: PRIntervalTime delta; andre@0: PRFloat64 baseRate = (PRFloat64)id->period / (PRFloat64)id->rate; andre@0: PRFloat64 offsetFromEpoch = (PRFloat64)id->accumulator * baseRate; andre@0: andre@0: id->accumulator += 1; /* every call advances to next period */ andre@0: id->lastNotify = id->nextNotify; /* just keeping track of things */ andre@0: id->nextNotify = (PRIntervalTime)(offsetFromEpoch + 0.5); andre@0: andre@0: delta = id->nextNotify - id->lastNotify; andre@0: return delta; andre@0: } /* pr_PredictNextNotifyTime */ andre@0: andre@0: static void PR_CALLBACK pr_alarmNotifier(void *arg) andre@0: { andre@0: /* andre@0: * This is the root of the notifier thread. There is one such thread andre@0: * for each PRAlarm. It may service an arbitrary (though assumed to be andre@0: * small) number of alarms using the same thread and structure. It andre@0: * continues to run until the alarm is destroyed. andre@0: */ andre@0: PRAlarmID *id = NULL; andre@0: PRAlarm *alarm = (PRAlarm*)arg; andre@0: enum {notify, abort, scan} why = scan; andre@0: andre@0: while (why != abort) andre@0: { andre@0: PRIntervalTime pause; andre@0: andre@0: PR_Lock(alarm->lock); andre@0: while (why == scan) andre@0: { andre@0: alarm->current = NULL; /* reset current id */ andre@0: if (alarm->state == alarm_inactive) why = abort; /* we're toast */ andre@0: else if (why == scan) /* the dominant case */ andre@0: { andre@0: id = pr_getNextAlarm(alarm, id); /* even if it's the same */ andre@0: if (id == NULL) /* there are no alarms set */ andre@0: (void)PR_WaitCondVar(alarm->cond, PR_INTERVAL_NO_TIMEOUT); andre@0: else andre@0: { andre@0: pause = id->nextNotify - (PR_IntervalNow() - id->epoch); andre@0: if ((PRInt32)pause <= 0) /* is this one's time up? */ andre@0: { andre@0: why = notify; /* set up to do our thing */ andre@0: alarm->current = id; /* id we're about to schedule */ andre@0: } andre@0: else andre@0: (void)PR_WaitCondVar(alarm->cond, pause); /* dally */ andre@0: } andre@0: } andre@0: } andre@0: PR_Unlock(alarm->lock); andre@0: andre@0: if (why == notify) andre@0: { andre@0: (void)pr_PredictNextNotifyTime(id); andre@0: if (!id->function(id, id->clientData, ~pause)) andre@0: { andre@0: /* andre@0: * Notified function decided not to continue. Free andre@0: * the alarm id to make sure it doesn't get back on andre@0: * the list. andre@0: */ andre@0: PR_DELETE(id); /* free notifier object */ andre@0: id = NULL; /* so it doesn't get back into the list */ andre@0: } andre@0: why = scan; /* so we can cycle through the loop again */ andre@0: } andre@0: } andre@0: andre@0: } /* pr_alarm_notifier */ andre@0: andre@0: PR_IMPLEMENT(PRAlarm*) PR_CreateAlarm(void) andre@0: { andre@0: PRAlarm *alarm = PR_NEWZAP(PRAlarm); andre@0: if (alarm != NULL) andre@0: { andre@0: if ((alarm->lock = PR_NewLock()) == NULL) goto done; andre@0: if ((alarm->cond = PR_NewCondVar(alarm->lock)) == NULL) goto done; andre@0: alarm->state = alarm_active; andre@0: PR_INIT_CLIST(&alarm->timers); andre@0: alarm->notifier = PR_CreateThread( andre@0: PR_USER_THREAD, pr_alarmNotifier, alarm, andre@0: PR_GetThreadPriority(PR_GetCurrentThread()), andre@0: PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); andre@0: if (alarm->notifier == NULL) goto done; andre@0: } andre@0: return alarm; andre@0: andre@0: done: andre@0: if (alarm->cond != NULL) PR_DestroyCondVar(alarm->cond); andre@0: if (alarm->lock != NULL) PR_DestroyLock(alarm->lock); andre@0: PR_DELETE(alarm); andre@0: return NULL; andre@0: } /* CreateAlarm */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_DestroyAlarm(PRAlarm *alarm) andre@0: { andre@0: PRStatus rv; andre@0: andre@0: PR_Lock(alarm->lock); andre@0: alarm->state = alarm_inactive; andre@0: rv = PR_NotifyCondVar(alarm->cond); andre@0: PR_Unlock(alarm->lock); andre@0: andre@0: if (rv == PR_SUCCESS) andre@0: rv = PR_JoinThread(alarm->notifier); andre@0: if (rv == PR_SUCCESS) andre@0: { andre@0: PR_DestroyCondVar(alarm->cond); andre@0: PR_DestroyLock(alarm->lock); andre@0: PR_DELETE(alarm); andre@0: } andre@0: return rv; andre@0: } /* PR_DestroyAlarm */ andre@0: andre@0: PR_IMPLEMENT(PRAlarmID*) PR_SetAlarm( andre@0: PRAlarm *alarm, PRIntervalTime period, PRUint32 rate, andre@0: PRPeriodicAlarmFn function, void *clientData) andre@0: { andre@0: /* andre@0: * Create a new periodic alarm an existing current structure. andre@0: * Set up the context and compute the first notify time (immediate). andre@0: * Link the new ID into the head of the list (since it's notifying andre@0: * immediately). andre@0: */ andre@0: andre@0: PRAlarmID *id = PR_NEWZAP(PRAlarmID); andre@0: andre@0: if (!id) andre@0: return NULL; andre@0: andre@0: id->alarm = alarm; andre@0: PR_INIT_CLIST(&id->list); andre@0: id->function = function; andre@0: id->clientData = clientData; andre@0: id->period = period; andre@0: id->rate = rate; andre@0: id->epoch = id->nextNotify = PR_IntervalNow(); andre@0: (void)pr_PredictNextNotifyTime(id); andre@0: andre@0: PR_Lock(alarm->lock); andre@0: PR_INSERT_BEFORE(&id->list, &alarm->timers); andre@0: PR_NotifyCondVar(alarm->cond); andre@0: PR_Unlock(alarm->lock); andre@0: andre@0: return id; andre@0: } /* PR_SetAlarm */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_ResetAlarm( andre@0: PRAlarmID *id, PRIntervalTime period, PRUint32 rate) andre@0: { andre@0: /* andre@0: * Can only be called from within the notify routine. Doesn't andre@0: * need locking because it can only be called from within the andre@0: * notify routine. andre@0: */ andre@0: if (id != id->alarm->current) andre@0: return PR_FAILURE; andre@0: id->period = period; andre@0: id->rate = rate; andre@0: id->accumulator = 1; andre@0: id->epoch = PR_IntervalNow(); andre@0: (void)pr_PredictNextNotifyTime(id); andre@0: return PR_SUCCESS; andre@0: } /* PR_ResetAlarm */ andre@0: andre@0: andre@0: