/* timer_fire -- Sends all current timer events. Returns number of milliseconds until next timer event (or a large number) */ int timer_fire(void) { evtime_t now; evtime_now(&now); while (timers && evtime_cmp(&now,&timers->date)>=0) { struct event_timer *ti = timers; at *p = cons(named("timer"), cons(new_gptr(ti), NIL)); timers = ti->next; event_add(ti->handler, p); UNLOCK(p); if (ti->period.sec>0 || ti->period.msec>0) { /* Periodic timer shoot only once per call */ while(evtime_cmp(&now,&ti->date) >= 0) evtime_add(&ti->date,&ti->period,&ti->date); ti_insert(ti); } else { /* One shot timer */ free(ti); } } if (timers) { evtime_t diff; evtime_sub(&timers->date, &now, &diff); if (diff.sec < 24*3600) return diff.sec * 1000 + diff.msec; } return 24*3600*1000; }
/* timer_add -- * Add a timer targeted to the specified handler * firing after delay milliseconds and every period * milliseconds after that. Specifying period equal * to zero sets a one shot timer. */ void *timer_add(at *handler, int delay, int period) { evtime_t now, add; evtime_now(&now); if (!handler) RAISEF("invalid event handler", handler); if (delay < 0) RAISEF("invalid timer delay", NEW_NUMBER(delay)); if (period < 0) RAISEF("invalid timer interval", NEW_NUMBER(period)); if (period > 0 && period < 20) period = 20; add.sec = delay/1000; add.msec = delay%1000; evtime_add(&now, &add, &add); return timer_add_sub(handler, add.sec, add.msec, period); }
/* timer_add -- Add a timer targeted to the specified handler firing after delay milliseconds and every period milliseconds after that. Specifying period equal to zero sets a one shot timer. */ void * timer_add(at *handler, int delay, int period) { evtime_t now, add; evtime_now(&now); if (! handler) error(NIL,"Illegal null event handler",NIL); if (delay < 0) error(NIL,"Illegal timer delay",NEW_NUMBER(delay)); if (period < 0) error(NIL,"Illegal timer interval",NEW_NUMBER(period)); if (period > 0 && period < 20) period = 20; add.sec = delay/1000; add.msec = delay%1000; evtime_add(&now, &add, &add); return timer_add_sub(handler, add.sec, add.msec, period); }