Commit f715487d authored by Joseph Noir's avatar Joseph Noir

Fix timer implementation in thread

parent fc6a2e9e
......@@ -120,21 +120,7 @@ namespace this_thread {
sleep_for(ns);
}
}
template <class Clock, class Period>
void sleep_until(const std::chrono::time_point<Clock, Period>& sleep_time) {
using namespace std::chrono;
mutex mtx;
condition_variable cv;
unique_lock<mutex> lk(mtx);
while(now() < sleep_time) {
cv.wait_until(lk,sleep_time);
}
}
template <class Period>
inline void sleep_until(const std::chrono::time_point<std::chrono::steady_clock,
Period>& sleep_time) {
sleep_for(sleep_time - now());
}
void sleep_until(const time_point& sleep_time);
} // namespace this_thread
class thread {
......
......@@ -73,22 +73,31 @@ void sleep_for(const chrono::nanoseconds& ns) {
using namespace chrono;
if (ns > nanoseconds::zero()) {
seconds s = duration_cast<seconds>(ns);
timespec ts;
using ts_sec = decltype(ts.tv_sec);
constexpr ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
if (s.count() < ts_sec_max) {
ts.tv_sec = static_cast<ts_sec>(s.count());
ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count());
microseconds ms = duration_cast<microseconds>(ns);
timex_t reltime;
constexpr uint32_t uint32_max = numeric_limits<uint32_t>::max();
if (s.count() < uint32_max) {
reltime.seconds = static_cast<uint32_t>(s.count());
reltime.microseconds =
static_cast<uint32_t>(duration_cast<microseconds>((ms-s)).count());
} else {
ts.tv_sec = ts_sec_max;
ts.tv_nsec = giga::num - 1;
reltime.seconds = uint32_max;
reltime.microseconds = uint32_max;
}
timex_t reltime;
reltime.seconds = ts.tv_sec;
reltime.microseconds = ts.tv_nsec / 1000u;
vtimer_t timer;
vtimer_set_wakeup(&timer, reltime, sched_active_pid);
thread_sleep();
vtimer_remove(&timer);
}
}
void sleep_until(const time_point& sleep_time) {
using namespace std::chrono;
mutex mtx;
condition_variable cv;
unique_lock<mutex> lk(mtx);
while(now() < sleep_time) {
cv.wait_until(lk,sleep_time);
}
}
......
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