123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
-
-
- #include <stddef.h>
- #include <latch.h>
- #include <gpxe/list.h>
- #include <gpxe/process.h>
- #include <gpxe/init.h>
- #include <gpxe/retry.h>
-
-
-
-
- #define MIN_TIMEOUT ( TICKS_PER_SEC / 4 )
-
-
- #define MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
-
-
- #if MIN_TIMEOUT < 7
- #undef MIN_TIMEOUT
- #define MIN_TIMEOUT 7
- #endif
-
-
- static LIST_HEAD ( timers );
-
-
- void start_timer ( struct retry_timer *timer ) {
- if ( ! timer->start )
- list_add ( &timer->list, &timers );
- timer->start = currticks();
- if ( timer->timeout < MIN_TIMEOUT )
- timer->timeout = MIN_TIMEOUT;
- DBG ( "Timer %p started at time %ld (expires at %ld)\n",
- timer, timer->start, ( timer->start + timer->timeout ) );
- }
-
-
- void stop_timer ( struct retry_timer *timer ) {
- unsigned long old_timeout = timer->timeout;
- unsigned long now = currticks();
- unsigned long runtime;
-
-
- if ( ! timer->start )
- return;
-
- list_del ( &timer->list );
- runtime = ( now - timer->start );
- timer->start = 0;
- DBG ( "Timer %p stopped at time %ld (ran for %ld)\n",
- timer, now, runtime );
-
-
-
- if ( timer->count ) {
- timer->count--;
- } else {
- timer->timeout -= ( timer->timeout >> 3 );
- timer->timeout += ( runtime >> 1 );
- if ( timer->timeout != old_timeout ) {
- DBG ( "Timer %p timeout updated to %ld\n",
- timer, timer->timeout );
- }
- }
- }
-
-
- static void timer_expired ( struct retry_timer *timer ) {
- int fail;
-
-
- DBG ( "Timer %p stopped at time %ld on expiry\n",
- timer, currticks() );
- list_del ( &timer->list );
- timer->start = 0;
- timer->count++;
-
-
- timer->timeout <<= 1;
- if ( ( fail = ( timer->timeout > MAX_TIMEOUT ) ) )
- timer->timeout = MAX_TIMEOUT;
- DBG ( "Timer %p timeout backed off to %ld\n",
- timer, timer->timeout );
-
-
- timer->expired ( timer, fail );
- }
-
-
- static void retry_step ( struct process *process ) {
- struct retry_timer *timer;
- struct retry_timer *tmp;
- unsigned long now = currticks();
- unsigned long used;
-
- list_for_each_entry_safe ( timer, tmp, &timers, list ) {
- used = ( now - timer->start );
- if ( used >= timer->timeout )
- timer_expired ( timer );
- }
-
- schedule ( process );
- }
-
-
- static struct process retry_process = {
- .step = retry_step,
- };
-
-
- static void init_retry ( void ) {
- schedule ( &retry_process );
- }
-
- INIT_FN ( INIT_PROCESS, init_retry, NULL, NULL );
|