From dcff02e4c3b1214143b19685361c3c6cffa62be7 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 17 Oct 2019 12:13:17 +0000 Subject: epee: allow a random component in once_a_time timeouts --- contrib/epee/include/math_helper.h | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'contrib/epee/include/math_helper.h') diff --git a/contrib/epee/include/math_helper.h b/contrib/epee/include/math_helper.h index 604a04680..29acffaea 100644 --- a/contrib/epee/include/math_helper.h +++ b/contrib/epee/include/math_helper.h @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -230,7 +231,7 @@ namespace math_helper } } - template + template class once_a_time { uint64_t get_time() const @@ -251,12 +252,18 @@ namespace math_helper #endif } + void set_next_interval() + { + m_interval = get_interval()(); + } + public: - once_a_time():m_interval(default_interval * scale) + once_a_time() { m_last_worked_time = 0; if(!start_immediate) m_last_worked_time = get_time(); + set_next_interval(); } void trigger() @@ -273,6 +280,7 @@ namespace math_helper { bool res = functr(); m_last_worked_time = get_time(); + set_next_interval(); return res; } return true; @@ -283,9 +291,13 @@ namespace math_helper uint64_t m_interval; }; + template struct get_constant_interval { public: uint64_t operator()() const { return N; } }; + template - class once_a_time_seconds: public once_a_time<1000000, default_interval, start_immediate> {}; + class once_a_time_seconds: public once_a_time, start_immediate> {}; template - class once_a_time_milliseconds: public once_a_time<1000, default_interval, start_immediate> {}; + class once_a_time_milliseconds: public once_a_time, start_immediate> {}; + template + class once_a_time_seconds_range: public once_a_time {}; } } -- cgit v1.2.3