LCOV - app.info - nsprpub/pr/src/misc/pralarm.c

LCOV - code coverage report
| Current view: | directory - nsprpub/pr/src/misc - pralarm.c (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 103 | 0 | 0.0 %
| Date: | 2012-07-07 | Functions: | 7 | 0 | 0.0 %

           1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
           2                 : /* ***** BEGIN LICENSE BLOCK *****
           3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           4                 :  *
           5                 :  * The contents of this file are subject to the Mozilla Public License Version
           6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           7                 :  * the License. You may obtain a copy of the License at
           8                 :  * http://www.mozilla.org/MPL/
           9                 :  *
          10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          12                 :  * for the specific language governing rights and limitations under the
          13                 :  * License.
          14                 :  *
          15                 :  * The Original Code is the Netscape Portable Runtime (NSPR).
          16                 :  *
          17                 :  * The Initial Developer of the Original Code is
          18                 :  * Netscape Communications Corporation.
          19                 :  * Portions created by the Initial Developer are Copyright (C) 1998-2000
          20                 :  * the Initial Developer. All Rights Reserved.
          21                 :  *
          22                 :  * Contributor(s):
          23                 :  *
          24                 :  * Alternatively, the contents of this file may be used under the terms of
          25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
          26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          28                 :  * of those above. If you wish to allow use of your version of this file only
          29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          30                 :  * use your version of this file under the terms of the MPL, indicate your
          31                 :  * decision by deleting the provisions above and replace them with the notice
          32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          33                 :  * the provisions above, a recipient may use your version of this file under
          34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          35                 :  *
          36                 :  * ***** END LICENSE BLOCK ***** */
          37                 :
          38                 : #include "primpl.h"
          39                 :
          40                 : /**********************************************************************/
          41                 : /******************************* PRALARM ******************************/
          42                 : /**********************************************************************/
          43                 :
          44                 : #include "obsolete/pralarm.h"
          45                 :
          46                 : struct PRAlarmID {                       /* typedef'd in pralarm.h       */
          47                 :     PRCList list;                        /* circular list linkage        */
          48                 :     PRAlarm *alarm;                      /* back pointer to owning alarm */
          49                 :     PRPeriodicAlarmFn function;          /* function to call for notify  */
          50                 :     void *clientData;                    /* opaque client context        */
          51                 :     PRIntervalTime period;               /* the client defined period    */
          52                 :     PRUint32 rate;                       /* rate of notification         */
          53                 :
          54                 :     PRUint32 accumulator;                /* keeps track of # notifies    */
          55                 :     PRIntervalTime epoch;                /* when timer was started       */
          56                 :     PRIntervalTime nextNotify;           /* when we'll next do our thing */
          57                 :     PRIntervalTime lastNotify;           /* when we last did our thing   */
          58                 : };
          59                 :
          60                 : typedef enum {alarm_active, alarm_inactive} _AlarmState;
          61                 :
          62                 : struct PRAlarm {                         /* typedef'd in pralarm.h       */
          63                 :     PRCList timers;                      /* base of alarm ids list       */
          64                 :     PRLock *lock;                        /* lock used to protect data    */
          65                 :     PRCondVar *cond;                     /* condition that used to wait  */
          66                 :     PRThread *notifier;                  /* thread to deliver notifies   */
          67                 :     PRAlarmID *current;                  /* current alarm being served   */
          68                 :     _AlarmState state;                   /* used to delete the alarm     */
          69                 : };
          70                 :
          71               0 : static PRAlarmID *pr_getNextAlarm(PRAlarm *alarm, PRAlarmID *id)
          72                 : {
          73                 : /*
          74                 :  * Puts 'id' back into the sorted list iff it's not NULL.
          75                 :  * Removes the first element from the list and returns it (or NULL).
          76                 :  * List is "assumed" to be short.
          77                 :  *
          78                 :  * NB: Caller is providing locking
          79                 :  */
          80                 :     PRCList *timer;
          81               0 :     PRAlarmID *result = id;
          82               0 :     PRIntervalTime now = PR_IntervalNow();
          83                 :
          84               0 :     if (!PR_CLIST_IS_EMPTY(&alarm->timers))
          85                 :     {
          86               0 :         if (id != NULL)  /* have to put this id back in */
          87                 :         {
          88               0 :             PRIntervalTime idDelta = now - id->nextNotify;
          89               0 :             timer = alarm->timers.next;
          90                 :             do
          91                 :             {
          92               0 :                 result = (PRAlarmID*)timer;
          93               0 :                 if ((PRIntervalTime)(now - result->nextNotify) > idDelta)
          94                 :                 {
          95               0 :                     PR_INSERT_BEFORE(&id->list, &alarm->timers);
          96               0 :                     break;
          97                 :                 }
          98               0 :                 timer = timer->next;
          99               0 :             } while (timer != &alarm->timers);
         100                 :         }
         101               0 :         result = (PRAlarmID*)(timer = PR_LIST_HEAD(&alarm->timers));
         102               0 :         PR_REMOVE_LINK(timer);  /* remove it from the list */
         103                 :     }
         104                 :
         105               0 :     return result;
         106                 : }  /* pr_getNextAlarm */
         107                 :
         108               0 : static PRIntervalTime pr_PredictNextNotifyTime(PRAlarmID *id)
         109                 : {
         110                 :     PRIntervalTime delta;
         111               0 :     PRFloat64 baseRate = (PRFloat64)id->period / (PRFloat64)id->rate;
         112               0 :     PRFloat64 offsetFromEpoch = (PRFloat64)id->accumulator * baseRate;
         113                 :
         114               0 :     id->accumulator += 1;  /* every call advances to next period */
         115               0 :     id->lastNotify = id->nextNotify;  /* just keeping track of things */
         116               0 :     id->nextNotify = (PRIntervalTime)(offsetFromEpoch + 0.5);
         117                 :
         118               0 :     delta = id->nextNotify - id->lastNotify;
         119               0 :     return delta;
         120                 : }  /* pr_PredictNextNotifyTime */
         121                 :
         122               0 : static void PR_CALLBACK pr_alarmNotifier(void *arg)
         123                 : {
         124                 :     /*
         125                 :      * This is the root of the notifier thread. There is one such thread
         126                 :      * for each PRAlarm. It may service an arbitrary (though assumed to be
         127                 :      * small) number of alarms using the same thread and structure. It
         128                 :      * continues to run until the alarm is destroyed.
         129                 :      */
         130               0 :     PRAlarmID *id = NULL;
         131               0 :     PRAlarm *alarm = (PRAlarm*)arg;
         132               0 :     enum {notify, abort, scan} why = scan;
         133                 :
         134               0 :     while (why != abort)
         135                 :     {
         136                 :         PRIntervalTime pause;
         137                 :
         138               0 :         PR_Lock(alarm->lock);
         139               0 :         while (why == scan)
         140                 :         {
         141               0 :             alarm->current = NULL;  /* reset current id */
         142               0 :             if (alarm->state == alarm_inactive) why = abort;  /* we're toast */
         143               0 :             else if (why == scan)  /* the dominant case */
         144                 :             {
         145               0 :                 id = pr_getNextAlarm(alarm, id);  /* even if it's the same */
         146               0 :                 if (id == NULL)  /* there are no alarms set */
         147               0 :                     (void)PR_WaitCondVar(alarm->cond, PR_INTERVAL_NO_TIMEOUT);
         148                 :                 else
         149                 :                 {
         150               0 :                     pause = id->nextNotify - (PR_IntervalNow() - id->epoch);
         151               0 :                     if ((PRInt32)pause <= 0)  /* is this one's time up? */
         152                 :                     {
         153               0 :                         why = notify;  /* set up to do our thing */
         154               0 :                         alarm->current = id;  /* id we're about to schedule */
         155                 :                     }
         156                 :                     else
         157               0 :                         (void)PR_WaitCondVar(alarm->cond, pause);  /* dally */
         158                 :                 }
         159                 :             }
         160                 :         }
         161               0 :         PR_Unlock(alarm->lock);
         162                 :
         163               0 :         if (why == notify)
         164                 :         {
         165               0 :             (void)pr_PredictNextNotifyTime(id);
         166               0 :             if (!id->function(id, id->clientData, ~pause))
         167                 :             {
         168                 :                 /*
         169                 :                  * Notified function decided not to continue. Free
         170                 :                  * the alarm id to make sure it doesn't get back on
         171                 :                  * the list.
         172                 :                  */
         173               0 :                 PR_DELETE(id);  /* free notifier object */
         174               0 :                 id = NULL;  /* so it doesn't get back into the list */
         175                 :             }
         176               0 :             why = scan;  /* so we can cycle through the loop again */
         177                 :         }
         178                 :     }
         179                 :
         180               0 : }  /* pr_alarm_notifier */
         181                 :
         182               0 : PR_IMPLEMENT(PRAlarm*) PR_CreateAlarm(void)
         183                 : {
         184               0 :     PRAlarm *alarm = PR_NEWZAP(PRAlarm);
         185               0 :     if (alarm != NULL)
         186                 :     {
         187               0 :         if ((alarm->lock = PR_NewLock()) == NULL) goto done;
         188               0 :         if ((alarm->cond = PR_NewCondVar(alarm->lock)) == NULL) goto done;
         189               0 :         alarm->state = alarm_active;
         190               0 :         PR_INIT_CLIST(&alarm->timers);
         191               0 :         alarm->notifier = PR_CreateThread(
         192                 :             PR_USER_THREAD, pr_alarmNotifier, alarm,
         193               0 :             PR_GetThreadPriority(PR_GetCurrentThread()),
         194                 :             PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
         195               0 :         if (alarm->notifier == NULL) goto done;
         196                 :     }
         197               0 :     return alarm;
         198                 :
         199                 : done:
         200               0 :     if (alarm->cond != NULL) PR_DestroyCondVar(alarm->cond);
         201               0 :     if (alarm->lock != NULL) PR_DestroyLock(alarm->lock);
         202               0 :     PR_DELETE(alarm);
         203               0 :     return NULL;
         204                 : }  /* CreateAlarm */
         205                 :
         206               0 : PR_IMPLEMENT(PRStatus) PR_DestroyAlarm(PRAlarm *alarm)
         207                 : {
         208                 :     PRStatus rv;
         209                 :
         210               0 :     PR_Lock(alarm->lock);
         211               0 :     alarm->state = alarm_inactive;
         212               0 :     rv = PR_NotifyCondVar(alarm->cond);
         213               0 :     PR_Unlock(alarm->lock);
         214                 :
         215               0 :     if (rv == PR_SUCCESS)
         216               0 :         rv = PR_JoinThread(alarm->notifier);
         217               0 :     if (rv == PR_SUCCESS)
         218                 :     {
         219               0 :         PR_DestroyCondVar(alarm->cond);
         220               0 :         PR_DestroyLock(alarm->lock);
         221               0 :         PR_DELETE(alarm);
         222                 :     }
         223               0 :     return rv;
         224                 : }  /* PR_DestroyAlarm */
         225                 :
         226               0 : PR_IMPLEMENT(PRAlarmID*) PR_SetAlarm(
         227                 :     PRAlarm *alarm, PRIntervalTime period, PRUint32 rate,
         228                 :     PRPeriodicAlarmFn function, void *clientData)
         229                 : {
         230                 :     /*
         231                 :      * Create a new periodic alarm an existing current structure.
         232                 :      * Set up the context and compute the first notify time (immediate).
         233                 :      * Link the new ID into the head of the list (since it's notifying
         234                 :      * immediately).
         235                 :      */
         236                 :
         237               0 :     PRAlarmID *id = PR_NEWZAP(PRAlarmID);
         238                 :
         239               0 :     if (!id)
         240               0 :         return NULL;
         241                 :
         242               0 :     id->alarm = alarm;
         243               0 :     PR_INIT_CLIST(&id->list);
         244               0 :     id->function = function;
         245               0 :     id->clientData = clientData;
         246               0 :     id->period = period;
         247               0 :     id->rate = rate;
         248               0 :     id->epoch = id->nextNotify = PR_IntervalNow();
         249               0 :     (void)pr_PredictNextNotifyTime(id);
         250                 :
         251               0 :     PR_Lock(alarm->lock);
         252               0 :     PR_INSERT_BEFORE(&id->list, &alarm->timers);
         253               0 :     PR_NotifyCondVar(alarm->cond);
         254               0 :     PR_Unlock(alarm->lock);
         255                 :
         256               0 :     return id;
         257                 : }  /* PR_SetAlarm */
         258                 :
         259               0 : PR_IMPLEMENT(PRStatus) PR_ResetAlarm(
         260                 :     PRAlarmID *id, PRIntervalTime period, PRUint32 rate)
         261                 : {
         262                 :     /*
         263                 :      * Can only be called from within the notify routine. Doesn't
         264                 :      * need locking because it can only be called from within the
         265                 :      * notify routine.
         266                 :      */
         267               0 :     if (id != id->alarm->current)
         268               0 :         return PR_FAILURE;
         269               0 :     id->period = period;
         270               0 :     id->rate = rate;
         271               0 :     id->accumulator = 1;
         272               0 :     id->epoch = PR_IntervalNow();
         273               0 :     (void)pr_PredictNextNotifyTime(id);
         274               0 :     return PR_SUCCESS;
         275                 : }  /* PR_ResetAlarm */
         276                 :
         277                 :
         278                 :

Generated by: LCOV version 1.7