Annotation of libwww/Library/src/HTTimer.c, revision 2.1
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.
! 6: ** @(#) $Id: HTTimer.c,v 1.1.2.1 1996年11月21日 21:12:26 eric Exp $
! 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 {
! 27: HTTimer * next; /* The next guy in line */
! 28: int millis;
! 29: int expires; /* Abs value in millis */
! 30: BOOL relative;
! 31: void * param; /* Client supplied context */
! 32: HTTimerCallback * cbf;
! 33: };
! 34:
! 35: PRIVATE HTTimer * timers = NULL; /* List of timers */
! 36:
! 37: /* ------------------------------------------------------------------------- */
! 38:
! 39: PUBLIC BOOL HTTimer_delete (HTTimer * timer)
! 40: {
! 41: if (timer && timers) {
! 42: HTTimer ** prev;
! 43: for (prev = &timers; *prev; prev = &(*prev)->next) {
! 44: if (*prev == timer) {
! 45: *prev = timer->next;
! 46: break;
! 47: }
! 48: }
! 49: if (THD_TRACE) HTTrace("Timer....... Deleted timer %p\n", timer);
! 50: HT_FREE(timer);
! 51: return YES;
! 52: }
! 53: return NO;
! 54: }
! 55:
! 56: PUBLIC HTTimer * HTTimer_new (HTTimer * timer, HTTimerCallback * cbf,
! 57: void * param, int millis, BOOL relative)
! 58: {
! 59: HTTimer ** prev;
! 60: int now = HTGetTimeInMillis();
! 61: if (!timer) {
! 62: if ((timer = (HTTimer *) HT_CALLOC(1, sizeof(HTTimer))) == NULL)
! 63: HT_OUTOFMEM("HTTimer_set");
! 64: if (THD_TRACE) HTTrace("Timer....... Created timer %p\n", timer);
! 65: } else {
! 66: for (prev = &timers; *prev; prev = &(*prev)->next) {
! 67: if (*prev == timer) {
! 68: *prev = timer->next;
! 69: break;
! 70: }
! 71: }
! 72: }
! 73: if (!millis) return timer;
! 74: timer->expires = millis;
! 75: if (relative) timer->expires += now;
! 76: timer->cbf = cbf;
! 77: timer->param = param;
! 78: timer->millis = millis;
! 79: timer->relative = relative;
! 80: if (timer->expires <= now) {
! 81: int status;
! 82: timer->next = NULL;
! 83: if ((status = (*timer->cbf)(timer, timer->param)) != HT_OK) {
! 84: HTTimer_delete(timer);
! 85: return NULL;
! 86: }
! 87: }
! 88: for (prev = &timers;
! 89: *prev && millis > (*prev)->expires;
! 90: prev = &(*prev)->next);
! 91: timer->next = *prev;
! 92: *prev = timer;
! 93: return timer;
! 94: }
! 95:
! 96:
! 97: PUBLIC BOOL HTTimer_deleteAll (void)
! 98: {
! 99: if (timers) {
! 100: HTTimer * timer = NULL;
! 101: while ((timer = timers)) {
! 102: timers = timers->next;
! 103: HT_FREE(timer);
! 104: }
! 105: return YES;
! 106: }
! 107: return NO;
! 108: }
! 109:
! 110: /*
! 111: ** When a timer has expired, we dispatch the event handler and re-register the
! 112: ** timer with the next expiration time.
! 113: */
! 114: PRIVATE int HTTimer_dispatch (HTTimer * timer, int now, HTTimer ** prev)
! 115: {
! 116: *prev = timer->next;
! 117: timer->next = NULL;
! 118: if (timer->relative)
! 119: HTTimer_new(timer, timer->cbf, timer->param, timer->millis, YES);
! 120: if (THD_TRACE) HTTrace("Timer....... Dispatch timer %p\n", timer);
! 121: return (*timer->cbf) (timer, timer->param);
! 122: }
! 123:
! 124: PUBLIC int HTTimer_soonest (void)
! 125: {
! 126: int now;
! 127: if (timers == NULL) return 0;
! 128: now = HTGetTimeInMillis();
! 129: while (timers && timers->expires <= now)
! 130: HTTimer_dispatch(timers, now, &timers);
! 131: if (timers)
! 132: return timers->expires - now;
! 133: return 0;
! 134: }
! 135:
! 136: PUBLIC int HTTimer_dispatchAll (void)
! 137: {
! 138: int now;
! 139: if (timers) {
! 140: now = HTGetTimeInMillis();
! 141: while (timers && timers->expires <= now)
! 142: HTTimer_dispatch(timers, now, &timers);
! 143: }
! 144: return HT_OK;
! 145: }
Webmaster