home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / misc / pralarm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.1 KB  |  264 lines

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