Commit 0ed394ab authored by Joseph Noir's avatar Joseph Noir

Fix bug in join and sleep_for

parent 5ac04156
#include <time.h> extern "C" {
#include "vtimer.h"
}
#include <cerrno> #include <cerrno>
#include <system_error> #include <system_error>
...@@ -22,12 +24,12 @@ void thread::join() { ...@@ -22,12 +24,12 @@ void thread::join() {
"Joining this leads to a deadlock."); "Joining this leads to a deadlock.");
} }
if (joinable()) { if (joinable()) {
m_handle = thread_uninitialized;
auto status = thread_getstatus(m_handle); auto status = thread_getstatus(m_handle);
if (status != STATUS_NOT_FOUND && status != STATUS_STOPPED) { if (status != STATUS_NOT_FOUND && status != STATUS_STOPPED) {
m_data->joining_thread = sched_active_pid; m_data->joining_thread = sched_active_pid;
thread_sleep(); thread_sleep();
} }
m_handle = thread_uninitialized;
} else { } else {
throw system_error(make_error_code(errc::invalid_argument), throw system_error(make_error_code(errc::invalid_argument),
"Can not join an unjoinable thread."); "Can not join an unjoinable thread.");
...@@ -60,16 +62,20 @@ void sleep_for(const chrono::nanoseconds& ns) { ...@@ -60,16 +62,20 @@ void sleep_for(const chrono::nanoseconds& ns) {
seconds s = duration_cast<seconds>(ns); seconds s = duration_cast<seconds>(ns);
timespec ts; timespec ts;
using ts_sec = decltype(ts.tv_sec); using ts_sec = decltype(ts.tv_sec);
// typedef decltype(ts.tv_sec) ts_sec;
constexpr ts_sec ts_sec_max = numeric_limits<ts_sec>::max(); constexpr ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
if (s.count() < ts_sec_max) { if (s.count() < ts_sec_max) {
ts.tv_sec = static_cast<ts_sec>(s.count()); ts.tv_sec = static_cast<ts_sec>(s.count());
ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count()); ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((ns-s).count());
} else { } else {
ts.tv_sec = ts_sec_max; ts.tv_sec = ts_sec_max;
ts.tv_nsec = giga::num - 1; ts.tv_nsec = giga::num - 1;
} }
while (nanosleep(&ts, &ts) == -1 && errno == EINTR) { ; } 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();
} }
} }
......
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