[BACK] Return to HTTimer.c CVS log [TXT] [DIR] Up to [Public] / libwww / Library / src

Annotation of libwww/Library/src/HTTimer.c, revision 2.14

2.1 frystyk 1: /*                                 HTEvntrg.c
 2: **   EVENT MANAGER
 3: **
 4: **   (c) COPYRIGHT MIT 1995.
 5: **   Please first read the full copyright statement in the file COPYRIGH.
2.14 ! frystyk 6: **   @(#) $Id: HTTimer.c,v 2.13 1997年01月27日 20:54:41 frystyk Exp $
2.1 frystyk 7: **
 8: **   Updated HTEvent module 
 9: **   This new module combines the functions of the old HTEvent module and 
 10: **   the HTThread module. We retain the old HTThread module, but it
 11: **   consists of calls to the HTEvent interfaces
 12: **
 13: ** Authors:
 14: **   EGP   Eric Prud'hommeaux (eric@w3.org)
 15: ** Bugs
 16: **
 17: */
 18: 
 19: /* Implementation dependent include files */
 20: #include "sysdep.h"
 21: #include "WWWUtil.h"
 22: #include "WWWCore.h"
 23: #include "HTReqMan.h"
 24: #include "HTTimer.h"                  /* Implemented here */
 25: 
 26: struct _HTTimer {
2.2 frystyk 27:   ms_t    millis;     /* Relative value in millis */
 28:   ms_t    expires;    /* Absolute value in millis */
2.1 frystyk 29:   BOOL    relative;
 30:   void *   param;     /* Client supplied context */
 31:   HTTimerCallback * cbf;
 32: };
 33: 
2.3 eric 34: PRIVATE HTList * Timers = NULL;              /* List of timers */
2.1 frystyk 35: 
2.9 frystyk 36: PRIVATE HTTimerSetCallback * SetPlatformTimer = NULL;
 37: PRIVATE HTTimerSetCallback * DeletePlatformTimer = NULL;
 38: 
2.7 eric 39: #if 1 /* WATCH_RECURSION */
 40: 
 41: PRIVATE HTTimer * InTimer = NULL;
 42: #define CHECKME(timer) if (InTimer != NULL) HTDebugBreak(); InTimer = timer;
 43: #define CLEARME(timer) if (InTimer != timer) HTDebugBreak(); InTimer = NULL;
 44: #define SETME(timer) InTimer = timer;
 45: 
 46: #else /* WATCH_RECURSION */
 47: 
 48: #define CHECKME(timer)
 49: #define CLEARME(timer)
 50: #define SETME(timer)
 51: 
 52: #endif /* !WATCH_RECURSION */
2.1 frystyk 53: /* ------------------------------------------------------------------------- */
 54: 
2.9 frystyk 55: PUBLIC BOOL HTTimer_registerSetTimerCallback (HTTimerSetCallback * cbf)
2.4 eric 56: {
2.9 frystyk 57:   if (CORE_TRACE) HTTrace("Timer....... registering %p as timer set cbf\n", cbf);
 58:   if (cbf) {
 59:    SetPlatformTimer = cbf;
 60:    return YES;
 61:   }
 62:   return NO;
2.4 eric 63: }
 64: 
2.9 frystyk 65: PUBLIC BOOL HTTimer_registerDeleteTimerCallback (HTTimerSetCallback * cbf)
2.4 eric 66: {
2.9 frystyk 67:   if (CORE_TRACE) HTTrace("Timer....... registering %p as timer delete cbf\n", cbf);
 68:   if (cbf) {
 69:    DeletePlatformTimer = cbf;
 70:    return YES;
 71:   }
 72:   return NO;
2.10 eric 73: }
 74: 
 75: PUBLIC ms_t HTTimer_getTime(HTTimer * timer)
 76: {
 77:   if (timer)
 78:    return timer->millis;
 79:   return 0;
2.4 eric 80: }
 81: 
2.1 frystyk 82: PUBLIC BOOL HTTimer_delete (HTTimer * timer)
 83: {
2.3 eric 84:   HTList * last;
 85:   HTList * cur;
2.7 eric 86:   CHECKME(timer);
 87:   if ((cur = HTList_elementOf(Timers, (void *)timer, &last)) == NULL) {
 88:    HTDebugBreak();
 89:    CLEARME(timer);
2.3 eric 90:    return NO;
2.7 eric 91:   }
2.11 frystyk 92:   if (HTList_quickRemoveElement(cur, last))
 93:    if (THD_TRACE) HTTrace("Timer....... Deleted timer %p\n", timer);
 94:   else
 95:    if (THD_TRACE) HTTrace("Timer....... Could not delete timer %p\n", timer);
2.9 frystyk 96: 
 97:   /*
 98:   ** Call any platform specific timer handler
 99:   */
 100:   if (DeletePlatformTimer) DeletePlatformTimer(timer);
 101: 
2.7 eric 102:   CLEARME(timer);
2.3 eric 103:   HT_FREE(timer);
 104:   return YES;
2.1 frystyk 105: }
 106: 
 107: PUBLIC HTTimer * HTTimer_new (HTTimer * timer, HTTimerCallback * cbf,
2.2 frystyk 108:               void * param, ms_t millis, BOOL relative)
2.1 frystyk 109: {
2.7 eric 110:   HTList * last;
 111:   HTList * cur;
2.2 frystyk 112:   ms_t now = HTGetTimeInMillis();
2.3 eric 113:   ms_t expires;
2.7 eric 114:   HTTimer * pres;
2.3 eric 115: 
2.7 eric 116:   CHECKME(timer);
2.3 eric 117:   expires = millis;
 118:   if (relative) expires += now;
 119: 
 120:   if (Timers == NULL)
 121:    Timers = HTList_new();
 122: 
 123:   if (timer) {
 124: 
 125:    /*   if a timer is specified, it should already exist
 126:     */
2.7 eric 127:    if ((cur = HTList_elementOf(Timers, (void *)timer, &last)) == NULL) {
 128:      HTDebugBreak();
 129:      CLEARME(timer);
2.3 eric 130:      return NULL;
2.7 eric 131:    }
 132:    HTList_quickRemoveElement(cur, last);
2.11 frystyk 133:    if (THD_TRACE)
 134:      HTTrace("Timer....... Found timer %p with callback %p, context %p, and %s timeout %d\n",
 135:          timer, cbf, param, relative ? "relative" : "absolute", millis);
2.7 eric 136:    /* could optimize by sorting from last when ((HTList *)(last->object))->expires < expires (most common case) */
2.3 eric 137:   } else {
 138: 
 139:    /*   create a new timer
 140:     */
2.1 frystyk 141:    if ((timer = (HTTimer *) HT_CALLOC(1, sizeof(HTTimer))) == NULL)
2.2 frystyk 142:      HT_OUTOFMEM("HTTimer_new");
2.7 eric 143:    last = Timers;
2.11 frystyk 144:    if (THD_TRACE)
 145:      HTTrace("Timer....... Created timer %p with callback %p, context %p, and %s timeout %d\n",
 146:          timer, cbf, param, relative ? "relative" : "absolute", millis);
2.1 frystyk 147:   }
2.7 eric 148:   /* sort new element into list
 149:   */
 150:   for (cur = last; 
 151:     (pres = (HTTimer *) HTList_nextObject(cur)) != NULL && pres->expires < expires; 
 152:     last = cur);
2.14 ! frystyk 153:   if (!millis) {
 ! 154:    if (THD_TRACE) HTTrace("Timer....... Timeout is 0 - returning\n");
 ! 155:    return timer;
 ! 156:   }
2.3 eric 157:   timer->expires = expires;
2.1 frystyk 158:   timer->cbf = cbf;
 159:   timer->param = param;
 160:   timer->millis = millis;
 161:   timer->relative = relative;
2.7 eric 162:   SETME(timer);
2.3 eric 163:   /* may already be obsolete
 164:   */
2.1 frystyk 165:   if (timer->expires <= now) {
 166:    int status;
2.7 eric 167:    if ((status = (*timer->cbf)(timer, timer->param, HTEvent_TIMEOUT)) != HT_OK) {
2.3 eric 168:      if (cur)
 169:        HTList_quickRemoveElement(cur, last);
 170:      HT_FREE(timer);
2.7 eric 171:      CLEARME(timer);
2.1 frystyk 172:      return NULL;
 173:    }
 174:   }
2.3 eric 175: 
 176:   /*
 177:   ** add to list if timer is new
 178:   */
2.7 eric 179:   HTList_addObject(last, (void *)timer);
2.9 frystyk 180: 
 181:   /*
 182:   ** Call any platform specific timer handler
 183:   */
 184:   if (SetPlatformTimer) SetPlatformTimer(timer);
 185: 
2.7 eric 186:   CLEARME(timer);
2.1 frystyk 187:   return timer;
 188: }
 189: 
 190: 
2.7 eric 191: PUBLIC BOOL HTTimer_refresh (HTTimer * timer, ms_t now)
 192: {
 193:   if (timer == NULL || timer->relative == NO)
 194:    return NO;
 195:   if (HTTimer_new(timer, timer->cbf, timer->param, timer->millis, YES) == NULL)
 196:    return NO;
 197:   return YES;
 198: }
 199: 
2.1 frystyk 200: PUBLIC BOOL HTTimer_deleteAll (void)
 201: {
2.3 eric 202:   HTList * cur = Timers;
 203:   HTTimer * pres;
 204:   if (Timers) {
 205:    while ((pres = (HTTimer *) HTList_nextObject(cur))) {
2.9 frystyk 206: 
 207:      /*
 208:      ** Call any platform specific timer handler
 209:      */
 210:      if (DeletePlatformTimer) DeletePlatformTimer(pres);
2.3 eric 211:      HT_FREE(pres);
2.1 frystyk 212:    }
2.3 eric 213:    HTList_delete(Timers);
 214:    Timers = NULL;
2.1 frystyk 215:    return YES;
 216:   }
 217:   return NO;
 218: }
 219: 
 220: /*
 221: ** When a timer has expired, we dispatch the event handler and re-register the
 222: ** timer with the next expiration time.
 223: */
2.3 eric 224: PRIVATE int Timer_dispatch (HTList * cur, HTList * last, int now)
2.1 frystyk 225: {
2.3 eric 226:   HTTimer * timer;
 227:   int ret;
 228: 
 229:   timer = (HTTimer *)HTList_objectOf(cur);
2.7 eric 230:   if (timer == NULL) {
 231:    HTDebugBreak();
 232:    CLEARME(timer);
2.3 eric 233:    return HT_ERROR;
2.7 eric 234:   }
2.1 frystyk 235:   if (timer->relative)
 236:    HTTimer_new(timer, timer->cbf, timer->param, timer->millis, YES);
2.3 eric 237:   else
 238:    HTList_quickRemoveElement(cur, last);
2.1 frystyk 239:   if (THD_TRACE) HTTrace("Timer....... Dispatch timer %p\n", timer);
2.7 eric 240: /*  CHECKME(timer); all entries to this function are now re-entry save */
 241:   ret = (*timer->cbf) (timer, timer->param, HTEvent_TIMEOUT);
 242: /*  CLEARME(timer); */
2.3 eric 243:   if (!timer->relative)
 244:    HT_FREE(timer);
 245:   return ret;
 246: }
 247: 
 248: PUBLIC int HTTimer_dispatch (HTTimer * timer)
 249: {
 250:   HTList * cur;
 251:   HTList * last = Timers;
 252:   ms_t now = HTGetTimeInMillis();
 253: 
 254:   cur = HTList_elementOf(Timers, (void *)timer, &last);
 255:   return Timer_dispatch(cur, last, now);
2.1 frystyk 256: }
 257: 
2.7 eric 258: PUBLIC int HTTimer_next (ms_t * pSoonest)
2.1 frystyk 259: {
2.7 eric 260:   HTList * cur;
 261:   HTList * last;
 262:   HTTimer * pres;
2.3 eric 263:   ms_t now = HTGetTimeInMillis();
2.7 eric 264:   int ret = HT_OK;
 265:   HTList * head;
2.3 eric 266: 
 267:   if (Timers == NULL)
2.7 eric 268:    return HT_OK;
2.3 eric 269: 
2.7 eric 270:   /* The Timers list may be modified during a dispatch
 271:   ** so we have to build an intermediate list
 272:   */
 273:   head = last = HTList_new();
 274:   cur = Timers;
2.3 eric 275:   while ((pres = (HTTimer *) HTList_nextObject(cur)) && pres->expires <= now) {
2.7 eric 276:    HTList_addObject(last, (void *)pres);
 277:    last = HTList_nextObject(last);
 278:   }
 279: 
 280:   /*
 281:   ** Now dispatch the intermediate list
 282:   */
 283:   cur = last = head;
 284:   while ((pres = (HTTimer *) HTList_nextObject(cur)) && ret == HT_OK) {
 285:    ret = Timer_dispatch(cur, last, now);
2.3 eric 286:    last = cur;
2.2 frystyk 287:   }
2.3 eric 288: 
2.7 eric 289:   if (pSoonest) {
 290:    /*
 291:    **   First element in Timers is the next to expire.
2.3 eric 292:    */
2.8 frystyk 293:    HTList * cur = Timers; /* for now */
 294:    pres = (HTTimer *) HTList_nextObject(cur);
2.7 eric 295:    *pSoonest = pres ? pres->expires - now : 0;
2.1 frystyk 296:   }
2.7 eric 297:   HTList_delete(head);
 298:   return ret;
2.1 frystyk 299: }
2.12 eric 300: 
2.13 frystyk 301: #ifdef WATCH_RECURSION
2.12 eric 302: extern void CheckSockEvent(HTTimer * timer, HTTimerCallback * cbf, void * param);
2.13 frystyk 303: PRIVATE void CheckTimers(void)
2.12 eric 304: {
 305:   HTList * cur = Timers;
 306:   HTTimer * pres;
 307:   while ((pres = (HTTimer *) HTList_nextObject(cur))) {
 308:    CheckSockEvent(pres, pres->cbf, pres->param);
 309:   }
 310: }
2.13 frystyk 311: #endif

Webmaster

AltStyle によって変換されたページ (->オリジナル) /