retry.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <stddef.h>
00022 #include <gpxe/timer.h>
00023 #include <gpxe/list.h>
00024 #include <gpxe/process.h>
00025 #include <gpxe/init.h>
00026 #include <gpxe/retry.h>
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #define MIN_TIMEOUT 7
00046
00047
00048 static LIST_HEAD ( timers );
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 void start_timer ( struct retry_timer *timer ) {
00060 if ( ! timer->running )
00061 list_add ( &timer->list, &timers );
00062 timer->start = currticks();
00063 timer->running = 1;
00064
00065
00066 if ( timer->min_timeout == 0 )
00067 timer->min_timeout = DEFAULT_MIN_TIMEOUT;
00068
00069 if ( timer->min_timeout < MIN_TIMEOUT )
00070 timer->min_timeout = MIN_TIMEOUT;
00071
00072 if ( timer->timeout < timer->min_timeout )
00073 timer->timeout = timer->min_timeout;
00074
00075 DBG2 ( "Timer %p started at time %ld (expires at %ld)\n",
00076 timer, timer->start, ( timer->start + timer->timeout ) );
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 void start_timer_fixed ( struct retry_timer *timer, unsigned long timeout ) {
00086 start_timer ( timer );
00087 timer->timeout = timeout;
00088 DBG2 ( "Timer %p expiry time changed to %ld\n",
00089 timer, ( timer->start + timer->timeout ) );
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099 void stop_timer ( struct retry_timer *timer ) {
00100 unsigned long old_timeout = timer->timeout;
00101 unsigned long now = currticks();
00102 unsigned long runtime;
00103
00104
00105 if ( ! timer->running )
00106 return;
00107
00108 list_del ( &timer->list );
00109 runtime = ( now - timer->start );
00110 timer->running = 0;
00111 DBG2 ( "Timer %p stopped at time %ld (ran for %ld)\n",
00112 timer, now, runtime );
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 if ( timer->count ) {
00130 timer->count--;
00131 } else {
00132 timer->timeout -= ( timer->timeout >> 3 );
00133 timer->timeout += ( runtime >> 1 );
00134 if ( timer->timeout != old_timeout ) {
00135 DBG ( "Timer %p timeout updated to %ld\n",
00136 timer, timer->timeout );
00137 }
00138 }
00139 }
00140
00141
00142
00143
00144
00145
00146 static void timer_expired ( struct retry_timer *timer ) {
00147 int fail;
00148
00149
00150 DBG2 ( "Timer %p stopped at time %ld on expiry\n",
00151 timer, currticks() );
00152 assert ( timer->running );
00153 list_del ( &timer->list );
00154 timer->running = 0;
00155 timer->count++;
00156
00157
00158 timer->timeout <<= 1;
00159 if ( timer->max_timeout == 0 )
00160 timer->max_timeout = DEFAULT_MAX_TIMEOUT;
00161 if ( ( fail = ( timer->timeout > timer->max_timeout ) ) )
00162 timer->timeout = timer->max_timeout;
00163 DBG ( "Timer %p timeout backed off to %ld\n",
00164 timer, timer->timeout );
00165
00166
00167 timer->expired ( timer, fail );
00168 }
00169
00170
00171
00172
00173
00174
00175 static void retry_step ( struct process *process __unused ) {
00176 struct retry_timer *timer;
00177 struct retry_timer *tmp;
00178 unsigned long now = currticks();
00179 unsigned long used;
00180
00181 list_for_each_entry_safe ( timer, tmp, &timers, list ) {
00182 used = ( now - timer->start );
00183 if ( used >= timer->timeout )
00184 timer_expired ( timer );
00185 }
00186 }
00187
00188
00189 struct process retry_process __permanent_process = {
00190 .list = LIST_HEAD_INIT ( retry_process.list ),
00191 .step = retry_step,
00192 };