Commit f715487d authored by Joseph Noir's avatar Joseph Noir

Fix timer implementation in thread

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