You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

retry.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifndef _IPXE_RETRY_H
  2. #define _IPXE_RETRY_H
  3. /** @file
  4. *
  5. * Retry timers
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <ipxe/list.h>
  10. /** Default minimum timeout value (in ticks) */
  11. #define DEFAULT_MIN_TIMEOUT ( TICKS_PER_SEC / 4 )
  12. /** Default maximum timeout value (in ticks) */
  13. #define DEFAULT_MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
  14. /** A retry timer */
  15. struct retry_timer {
  16. /** List of active timers */
  17. struct list_head list;
  18. /** Timer is currently running */
  19. unsigned int running;
  20. /** Timeout value (in ticks) */
  21. unsigned long timeout;
  22. /** Minimum timeout value (in ticks), or zero to use default
  23. *
  24. * The timeout will never be reduced below this value.
  25. */
  26. unsigned long min;
  27. /** Maximum timeout value (in ticks), or zero to use default
  28. *
  29. * The timeout will be deemed permanent (according to the
  30. * failure indicator passed to expired()) when it exceeds this
  31. * value.
  32. */
  33. unsigned long max;
  34. /** Start time (in ticks) */
  35. unsigned long start;
  36. /** Retry count */
  37. unsigned int count;
  38. /** Timer expired callback
  39. *
  40. * @v timer Retry timer
  41. * @v fail Failure indicator
  42. *
  43. * The timer will already be stopped when this method is
  44. * called. The failure indicator will be True if the retry
  45. * timeout has already exceeded @c max_timeout.
  46. */
  47. void ( * expired ) ( struct retry_timer *timer, int over );
  48. /** Reference counter
  49. *
  50. * If this interface is not part of a reference-counted
  51. * object, this field may be NULL.
  52. */
  53. struct refcnt *refcnt;
  54. };
  55. /**
  56. * Initialise a timer
  57. *
  58. * @v timer Retry timer
  59. * @v expired Timer expired callback
  60. * @v refcnt Reference counter, or NULL
  61. */
  62. static inline __attribute__ (( always_inline )) void
  63. timer_init ( struct retry_timer *timer,
  64. void ( * expired ) ( struct retry_timer *timer, int over ),
  65. struct refcnt *refcnt ) {
  66. timer->expired = expired;
  67. timer->refcnt = refcnt;
  68. }
  69. /**
  70. * Initialise a static timer
  71. *
  72. * @v expired_fn Timer expired callback
  73. */
  74. #define TIMER_INIT( expired_fn ) { \
  75. .expired = (expired_fn), \
  76. }
  77. extern void start_timer ( struct retry_timer *timer );
  78. extern void start_timer_fixed ( struct retry_timer *timer,
  79. unsigned long timeout );
  80. extern void stop_timer ( struct retry_timer *timer );
  81. extern void retry_poll ( void );
  82. /**
  83. * Start timer with no delay
  84. *
  85. * @v timer Retry timer
  86. *
  87. * This starts the timer running with a zero timeout value.
  88. */
  89. static inline void start_timer_nodelay ( struct retry_timer *timer ) {
  90. start_timer_fixed ( timer, 0 );
  91. }
  92. /**
  93. * Test to see if timer is currently running
  94. *
  95. * @v timer Retry timer
  96. * @ret running Non-zero if timer is running
  97. */
  98. static inline __attribute__ (( always_inline )) unsigned long
  99. timer_running ( struct retry_timer *timer ) {
  100. return ( timer->running );
  101. }
  102. /**
  103. * Set minimum and maximum timeouts
  104. *
  105. * @v timer Retry timer
  106. * @v min Minimum timeout (in ticks), or zero to use default
  107. * @v max Maximum timeout (in ticks), or zero to use default
  108. */
  109. static inline __attribute__ (( always_inline )) void
  110. set_timer_limits ( struct retry_timer *timer, unsigned long min,
  111. unsigned long max ) {
  112. timer->min = min;
  113. timer->max = max;
  114. }
  115. #endif /* _IPXE_RETRY_H */