Annotation of libwww/Library/src/HTTimer.c, revision 2.16
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.16 ! frystyk 6: ** @(#) $Id: HTTimer.c,v 2.15 1997/01/31 22:55:27 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: CLEARME(timer);
2.3 eric 89: return NO;
2.7 eric 90: }
2.11 frystyk 91: if (HTList_quickRemoveElement(cur, last))
92: if (THD_TRACE) HTTrace("Timer....... Deleted timer %p\n", timer);
93: else
94: if (THD_TRACE) HTTrace("Timer....... Could not delete timer %p\n", timer);
2.9 frystyk 95:
96: /*
97: ** Call any platform specific timer handler
98: */
99: if (DeletePlatformTimer) DeletePlatformTimer(timer);
100:
2.7 eric 101: CLEARME(timer);
2.3 eric 102: HT_FREE(timer);
103: return YES;
2.1 frystyk 104: }
105:
106: PUBLIC HTTimer * HTTimer_new (HTTimer * timer, HTTimerCallback * cbf,
2.2 frystyk 107: void * param, ms_t millis, BOOL relative)
2.1 frystyk 108: {
2.7 eric 109: HTList * last;
110: HTList * cur;
2.2 frystyk 111: ms_t now = HTGetTimeInMillis();
2.3 eric 112: ms_t expires;
2.7 eric 113: HTTimer * pres;
2.3 eric 114:
2.7 eric 115: CHECKME(timer);
2.3 eric 116: expires = millis;
117: if (relative) expires += now;
118:
119: if (Timers == NULL)
120: Timers = HTList_new();
121:
122: if (timer) {
123:
124: /* if a timer is specified, it should already exist
125: */
2.7 eric 126: if ((cur = HTList_elementOf(Timers, (void *)timer, &last)) == NULL) {
127: HTDebugBreak();
128: CLEARME(timer);
2.3 eric 129: return NULL;
2.7 eric 130: }
131: HTList_quickRemoveElement(cur, last);
2.11 frystyk 132: if (THD_TRACE)
133: HTTrace("Timer....... Found timer %p with callback %p, context %p, and %s timeout %d\n",
134: timer, cbf, param, relative ? "relative" : "absolute", millis);
2.7 eric 135: /* could optimize by sorting from last when ((HTList *)(last->object))->expires < expires (most common case) */
2.3 eric 136: } else {
137:
138: /* create a new timer
139: */
2.1 frystyk 140: if ((timer = (HTTimer *) HT_CALLOC(1, sizeof(HTTimer))) == NULL)
2.2 frystyk 141: HT_OUTOFMEM("HTTimer_new");
2.7 eric 142: last = Timers;
2.11 frystyk 143: if (THD_TRACE)
144: HTTrace("Timer....... Created timer %p with callback %p, context %p, and %s timeout %d\n",
145: timer, cbf, param, relative ? "relative" : "absolute", millis);
2.1 frystyk 146: }
2.16 ! frystyk 147:
! 148: /*
! 149: ** Sort new element into list
! 150: */
2.7 eric 151: for (cur = last;
152: (pres = (HTTimer *) HTList_nextObject(cur)) != NULL && pres->expires < expires;
153: last = cur);
2.16 ! frystyk 154:
! 155: /*
! 156: ** If the expiration is 0 then we still register it but dispatch it immediately.
! 157: */
! 158: if (!millis) if (THD_TRACE) HTTrace("Timer....... Timeout is 0 - expires NOW\n");
! 159:
2.3 eric 160: timer->expires = expires;
2.1 frystyk 161: timer->cbf = cbf;
162: timer->param = param;
163: timer->millis = millis;
164: timer->relative = relative;
2.7 eric 165: SETME(timer);
2.3 eric 166:
167: /*
168: ** add to list if timer is new
169: */
2.7 eric 170: HTList_addObject(last, (void *)timer);
2.9 frystyk 171:
172: /*
173: ** Call any platform specific timer handler
174: */
175: if (SetPlatformTimer) SetPlatformTimer(timer);
176:
2.16 ! frystyk 177: /*
! 178: ** Check if the timer object has already expired
! 179: */
! 180: if (timer->expires <= now) {
! 181: int status;
! 182: if ((status = (*timer->cbf)(timer, timer->param, HTEvent_TIMEOUT)) != HT_OK) {
! 183: if (cur) HTList_quickRemoveElement(cur, last);
! 184: CLEARME(timer);
! 185: HT_FREE(timer);
! 186: return NULL;
! 187: }
! 188: }
! 189:
2.7 eric 190: CLEARME(timer);
2.1 frystyk 191: return timer;
192: }
193:
194:
2.7 eric 195: PUBLIC BOOL HTTimer_refresh (HTTimer * timer, ms_t now)
196: {
197: if (timer == NULL || timer->relative == NO)
198: return NO;
199: if (HTTimer_new(timer, timer->cbf, timer->param, timer->millis, YES) == NULL)
200: return NO;
201: return YES;
202: }
203:
2.1 frystyk 204: PUBLIC BOOL HTTimer_deleteAll (void)
205: {
2.3 eric 206: HTList * cur = Timers;
207: HTTimer * pres;
208: if (Timers) {
209: while ((pres = (HTTimer *) HTList_nextObject(cur))) {
2.9 frystyk 210:
211: /*
212: ** Call any platform specific timer handler
213: */
214: if (DeletePlatformTimer) DeletePlatformTimer(pres);
2.3 eric 215: HT_FREE(pres);
2.1 frystyk 216: }
2.3 eric 217: HTList_delete(Timers);
218: Timers = NULL;
2.1 frystyk 219: return YES;
220: }
221: return NO;
222: }
223:
224: /*
225: ** When a timer has expired, we dispatch the event handler and re-register the
226: ** timer with the next expiration time.
227: */
2.3 eric 228: PRIVATE int Timer_dispatch (HTList * cur, HTList * last, int now)
2.1 frystyk 229: {
2.3 eric 230: HTTimer * timer;
231: int ret;
232:
233: timer = (HTTimer *)HTList_objectOf(cur);
2.7 eric 234: if (timer == NULL) {
235: HTDebugBreak();
236: CLEARME(timer);
2.3 eric 237: return HT_ERROR;
2.7 eric 238: }
2.1 frystyk 239: if (timer->relative)
240: HTTimer_new(timer, timer->cbf, timer->param, timer->millis, YES);
2.3 eric 241: else
242: HTList_quickRemoveElement(cur, last);
2.1 frystyk 243: if (THD_TRACE) HTTrace("Timer....... Dispatch timer %p\n", timer);
2.7 eric 244: /* CHECKME(timer); all entries to this function are now re-entry save */
245: ret = (*timer->cbf) (timer, timer->param, HTEvent_TIMEOUT);
246: /* CLEARME(timer); */
2.3 eric 247: if (!timer->relative)
248: HT_FREE(timer);
249: return ret;
250: }
251:
252: PUBLIC int HTTimer_dispatch (HTTimer * timer)
253: {
254: HTList * cur;
255: HTList * last = Timers;
256: ms_t now = HTGetTimeInMillis();
257:
258: cur = HTList_elementOf(Timers, (void *)timer, &last);
259: return Timer_dispatch(cur, last, now);
2.1 frystyk 260: }
261:
2.7 eric 262: PUBLIC int HTTimer_next (ms_t * pSoonest)
2.1 frystyk 263: {
2.7 eric 264: HTList * cur;
265: HTList * last;
266: HTTimer * pres;
2.3 eric 267: ms_t now = HTGetTimeInMillis();
2.7 eric 268: int ret = HT_OK;
269: HTList * head;
2.3 eric 270:
271: if (Timers == NULL)
2.7 eric 272: return HT_OK;
2.3 eric 273:
2.7 eric 274: /* The Timers list may be modified during a dispatch
275: ** so we have to build an intermediate list
276: */
277: head = last = HTList_new();
278: cur = Timers;
2.3 eric 279: while ((pres = (HTTimer *) HTList_nextObject(cur)) && pres->expires <= now) {
2.7 eric 280: HTList_addObject(last, (void *)pres);
281: last = HTList_nextObject(last);
282: }
283:
284: /*
285: ** Now dispatch the intermediate list
286: */
287: cur = last = head;
288: while ((pres = (HTTimer *) HTList_nextObject(cur)) && ret == HT_OK) {
289: ret = Timer_dispatch(cur, last, now);
2.3 eric 290: last = cur;
2.2 frystyk 291: }
2.3 eric 292:
2.7 eric 293: if (pSoonest) {
294: /*
295: ** First element in Timers is the next to expire.
2.3 eric 296: */
2.8 frystyk 297: HTList * cur = Timers; /* for now */
298: pres = (HTTimer *) HTList_nextObject(cur);
2.7 eric 299: *pSoonest = pres ? pres->expires - now : 0;
2.1 frystyk 300: }
2.7 eric 301: HTList_delete(head);
302: return ret;
2.1 frystyk 303: }
2.12 eric 304:
2.13 frystyk 305: #ifdef WATCH_RECURSION
2.12 eric 306: extern void CheckSockEvent(HTTimer * timer, HTTimerCallback * cbf, void * param);
2.13 frystyk 307: PRIVATE void CheckTimers(void)
2.12 eric 308: {
309: HTList * cur = Timers;
310: HTTimer * pres;
311: while ((pres = (HTTimer *) HTList_nextObject(cur))) {
312: CheckSockEvent(pres, pres->cbf, pres->param);
313: }
314: }
2.13 frystyk 315: #endif
Webmaster