Commit 974e4077 authored by Joseph Noir's avatar Joseph Noir

Fixed bugs in join and operator=

parent 4e1aff42
......@@ -32,8 +32,9 @@
#include <type_traits>
extern "C" {
#include <time.h>
#include "time.h"
#include "thread.h"
#include "kernel_internal.h"
}
#include "caf/mutex.hpp"
......@@ -153,9 +154,12 @@ class thread {
~thread();
thread(const thread&) = delete;
inline thread(thread&& t) noexcept : m_handle{t.m_handle} {
inline thread(thread&& t) noexcept
: m_handle{t.m_handle},
m_data{t.m_data} {
t.m_handle = thread_uninitialized;
std::swap(m_data, t.m_data);
//std::swap(m_data, t.m_data);
t.m_data.reset();
}
thread& operator=(const thread&) = delete;
thread& operator=(thread&&) noexcept;
......@@ -165,7 +169,9 @@ class thread {
std::swap(m_handle, t.m_handle);
}
inline bool joinable() const noexcept { return false; }
inline bool joinable() const noexcept {
return m_handle != thread_uninitialized;
}
void join();
void detach();
inline id get_id() const noexcept { return m_handle; }
......@@ -186,17 +192,16 @@ void* thread_proxy(void* vp) {
data->deref(); // remove count incremented in constructor
auto indices =
detail::get_right_indices<caf::detail::tl_size<F>::value - 2>(*p);
try {
detail::apply_args(std::get<1>(*p), indices, *p);
} catch (...) {
// nop
}
if (data->joining_thread != thread_uninitialized) {
thread_wakeup(data->joining_thread);
}
//sched_task_exit();
//dINT();
//sched_threads[sched_active_pid] = NULL;
--sched_num_threads;
//sched_set_status((tcb_t *)sched_active_thread, STATUS_STOPPED);
//sched_active_thread = NULL;
//cpu_switch_context_exit();
// some riot cleanup code
sched_task_exit();
return nullptr;
}
......@@ -230,6 +235,7 @@ inline thread& thread::operator=(thread&& other) noexcept {
}
m_handle = other.m_handle;
other.m_handle = thread_uninitialized;
std::swap(m_data, other.m_data);
return *this;
}
......
......@@ -2,24 +2,37 @@
#include <time.h>
#include <cerrno>
#include <system_error>
#include "caf/thread.hpp"
using namespace std;
namespace caf {
thread::~thread() {
// not needed, as our thread is always detachted
if (m_handle != thread_uninitialized) {
throw std::runtime_error("Thread is not joined or detached!");
if (joinable()) {
terminate();
}
}
void thread::join() {
if (m_handle != thread_uninitialized) {
m_data->joining_thread = sched_active_pid;
if (this->get_id() == this_thread::get_id()) {
throw system_error(make_error_code(errc::resource_deadlock_would_occur),
"Joining this leads to a deadlock.");
}
if (joinable()) {
m_handle = thread_uninitialized;
auto status = thread_getstatus(m_handle);
if (status != STATUS_NOT_FOUND && status != STATUS_STOPPED) {
m_data->joining_thread = sched_active_pid;
thread_sleep();
}
} else {
throw system_error(make_error_code(errc::invalid_argument),
"Can not join an unjoinable thread.");
}
// missing: no_such_process system error
}
void thread::join() {
......@@ -27,33 +40,34 @@ void thread::join() {
}
void thread::detach() {
if (m_handle != thread_uninitialized) {
if (joinable()) {
m_handle = thread_uninitialized;
} else {
throw system_error(make_error_code(errc::invalid_argument),
"Can not detach an unjoinable thread.");
}
}
unsigned thread::hardware_concurrency() noexcept {
// embedded systems often do not have more cores
// RIOT does currently not ofter these informations
return 1;
}
namespace this_thread {
void sleep_for(const std::chrono::nanoseconds& ns) {
using namespace std::chrono;
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);
// typedef decltype(ts.tv_sec) ts_sec;
constexpr ts_sec ts_sec_max = std::numeric_limits<ts_sec>::max();
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());
} else {
ts.tv_sec = ts_sec_max;
ts.tv_nsec = std::giga::num - 1;
ts.tv_nsec = giga::num - 1;
}
while (nanosleep(&ts, &ts) == -1 && errno == EINTR) { ; }
}
......
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