Skip to content

Commit

Permalink
Improve internal backoff to perform
Browse files Browse the repository at this point in the history
lazy initialization of the seed.
  • Loading branch information
P-p-H-d committed Mar 26, 2024
1 parent 7bf9f6a commit 5174781
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions m-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -5034,7 +5034,7 @@ m_core_parse2_enum (const char str[], const char **endptr)
/* Can be increased / decreased by user code if needed
to increase / decrease backoff of code */
#ifndef M_USE_BACKOFF_MAX_COUNT
#define M_USE_BACKOFF_MAX_COUNT 6
#define M_USE_BACKOFF_MAX_COUNT 8
#endif

/* Exponential backoff object.
Expand All @@ -5048,14 +5048,12 @@ typedef struct m_core_backoff_s {
unsigned int seed; // Initial seed
} m_core_backoff_ct[1];

/* Initialize a backoff object.
* Use the C function rand to initialize its internal seed.
* It should be good enough for the purpose of the backoff */
/* Initialize a backoff object. */
M_INLINE void
m_core_backoff_init(m_core_backoff_ct backoff)
{
backoff->count = 0;
backoff->seed = (unsigned int) rand();
backoff->seed = 0;
}

/* Reset the count of the backoff object */
Expand All @@ -5071,6 +5069,16 @@ m_core_backoff_reset(m_core_backoff_ct backoff)
M_INLINE void
m_core_backoff_wait(m_core_backoff_ct backoff)
{
if (backoff->count <= 2) {
backoff->count++;
if (backoff->count < 3)
return;
// Use the C function rand to initialize its internal seed.
// It should be good enough for the purpose of the backoff.
// We only do it once if we already have performed several loops
// to avoid paying the cost of 'rand' for the small start.
backoff->seed = (unsigned int) rand();
}
/* x is qualified as volatile to avoid being optimized away
by the compiler in the active sleep loop */
volatile int x = 0;
Expand Down

0 comments on commit 5174781

Please sign in to comment.