// ------------------------------------------------------------------------ // EXPORTED API: SET A TIMEOUT // This can be called from anywhere, including recursively from the timeout // functions themselves. cyg_uint32 timeout(timeout_fun *fun, void *arg, cyg_int32 delta) { int i; timeout_entry *e; cyg_uint32 stamp; // this needs to be atomic - recursive calls from the alarm // handler thread itself are allowed: int spl = cyg_splinternal(); stamp = 0; // Assume no slots available for (e = _timeouts, i = 0; i < NTIMEOUTS; i++, e++) { if ((e->flags & CALLOUT_PENDING) == 0) { // Free entry callout_init(e); e->flags = CALLOUT_LOCAL; callout_reset(e, delta, fun, arg); stamp = (cyg_uint32)e; break; } } cyg_splx(spl); return stamp; }
// ------------------------------------------------------------------------ // HANDLER THREAD ENTRY ROUTINE // This waits on the DSR to tell it to run: static void alarm_thread(cyg_addrword_t param) { // This is from the logical ethernet dev; it calls those delivery // functions who need attention. extern void eth_drv_run_deliveries( void ); // This is from the logical ethernet dev; it tickles somehow // all ethernet devices in case one is wedged. extern void eth_drv_tickle_devices( void ); while ( 1 ) { int spl; int x; #ifdef CYGPKG_NET_FAST_THREAD_TICKLE_DEVS cyg_tick_count_t later = cyg_current_time(); later += CYGNUM_NET_FAST_THREAD_TICKLE_DEVS_DELAY; x = cyg_flag_timed_wait( &alarm_flag, -1, CYG_FLAG_WAITMODE_OR | CYG_FLAG_WAITMODE_CLR, later ); #else x = cyg_flag_wait( &alarm_flag, -1, CYG_FLAG_WAITMODE_OR | CYG_FLAG_WAITMODE_CLR ); CYG_ASSERT( 3 & x, "Lost my bits" ); #endif // CYGPKG_NET_FAST_THREAD_TICKLE_DEVS CYG_ASSERT( !((~3) & x), "Extra bits" ); spl = cyg_splinternal(); CYG_ASSERT( 0 == spl, "spl nonzero" ); if ( 2 & x ) eth_drv_run_deliveries(); #ifdef CYGPKG_NET_FAST_THREAD_TICKLE_DEVS // This is in the else clause for "do we deliver" because the // network stack might have continuous timing events anyway - so // the timeout would not occur, x would be 1 every time. else // Tickle the devices... eth_drv_tickle_devices(); #endif // CYGPKG_NET_FAST_THREAD_TICKLE_DEVS if ( 1 & x ) do_timeout(); cyg_splx(spl); } }
// ------------------------------------------------------------------------ // EXPORTED API: CANCEL A TIMEOUT // This can be called from anywhere, including recursively from the timeout // functions themselves. void untimeout(timeout_fun *fun, void * arg) { timeout_entry *e; int spl = cyg_splinternal(); for (e = _timeouts; e; e = e->next) { if (e->delta && (e->fun == fun) && (e->arg == arg)) { callout_stop(e); break; } } cyg_splx(spl); }
void callout_stop(struct callout *c) { int spl = cyg_splinternal(); #ifdef TIMEOUT_DEBUG diag_printf("%s(%p) = %x\n", __FUNCTION__, c, c->flags); #endif if ((c->flags & CALLOUT_PENDING) == 0) { c->flags &= ~CALLOUT_ACTIVE; cyg_splx(spl); return; } c->flags &= ~(CALLOUT_PENDING | CALLOUT_ACTIVE); if (c->next) { c->next->prev = c->prev; } if (c->prev) { c->prev->next = c->next; } else { timeouts = c->next; } cyg_splx(spl); }
// ------------------------------------------------------------------------ // EXPORTED API: CANCEL A TIMEOUT // This can be called from anywhere, including recursively from the timeout // functions themselves. void untimeout(timeout_fun *fun, void * arg) { int i; timeout_entry *e; int spl = cyg_splinternal(); for (e = _timeouts, i = 0; i < NTIMEOUTS; i++, e++) { if (e->delta && (e->fun == fun) && (e->arg == arg)) { callout_stop(e); break; } } cyg_splx(spl); }
// ------------------------------------------------------------------------ // EXPORTED API: SET A TIMEOUT // This can be called from anywhere, including recursively from the timeout // functions themselves. cyg_uint32 timeout(timeout_fun *fun, void *arg, cyg_int32 delta) { int i; timeout_entry *e; cyg_uint32 stamp; // this needs to be atomic - recursive calls from the alarm // handler thread itself are allowed: int spl = cyg_splinternal(); CYG_ASSERT( 0 < delta, "delta is right now, or even sooner!" ); // Renormalize delta wrt the existing set alarm, if there is one if ( last_delta > 0 ) delta += (cyg_int32)(cyg_current_time() - last_set_time); // So recorded_delta is set to either: // alarm is active: delta + NOW - THEN // alarm is inactive: delta stamp = 0; // Assume no slots available for (e = timeouts, i = 0; i < NTIMEOUTS; i++, e++) { if ((e->delta == 0) && (e->fun == 0)) { // Free entry e->delta = delta; e->fun = fun; e->arg = arg; stamp = (cyg_uint32)e; break; } } if ( stamp && // we did add a record AND (0 == last_delta || // alarm was inactive OR delta < last_delta) ) { // alarm was active but later than we need // (if last_delta is -1, this call is recursive from the handler so // also do nothing in that case) // Here, we know the new item added is sooner than that which was // most recently set, if any, so we can just go and set it up. if ( 0 == last_delta ) last_set_time = cyg_current_time(); // So we use, to set the alarm either: // alarm is active: (delta + NOW - THEN) + THEN // alarm is inactive: delta + NOW // and in either case it is true that // (recorded_delta + last_set_time) == (delta + NOW) cyg_alarm_initialize(timeout_alarm_handle, last_set_time+delta, 0); last_delta = delta; } // Otherwise, the alarm is active, AND it is set to fire sooner than we // require, so when it does, that will sort out calling the item we // just added. Or we didn't actually add a record, so nothing has // changed. #ifdef CYGPKG_INFRA_DEBUG // Do some more checking akin to that in the alarm handler: if ( last_delta != -1 ) { // not a recursive call cyg_tick_count_t now = cyg_current_time(); CYG_ASSERT( last_delta >= 0, "Bad last delta" ); delta = 0x7fffffff; for (e = timeouts, i = 0; i < NTIMEOUTS; i++, e++) { if (e->delta) { CYG_ASSERT( e->delta >= last_delta, "e->delta underflow" ); CYG_ASSERT( last_set_time + e->delta + 1000 > now, "Recorded alarm not in the future!" ); if ( e->delta < delta ) delta = e->delta; } else { CYG_ASSERT( 0 == e->fun, "Function recorded for 0 delta" ); } } CYG_ASSERT( delta == last_delta, "We didn't pick the smallest delta!" ); } #endif cyg_splx(spl); return stamp; }
void callout_reset(struct callout *c, int delta, timeout_fun *f, void *p) { int spl = cyg_splinternal(); CYG_ASSERT( 0 < delta, "delta is right now, or even sooner!" ); // Renormalize delta wrt the existing set alarm, if there is one if (last_delta > 0) { #ifdef TIMEOUT_DEBUG int _delta = delta; int _time = cyg_current_time(); #endif // TIMEOUT_DEBUG // There is an active alarm if (last_set_time != 0) { // Adjust the delta to be absolute, relative to the alarm delta += (cyg_int32)(cyg_current_time() - last_set_time); } else { // We don't know exactly when the alarm will fire, so just // schedule this event for the first time, or sometime after ; // Leaving the value alone won't be "too wrong" } #ifdef TIMEOUT_DEBUG diag_printf("delta changed from %d to %d, now: %d, then: %d, last_delta: %d\n", _delta, delta, _time, (int)last_set_time, last_delta); _show_timeouts(); #endif } // So recorded_delta is set to either: // alarm is active: delta + NOW - THEN // alarm is inactive: delta // Add this callout/timeout to the list of things to do if (c->flags & CALLOUT_PENDING) { callout_stop(c); } c->prev = (timeout_entry *)NULL; c->next = timeouts; if (c->next != (timeout_entry *)NULL) { c->next->prev = c; } timeouts = c; c->flags |= CALLOUT_PENDING | CALLOUT_ACTIVE; c->fun = f; c->arg = p; c->delta = delta; #ifdef TIMEOUT_DEBUG diag_printf("%s(%p, %d, %p, %p)\n", __FUNCTION__, c, delta, f, p); _show_timeouts(); #endif if ((0 == last_delta || // alarm was inactive OR delta < last_delta) ) { // alarm was active but later than we need // (if last_delta is -1, this call is recursive from the handler so // also do nothing in that case) // Here, we know the new item added is sooner than that which was // most recently set, if any, so we can just go and set it up. if ( 0 == last_delta ) last_set_time = cyg_current_time(); // So we use, to set the alarm either: // alarm is active: (delta + NOW - THEN) + THEN // alarm is inactive: delta + NOW // and in either case it is true that // (recorded_delta + last_set_time) == (delta + NOW) cyg_alarm_initialize(timeout_alarm_handle, last_set_time+delta, 0); #ifdef TIMEOUT_DEBUG if ((int)last_set_time == 0) { diag_printf("delta: %d, time: %ld, last_delta: %d\n", delta, last_set_time, last_delta); } #endif last_delta = delta; } // Otherwise, the alarm is active, AND it is set to fire sooner than we // require, so when it does, that will sort out calling the item we // just added. #ifdef CYGPKG_INFRA_DEBUG // Do some more checking akin to that in the alarm handler: if ( last_delta != -1 ) { // not a recursive call cyg_tick_count_t now = cyg_current_time(); timeout_entry *e; CYG_ASSERT( last_delta >= 0, "Bad last delta" ); delta = 0x7fffffff; for (e = timeouts; e; e = e->next) { if (e->delta) { CYG_ASSERT( e->delta >= last_delta, "e->delta underflow" ); // the following triggers if the "next" timeout has not just // passed, but passed by 1000 ticks - which with the normal // 1 tick = 10ms means 10 seconds - a long time. CYG_ASSERT( last_set_time + e->delta + 1000 > now, "Recorded alarm not in the future! Starved network thread?" ); if ( e->delta < delta ) delta = e->delta; } else { CYG_ASSERT( 0 == e->fun, "Function recorded for 0 delta" ); } } if (delta < last_delta) { diag_printf("Failed to pick smallest delta - picked: %d, last: %d\n", delta, last_delta); for (e = timeouts; e; e = e->next) { diag_printf(" timeout: %p at %d\n", e->fun, e->delta); } } CYG_ASSERT( delta >= last_delta, "We didn't pick the smallest delta!" ); } #endif cyg_splx(spl); }