Annotation of libwww/Library/src/HTTimer.c, revision 2.12
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.12 ! eric 6: ** @(#) $Id: HTTimer.c,v 2.11 1997年01月23日 19:50:05 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.1 frystyk 153: if (!millis) return timer;
2.3 eric 154: timer->expires = expires;
2.1 frystyk 155: timer->cbf = cbf;
156: timer->param = param;
157: timer->millis = millis;
158: timer->relative = relative;
2.7 eric 159: SETME(timer);
2.3 eric 160: /* may already be obsolete
161: */
2.1 frystyk 162: if (timer->expires <= now) {
163: int status;
2.7 eric 164: if ((status = (*timer->cbf)(timer, timer->param, HTEvent_TIMEOUT)) != HT_OK) {
2.3 eric 165: if (cur)
166: HTList_quickRemoveElement(cur, last);
167: HT_FREE(timer);
2.7 eric 168: CLEARME(timer);
2.1 frystyk 169: return NULL;
170: }
171: }
2.3 eric 172:
173: /*
174: ** add to list if timer is new
175: */
2.7 eric 176: HTList_addObject(last, (void *)timer);
2.9 frystyk 177:
178: /*
179: ** Call any platform specific timer handler
180: */
181: if (SetPlatformTimer) SetPlatformTimer(timer);
182:
2.7 eric 183: CLEARME(timer);
2.1 frystyk 184: return timer;
185: }
186:
187:
2.7 eric 188: PUBLIC BOOL HTTimer_refresh (HTTimer * timer, ms_t now)
189: {
190: if (timer == NULL || timer->relative == NO)
191: return NO;
192: if (HTTimer_new(timer, timer->cbf, timer->param, timer->millis, YES) == NULL)
193: return NO;
194: return YES;
195: }
196:
2.1 frystyk 197: PUBLIC BOOL HTTimer_deleteAll (void)
198: {
2.3 eric 199: HTList * cur = Timers;
200: HTTimer * pres;
201: if (Timers) {
202: while ((pres = (HTTimer *) HTList_nextObject(cur))) {
2.9 frystyk 203:
204: /*
205: ** Call any platform specific timer handler
206: */
207: if (DeletePlatformTimer) DeletePlatformTimer(pres);
2.3 eric 208: HT_FREE(pres);
2.1 frystyk 209: }
2.3 eric 210: HTList_delete(Timers);
211: Timers = NULL;
2.1 frystyk 212: return YES;
213: }
214: return NO;
215: }
216:
217: /*
218: ** When a timer has expired, we dispatch the event handler and re-register the
219: ** timer with the next expiration time.
220: */
2.3 eric 221: PRIVATE int Timer_dispatch (HTList * cur, HTList * last, int now)
2.1 frystyk 222: {
2.3 eric 223: HTTimer * timer;
224: int ret;
225:
226: timer = (HTTimer *)HTList_objectOf(cur);
2.7 eric 227: if (timer == NULL) {
228: HTDebugBreak();
229: CLEARME(timer);
2.3 eric 230: return HT_ERROR;
2.7 eric 231: }
2.1 frystyk 232: if (timer->relative)
233: HTTimer_new(timer, timer->cbf, timer->param, timer->millis, YES);
2.3 eric 234: else
235: HTList_quickRemoveElement(cur, last);
2.1 frystyk 236: if (THD_TRACE) HTTrace("Timer....... Dispatch timer %p\n", timer);
2.7 eric 237: /* CHECKME(timer); all entries to this function are now re-entry save */
238: ret = (*timer->cbf) (timer, timer->param, HTEvent_TIMEOUT);
239: /* CLEARME(timer); */
2.3 eric 240: if (!timer->relative)
241: HT_FREE(timer);
242: return ret;
243: }
244:
245: PUBLIC int HTTimer_dispatch (HTTimer * timer)
246: {
247: HTList * cur;
248: HTList * last = Timers;
249: ms_t now = HTGetTimeInMillis();
250:
251: cur = HTList_elementOf(Timers, (void *)timer, &last);
252: return Timer_dispatch(cur, last, now);
2.1 frystyk 253: }
254:
2.7 eric 255: PUBLIC int HTTimer_next (ms_t * pSoonest)
2.1 frystyk 256: {
2.7 eric 257: HTList * cur;
258: HTList * last;
259: HTTimer * pres;
2.3 eric 260: ms_t now = HTGetTimeInMillis();
2.7 eric 261: int ret = HT_OK;
262: HTList * head;
2.3 eric 263:
264: if (Timers == NULL)
2.7 eric 265: return HT_OK;
2.3 eric 266:
2.7 eric 267: /* The Timers list may be modified during a dispatch
268: ** so we have to build an intermediate list
269: */
270: head = last = HTList_new();
271: cur = Timers;
2.3 eric 272: while ((pres = (HTTimer *) HTList_nextObject(cur)) && pres->expires <= now) {
2.7 eric 273: HTList_addObject(last, (void *)pres);
274: last = HTList_nextObject(last);
275: }
276:
277: /*
278: ** Now dispatch the intermediate list
279: */
280: cur = last = head;
281: while ((pres = (HTTimer *) HTList_nextObject(cur)) && ret == HT_OK) {
282: ret = Timer_dispatch(cur, last, now);
2.3 eric 283: last = cur;
2.2 frystyk 284: }
2.3 eric 285:
2.7 eric 286: if (pSoonest) {
287: /*
288: ** First element in Timers is the next to expire.
2.3 eric 289: */
2.8 frystyk 290: HTList * cur = Timers; /* for now */
291: pres = (HTTimer *) HTList_nextObject(cur);
2.7 eric 292: *pSoonest = pres ? pres->expires - now : 0;
2.1 frystyk 293: }
2.7 eric 294: HTList_delete(head);
295: return ret;
2.1 frystyk 296: }
2.12 ! eric 297:
! 298: extern void CheckSockEvent(HTTimer * timer, HTTimerCallback * cbf, void * param);
! 299: PUBLIC void CheckTimers(void)
! 300: {
! 301: HTList * cur = Timers;
! 302: HTTimer * pres;
! 303: while ((pres = (HTTimer *) HTList_nextObject(cur))) {
! 304: CheckSockEvent(pres, pres->cbf, pres->param);
! 305: }
! 306: }
! 307:
Webmaster