aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee
diff options
context:
space:
mode:
authorRiccardo Spagni <ric@spagni.net>2019-01-06 20:37:51 +0200
committerRiccardo Spagni <ric@spagni.net>2019-01-06 20:37:51 +0200
commit13b006137cc0b47f50968ddf35b5882dfca38bb8 (patch)
treeb77d344ec9e219d7aa1f9794c2036bf1b6b6ed12 /contrib/epee
parentMerge pull request #4938 (diff)
parentprotocol: change standby mode to not wait sleeping (diff)
downloadmonero-13b006137cc0b47f50968ddf35b5882dfca38bb8.tar.xz
Merge pull request #4949
5464725a protocol: change standby mode to not wait sleeping (moneromooo-monero) 85807dfb add a once_a_time_milliseconds class (moneromooo-monero)
Diffstat (limited to 'contrib/epee')
-rw-r--r--contrib/epee/include/math_helper.h39
1 files changed, 30 insertions, 9 deletions
diff --git a/contrib/epee/include/math_helper.h b/contrib/epee/include/math_helper.h
index ef839f609..e22e8ee6e 100644
--- a/contrib/epee/include/math_helper.h
+++ b/contrib/epee/include/math_helper.h
@@ -230,35 +230,56 @@ namespace math_helper
}
}
- template<int default_interval, bool start_immediate = true>
- class once_a_time_seconds
+ template<uint64_t scale, int default_interval, bool start_immediate = true>
+ class once_a_time
{
+ uint64_t get_time() const
+ {
+#ifdef _WIN32
+ FILETIME fileTime;
+ GetSystemTimeAsFileTime(&fileTime);
+ unsigned __int64 present = 0;
+ present |= fileTime.dwHighDateTime;
+ present = present << 32;
+ present |= fileTime.dwLowDateTime;
+ present /= 10; // mic-sec
+#else
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return tv.tv_sec * 1000000 + tv.tv_usec;
+#endif
+ }
+
public:
- once_a_time_seconds():m_interval(default_interval)
+ once_a_time():m_interval(default_interval * scale)
{
m_last_worked_time = 0;
if(!start_immediate)
- time(&m_last_worked_time);
+ m_last_worked_time = get_time();
}
template<class functor_t>
bool do_call(functor_t functr)
{
- time_t current_time = 0;
- time(&current_time);
+ uint64_t current_time = get_time();
if(current_time - m_last_worked_time > m_interval)
{
bool res = functr();
- time(&m_last_worked_time);
+ m_last_worked_time = get_time();
return res;
}
return true;
}
private:
- time_t m_last_worked_time;
- time_t m_interval;
+ uint64_t m_last_worked_time;
+ uint64_t m_interval;
};
+
+ template<int default_interval, bool start_immediate = true>
+ class once_a_time_seconds: public once_a_time<1000000, default_interval, start_immediate> {};
+ template<int default_interval, bool start_immediate = true>
+ class once_a_time_milliseconds: public once_a_time<1000, default_interval, start_immediate> {};
}
}