nsprpub/pr/src/misc/pralarm.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/misc/pralarm.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,246 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "primpl.h"
    1.10 +
    1.11 +/**********************************************************************/
    1.12 +/******************************* PRALARM ******************************/
    1.13 +/**********************************************************************/
    1.14 +
    1.15 +#include "obsolete/pralarm.h"
    1.16 +
    1.17 +struct PRAlarmID {                       /* typedef'd in pralarm.h       */
    1.18 +    PRCList list;                        /* circular list linkage        */
    1.19 +    PRAlarm *alarm;                      /* back pointer to owning alarm */
    1.20 +    PRPeriodicAlarmFn function;          /* function to call for notify  */
    1.21 +    void *clientData;                    /* opaque client context        */
    1.22 +    PRIntervalTime period;               /* the client defined period    */
    1.23 +    PRUint32 rate;                       /* rate of notification         */
    1.24 +
    1.25 +    PRUint32 accumulator;                /* keeps track of # notifies    */
    1.26 +    PRIntervalTime epoch;                /* when timer was started       */
    1.27 +    PRIntervalTime nextNotify;           /* when we'll next do our thing */
    1.28 +    PRIntervalTime lastNotify;           /* when we last did our thing   */
    1.29 +};
    1.30 +
    1.31 +typedef enum {alarm_active, alarm_inactive} _AlarmState;
    1.32 +
    1.33 +struct PRAlarm {                         /* typedef'd in pralarm.h       */
    1.34 +    PRCList timers;                      /* base of alarm ids list       */
    1.35 +    PRLock *lock;                        /* lock used to protect data    */
    1.36 +    PRCondVar *cond;                     /* condition that used to wait  */
    1.37 +    PRThread *notifier;                  /* thread to deliver notifies   */
    1.38 +    PRAlarmID *current;                  /* current alarm being served   */
    1.39 +    _AlarmState state;                   /* used to delete the alarm     */
    1.40 +};
    1.41 +
    1.42 +static PRAlarmID *pr_getNextAlarm(PRAlarm *alarm, PRAlarmID *id)
    1.43 +{
    1.44 +/*
    1.45 + * Puts 'id' back into the sorted list iff it's not NULL.
    1.46 + * Removes the first element from the list and returns it (or NULL).
    1.47 + * List is "assumed" to be short.
    1.48 + *
    1.49 + * NB: Caller is providing locking
    1.50 + */
    1.51 +    PRCList *timer;
    1.52 +    PRAlarmID *result = id;
    1.53 +    PRIntervalTime now = PR_IntervalNow();
    1.54 +
    1.55 +    if (!PR_CLIST_IS_EMPTY(&alarm->timers))
    1.56 +    {    
    1.57 +        if (id != NULL)  /* have to put this id back in */
    1.58 +        {        
    1.59 +            PRIntervalTime idDelta = now - id->nextNotify;
    1.60 +            timer = alarm->timers.next;
    1.61 +            do
    1.62 +            {
    1.63 +                result = (PRAlarmID*)timer;
    1.64 +                if ((PRIntervalTime)(now - result->nextNotify) > idDelta)
    1.65 +                {
    1.66 +                    PR_INSERT_BEFORE(&id->list, &alarm->timers);
    1.67 +                    break;
    1.68 +                }
    1.69 +                timer = timer->next;
    1.70 +            } while (timer != &alarm->timers);
    1.71 +        }
    1.72 +        result = (PRAlarmID*)(timer = PR_LIST_HEAD(&alarm->timers));
    1.73 +        PR_REMOVE_LINK(timer);  /* remove it from the list */
    1.74 +    }
    1.75 +
    1.76 +    return result;
    1.77 +}  /* pr_getNextAlarm */
    1.78 +
    1.79 +static PRIntervalTime pr_PredictNextNotifyTime(PRAlarmID *id)
    1.80 +{
    1.81 +    PRIntervalTime delta;
    1.82 +    PRFloat64 baseRate = (PRFloat64)id->period / (PRFloat64)id->rate;
    1.83 +    PRFloat64 offsetFromEpoch = (PRFloat64)id->accumulator * baseRate;
    1.84 +
    1.85 +    id->accumulator += 1;  /* every call advances to next period */
    1.86 +    id->lastNotify = id->nextNotify;  /* just keeping track of things */
    1.87 +    id->nextNotify = (PRIntervalTime)(offsetFromEpoch + 0.5);
    1.88 +
    1.89 +    delta = id->nextNotify - id->lastNotify;
    1.90 +    return delta;
    1.91 +}  /* pr_PredictNextNotifyTime */
    1.92 +
    1.93 +static void PR_CALLBACK pr_alarmNotifier(void *arg)
    1.94 +{
    1.95 +    /*
    1.96 +     * This is the root of the notifier thread. There is one such thread
    1.97 +     * for each PRAlarm. It may service an arbitrary (though assumed to be
    1.98 +     * small) number of alarms using the same thread and structure. It
    1.99 +     * continues to run until the alarm is destroyed.
   1.100 +     */
   1.101 +    PRAlarmID *id = NULL;
   1.102 +    PRAlarm *alarm = (PRAlarm*)arg;
   1.103 +    enum {notify, abort, scan} why = scan;
   1.104 +
   1.105 +    while (why != abort)
   1.106 +    {
   1.107 +        PRIntervalTime pause;
   1.108 +
   1.109 +        PR_Lock(alarm->lock);
   1.110 +        while (why == scan)
   1.111 +        {
   1.112 +            alarm->current = NULL;  /* reset current id */
   1.113 +            if (alarm->state == alarm_inactive) why = abort;  /* we're toast */
   1.114 +            else if (why == scan)  /* the dominant case */
   1.115 +            {
   1.116 +                id = pr_getNextAlarm(alarm, id);  /* even if it's the same */
   1.117 +                if (id == NULL)  /* there are no alarms set */
   1.118 +                    (void)PR_WaitCondVar(alarm->cond, PR_INTERVAL_NO_TIMEOUT);
   1.119 +                else
   1.120 +                {
   1.121 +                    pause = id->nextNotify - (PR_IntervalNow() - id->epoch);
   1.122 +                    if ((PRInt32)pause <= 0)  /* is this one's time up? */
   1.123 +                    {
   1.124 +                        why = notify;  /* set up to do our thing */
   1.125 +                        alarm->current = id;  /* id we're about to schedule */
   1.126 +                    }
   1.127 +                    else
   1.128 +                        (void)PR_WaitCondVar(alarm->cond, pause);  /* dally */
   1.129 +                }
   1.130 +            }
   1.131 +        }
   1.132 +        PR_Unlock(alarm->lock);
   1.133 +
   1.134 +        if (why == notify)
   1.135 +        {
   1.136 +            (void)pr_PredictNextNotifyTime(id);
   1.137 +            if (!id->function(id, id->clientData, ~pause))
   1.138 +            {
   1.139 +                /*
   1.140 +                 * Notified function decided not to continue. Free
   1.141 +                 * the alarm id to make sure it doesn't get back on
   1.142 +                 * the list.
   1.143 +                 */
   1.144 +                PR_DELETE(id);  /* free notifier object */
   1.145 +                id = NULL;  /* so it doesn't get back into the list */
   1.146 +            }
   1.147 +            why = scan;  /* so we can cycle through the loop again */
   1.148 +        }
   1.149 +    }
   1.150 +
   1.151 +}  /* pr_alarm_notifier */
   1.152 +
   1.153 +PR_IMPLEMENT(PRAlarm*) PR_CreateAlarm(void)
   1.154 +{
   1.155 +    PRAlarm *alarm = PR_NEWZAP(PRAlarm);
   1.156 +    if (alarm != NULL)
   1.157 +    {
   1.158 +        if ((alarm->lock = PR_NewLock()) == NULL) goto done;
   1.159 +        if ((alarm->cond = PR_NewCondVar(alarm->lock)) == NULL) goto done;
   1.160 +        alarm->state = alarm_active;
   1.161 +        PR_INIT_CLIST(&alarm->timers);
   1.162 +        alarm->notifier = PR_CreateThread(
   1.163 +            PR_USER_THREAD, pr_alarmNotifier, alarm,
   1.164 +            PR_GetThreadPriority(PR_GetCurrentThread()),
   1.165 +            PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
   1.166 +        if (alarm->notifier == NULL) goto done;
   1.167 +    }
   1.168 +    return alarm;
   1.169 +
   1.170 +done:
   1.171 +    if (alarm->cond != NULL) PR_DestroyCondVar(alarm->cond);
   1.172 +    if (alarm->lock != NULL) PR_DestroyLock(alarm->lock);
   1.173 +    PR_DELETE(alarm);
   1.174 +    return NULL;
   1.175 +}  /* CreateAlarm */
   1.176 +
   1.177 +PR_IMPLEMENT(PRStatus) PR_DestroyAlarm(PRAlarm *alarm)
   1.178 +{
   1.179 +    PRStatus rv;
   1.180 +
   1.181 +    PR_Lock(alarm->lock);
   1.182 +    alarm->state = alarm_inactive;
   1.183 +    rv = PR_NotifyCondVar(alarm->cond);
   1.184 +    PR_Unlock(alarm->lock);
   1.185 +
   1.186 +    if (rv == PR_SUCCESS)
   1.187 +        rv = PR_JoinThread(alarm->notifier);
   1.188 +    if (rv == PR_SUCCESS)
   1.189 +    {
   1.190 +        PR_DestroyCondVar(alarm->cond);
   1.191 +        PR_DestroyLock(alarm->lock);
   1.192 +        PR_DELETE(alarm);
   1.193 +    }
   1.194 +    return rv;
   1.195 +}  /* PR_DestroyAlarm */
   1.196 +
   1.197 +PR_IMPLEMENT(PRAlarmID*) PR_SetAlarm(
   1.198 +    PRAlarm *alarm, PRIntervalTime period, PRUint32 rate,
   1.199 +    PRPeriodicAlarmFn function, void *clientData)
   1.200 +{
   1.201 +    /*
   1.202 +     * Create a new periodic alarm an existing current structure.
   1.203 +     * Set up the context and compute the first notify time (immediate).
   1.204 +     * Link the new ID into the head of the list (since it's notifying
   1.205 +     * immediately).
   1.206 +     */
   1.207 +
   1.208 +    PRAlarmID *id = PR_NEWZAP(PRAlarmID);
   1.209 +
   1.210 +    if (!id)
   1.211 +        return NULL;
   1.212 +
   1.213 +    id->alarm = alarm;
   1.214 +    PR_INIT_CLIST(&id->list);
   1.215 +    id->function = function;
   1.216 +    id->clientData = clientData;
   1.217 +    id->period = period;
   1.218 +    id->rate = rate;
   1.219 +    id->epoch = id->nextNotify = PR_IntervalNow();
   1.220 +    (void)pr_PredictNextNotifyTime(id);
   1.221 +
   1.222 +    PR_Lock(alarm->lock);
   1.223 +    PR_INSERT_BEFORE(&id->list, &alarm->timers);
   1.224 +    PR_NotifyCondVar(alarm->cond);
   1.225 +    PR_Unlock(alarm->lock);
   1.226 +
   1.227 +    return id;
   1.228 +}  /* PR_SetAlarm */
   1.229 +
   1.230 +PR_IMPLEMENT(PRStatus) PR_ResetAlarm(
   1.231 +    PRAlarmID *id, PRIntervalTime period, PRUint32 rate)
   1.232 +{
   1.233 +    /*
   1.234 +     * Can only be called from within the notify routine. Doesn't
   1.235 +     * need locking because it can only be called from within the
   1.236 +     * notify routine.
   1.237 +     */
   1.238 +    if (id != id->alarm->current)
   1.239 +        return PR_FAILURE;
   1.240 +    id->period = period;
   1.241 +    id->rate = rate;
   1.242 +    id->accumulator = 1;
   1.243 +    id->epoch = PR_IntervalNow();
   1.244 +    (void)pr_PredictNextNotifyTime(id);
   1.245 +    return PR_SUCCESS;
   1.246 +}  /* PR_ResetAlarm */
   1.247 +
   1.248 +
   1.249 +

mercurial