Commit 9fcdc799 authored by Joseph Noir's avatar Joseph Noir

Add chrono impl to offer time point for RIOT timer

parent 3985278a
...@@ -21,6 +21,9 @@ ...@@ -21,6 +21,9 @@
#ifndef CAF_CHRONO_HPP #ifndef CAF_CHRONO_HPP
#define CAF_CHRONO_HPP #define CAF_CHRONO_HPP
#include <chrono>
#include <cstdio>
#ifdef __RIOTBUILD_FLAG #ifdef __RIOTBUILD_FLAG
extern "C" { extern "C" {
...@@ -29,19 +32,81 @@ extern "C" { ...@@ -29,19 +32,81 @@ extern "C" {
} }
namespace caf { namespace caf {
namespace chrono {
class duration;
namespace {
constexpr uint32_t microsec_in_secs = 1000000;
} // namespace anaonymous
class time_point {
using native_handle_type = timex_t;
public:
inline time_point() : m_handle{0,0} { }
inline time_point(timex_t&& tp) : m_handle(tp) { }
constexpr time_point(const time_point& tp) = default;
constexpr time_point(time_point&& tp) = default;
inline native_handle_type native_handle() const { return m_handle; }
time_point& operator+=(const caf::duration& d);
template<class Rep, class Period>
inline time_point& operator+=(const std::chrono::duration<Rep,Period>& d) {
auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
auto m = (std::chrono::duration_cast<std::chrono::microseconds>(d) - s);
m_handle.seconds += s.count();
m_handle.microseconds += m.count();
adjust_overhead();
return *this;
}
inline uint32_t seconds() const { return m_handle.seconds; }
inline uint32_t microseconds() const { return m_handle.microseconds; }
private:
timex_t m_handle;
void inline adjust_overhead() {
while (m_handle.microseconds >= microsec_in_secs) {
m_handle.seconds += 1;
m_handle.microseconds -= microsec_in_secs;
}
}
};
inline time_point now() {
timex_t tp;
vtimer_now(&tp);
return time_point(std::move(tp));
}
inline bool operator<(const time_point& lhs, const time_point& rhs) {
return lhs.seconds() < rhs.seconds() ||
(lhs.seconds() == rhs.seconds() &&
lhs.microseconds() < rhs.microseconds());
}
inline bool operator>(const time_point& lhs, const time_point& rhs) {
return rhs < lhs;
}
inline bool operator<=(const time_point& lhs, const time_point& rhs) {
return !(rhs < lhs);
}
inline bool operator>=(const time_point& lhs, const time_point& rhs) {
return !(lhs < rhs);
}
} // namespace chrono
} // namespace caf } // namespace caf
#else #else
#include <chrono>
namespace caf { namespace caf {
using time_point = std::chrono::high_resolution_clock::time_point;
using now = std::chrono::high_resolution_clock::now();
// todo mapping for caf // todo mapping for caf
} // namespace caf } // namespace caf
......
...@@ -24,11 +24,12 @@ ...@@ -24,11 +24,12 @@
#define CAF_CACHE_LINE_SIZE 64 #define CAF_CACHE_LINE_SIZE 64
#include <chrono>
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <cassert> #include <cassert>
#include "caf/chrono.hpp"
// GCC hack // GCC hack
#if defined(CAF_GCC) && !defined(_GLIBCXX_USE_SCHED_YIELD) #if defined(CAF_GCC) && !defined(_GLIBCXX_USE_SCHED_YIELD)
#include <time.h> #include <time.h>
......
...@@ -21,10 +21,11 @@ ...@@ -21,10 +21,11 @@
#define CAF_DURATION_HPP #define CAF_DURATION_HPP
#include <string> #include <string>
#include <chrono>
#include <cstdint> #include <cstdint>
#include <stdexcept> #include <stdexcept>
#include "caf/chrono.hpp"
namespace caf { namespace caf {
/** /**
......
...@@ -20,15 +20,15 @@ ...@@ -20,15 +20,15 @@
#ifndef CAF_ON_HPP #ifndef CAF_ON_HPP
#define CAF_ON_HPP #define CAF_ON_HPP
#include <chrono>
#include <memory> #include <memory>
#include <functional> #include <functional>
#include <type_traits> #include <type_traits>
#include "caf/unit.hpp" #include "caf/unit.hpp"
#include "caf/atom.hpp" #include "caf/atom.hpp"
#include "caf/anything.hpp" #include "caf/chrono.hpp"
#include "caf/message.hpp" #include "caf/message.hpp"
#include "caf/anything.hpp"
#include "caf/duration.hpp" #include "caf/duration.hpp"
#include "caf/skip_message.hpp" #include "caf/skip_message.hpp"
#include "caf/may_have_timeout.hpp" #include "caf/may_have_timeout.hpp"
......
...@@ -21,10 +21,10 @@ ...@@ -21,10 +21,10 @@
#define CAF_POLICY_WORK_STEALING_HPP #define CAF_POLICY_WORK_STEALING_HPP
#include <deque> #include <deque>
#include <chrono>
#include <random> #include <random>
#include <cstddef> #include <cstddef>
#include "caf/chrono.hpp"
#include "caf/thread.hpp" #include "caf/thread.hpp"
#include "caf/resumable.hpp" #include "caf/resumable.hpp"
......
...@@ -20,13 +20,13 @@ ...@@ -20,13 +20,13 @@
#ifndef CAF_SCHEDULER_ABSTRACT_COORDINATOR_HPP #ifndef CAF_SCHEDULER_ABSTRACT_COORDINATOR_HPP
#define CAF_SCHEDULER_ABSTRACT_COORDINATOR_HPP #define CAF_SCHEDULER_ABSTRACT_COORDINATOR_HPP
#include <chrono>
#include <atomic> #include <atomic>
#include <cstddef> #include <cstddef>
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/atom.hpp" #include "caf/atom.hpp"
#include "caf/actor.hpp" #include "caf/actor.hpp"
#include "caf/chrono.hpp"
#include "caf/channel.hpp" #include "caf/channel.hpp"
#include "caf/message.hpp" #include "caf/message.hpp"
#include "caf/duration.hpp" #include "caf/duration.hpp"
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
#ifdef __RIOTBUILD_FLAG #ifdef __RIOTBUILD_FLAG
#include <tuple> #include <tuple>
#include <chrono>
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <exception> #include <exception>
...@@ -39,6 +38,7 @@ extern "C" { ...@@ -39,6 +38,7 @@ extern "C" {
} }
#include "caf/mutex.hpp" #include "caf/mutex.hpp"
#include "caf/chrono.hpp"
#include "caf/ref_counted.hpp" #include "caf/ref_counted.hpp"
#include "caf/thread_util.hpp" #include "caf/thread_util.hpp"
#include "caf/intrusive_ptr.hpp" #include "caf/intrusive_ptr.hpp"
......
...@@ -20,12 +20,12 @@ ...@@ -20,12 +20,12 @@
#include "caf/scheduler/abstract_coordinator.hpp" #include "caf/scheduler/abstract_coordinator.hpp"
#include <atomic> #include <atomic>
#include <chrono>
#include <iostream> #include <iostream>
#include "caf/on.hpp" #include "caf/on.hpp"
#include "caf/send.hpp" #include "caf/send.hpp"
#include "caf/spawn.hpp" #include "caf/spawn.hpp"
#include "caf/chrono.hpp"
#include "caf/thread.hpp" #include "caf/thread.hpp"
#include "caf/anything.hpp" #include "caf/anything.hpp"
#include "caf/to_string.hpp" #include "caf/to_string.hpp"
...@@ -96,7 +96,7 @@ class timer_actor : public blocking_actor { ...@@ -96,7 +96,7 @@ class timer_actor : public blocking_actor {
// setup & local variables // setup & local variables
bool received_exit = false; bool received_exit = false;
mailbox_element_ptr msg_ptr; mailbox_element_ptr msg_ptr;
std::multimap<hrc::time_point, delayed_msg> messages; std::multimap<time_point, delayed_msg> messages;
// message handling rules // message handling rules
message_handler mfun{ message_handler mfun{
[&](const duration& d, actor_addr& from, channel& to, [&](const duration& d, actor_addr& from, channel& to,
...@@ -117,7 +117,7 @@ class timer_actor : public blocking_actor { ...@@ -117,7 +117,7 @@ class timer_actor : public blocking_actor {
if (messages.empty()) if (messages.empty())
msg_ptr = dequeue(); msg_ptr = dequeue();
else { else {
auto tout = hrc::now(); auto tout = now();
// handle timeouts (send messages) // handle timeouts (send messages)
auto it = messages.begin(); auto it = messages.begin();
while (it != messages.end() && (it->first) <= tout) { while (it != messages.end() && (it->first) <= tout) {
......
...@@ -18,13 +18,27 @@ ...@@ -18,13 +18,27 @@
* http://www.boost.org/LICENSE_1_0.txt. * * http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/ ******************************************************************************/
extern "C" { #include "caf/chrono.hpp"
#include "vtimer.h" #include "caf/duration.hpp"
}
using namespace std;
namespace caf { namespace caf {
time_point& time_point::operator+=(const duration& d) {
switch (d.unit) {
case time_unit::seconds:
m_handle.seconds += static_cast<uint32_t>(d.count);
break;
case time_unit::microseconds:
m_handle.microseconds += static_cast<uint32_t>(d.count);
break;
case time_unit::milliseconds:
m_handle.microseconds += static_cast<uint32_t>(1000 * d.count);
break;
case time_unit::invalid:
break;
}
adjust_overhead();
return *this;
}
} // namespace caf } // namespace caf
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment