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