diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-03-16 17:20:42 +0100 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-03-16 17:20:42 +0100 |
commit | 76df856e0df1b67770038e657f526f915c11a140 (patch) | |
tree | 413bb8405bc9a1abe0a0a029eaf483682347dc23 /src/utils/timer.cpp | |
parent | 6cdc94206f3c0cc60dca5ed024619b78dff01a64 (diff) |
Implement regular event launching using systemd event loop
Regular events is made launch a timerfd event from binder event loop and then
in the event handler, reschedule next launch in the future based upon the
signal frequency.
Change-Id: I0b1e84eb2135474f4bcc5ee256ba513eea4035a6
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src/utils/timer.cpp')
-rw-r--r-- | src/utils/timer.cpp | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/src/utils/timer.cpp b/src/utils/timer.cpp index e708a7ed..9d385fe5 100644 --- a/src/utils/timer.cpp +++ b/src/utils/timer.cpp @@ -15,25 +15,43 @@ * limitations under the License. */ +#include <time.h> #include <stdlib.h> #include "timer.hpp" #define MS_PER_SECOND 1000 +long long int system_time_us() +{ + struct timespec t_usec; + long long int timestamp_usec; + + if(!::clock_gettime(CLOCK_MONOTONIC, &t_usec)) + timestamp_usec = (t_usec.tv_nsec / 1000ll) + (t_usec.tv_sec* 1000000ll); + return timestamp_usec; +} + long long int system_time_ms() { - struct timeb t_msec; + struct timespec t_msec; long long int timestamp_msec; - if(!::ftime(&t_msec)) - { - timestamp_msec = (t_msec.time) * 1000ll + - t_msec.millitm; - } + if(!::clock_gettime(CLOCK_MONOTONIC, &t_msec)) + timestamp_msec = (t_msec.tv_nsec / 1000000ll) + (t_msec.tv_sec* 1000ll); return timestamp_msec; } +long long int system_time_s() +{ + struct timespec t_sec; + long long int timestamp_sec; + + if(!::clock_gettime(CLOCK_MONOTONIC, &t_sec)) + timestamp_sec = t_sec.tv_sec; + return timestamp_sec; +} + frequency_clock_t::frequency_clock_t() : frequency_{10.0}, last_tick_{0}, time_function_{nullptr} {} @@ -66,12 +84,17 @@ bool frequency_clock_t::elapsed(bool stagger) if(!started() && stagger) last_tick_ = get_time_function()() - (rand() % int(period)); else - // Make sure it ticks the the first call to conditionalTick(...) + // Make sure it ticks the the first call elapsed_time = !started() ? period : get_time_function()() - last_tick_; return frequency_ == 0 || elapsed_time >= period; } +float frequency_clock_t::get_frequency() const +{ + return frequency_; +} + /// @brief Force the clock to tick, regardless of it its time has actually /// elapsed. void frequency_clock_t::tick() |