Commit 611214a1 authored by Joseph Noir's avatar Joseph Noir

Move stack for threads to the heap.

Creating the stack for a RIOT thread in the main
is a problem, as the main does have the same stack
size.
parent e7dcb863
...@@ -48,6 +48,7 @@ namespace caf { ...@@ -48,6 +48,7 @@ namespace caf {
namespace { namespace {
constexpr kernel_pid_t thread_uninitialized = -1; constexpr kernel_pid_t thread_uninitialized = -1;
constexpr size_t stack_size = KERNEL_CONF_STACKSIZE_MAIN;
} }
class thread_id { class thread_id {
...@@ -147,11 +148,15 @@ class thread { ...@@ -147,11 +148,15 @@ class thread {
thread(const thread&) = delete; thread(const thread&) = delete;
inline thread(thread&& t) noexcept : m_handle{t.m_handle} { inline thread(thread&& t) noexcept : m_handle{t.m_handle} {
t.m_handle = thread_uninitialized; t.m_handle = thread_uninitialized;
std::swap(m_stack, t.m_stack);
} }
thread& operator=(const thread&) = delete; thread& operator=(const thread&) = delete;
thread& operator=(thread&&) noexcept; thread& operator=(thread&&) noexcept;
void swap(thread& t) noexcept { std::swap(m_handle, t.m_handle); } void swap(thread& t) noexcept {
std::swap(m_handle, t.m_handle);
std::swap(m_stack, t.m_stack);
}
inline bool joinable() const noexcept { return false; } inline bool joinable() const noexcept { return false; }
void join(); void join();
...@@ -162,7 +167,7 @@ class thread { ...@@ -162,7 +167,7 @@ class thread {
static unsigned hardware_concurrency() noexcept; static unsigned hardware_concurrency() noexcept;
kernel_pid_t m_handle; kernel_pid_t m_handle;
char m_stack[KERNEL_CONF_STACKSIZE_MAIN]; std::unique_ptr<char[]> m_stack;
}; };
void swap(thread& lhs, thread& rhs) noexcept; void swap(thread& lhs, thread& rhs) noexcept;
...@@ -179,13 +184,16 @@ void* thread_proxy(void* vp) { ...@@ -179,13 +184,16 @@ void* thread_proxy(void* vp) {
template <class F, class ...Args, template <class F, class ...Args,
class class
> >
thread::thread(F&& f, Args&&... args) { thread::thread(F&& f, Args&&... args) : m_stack(new char[stack_size]) {
using namespace std; using namespace std;
using func_and_args = tuple<typename decay<F>::type, using func_and_args = tuple<typename decay<F>::type,
typename decay<Args>::type...>; typename decay<Args>::type...>;
// if(!m_stack) {
// m_stack = std::unique_ptr<char[]>(new char[stack_size]);
// }
std::unique_ptr<func_and_args> p(new func_and_args(decay_copy(forward<F>(f)), std::unique_ptr<func_and_args> p(new func_and_args(decay_copy(forward<F>(f)),
decay_copy(forward<Args>(args))...)); decay_copy(forward<Args>(args))...));
m_handle = thread_create(m_stack, sizeof(m_stack), m_handle = thread_create(m_stack.get(), stack_size,
PRIORITY_MAIN - 1, 0, // CREATE_WOUT_YIELD PRIORITY_MAIN - 1, 0, // CREATE_WOUT_YIELD
&thread_proxy<func_and_args>, &thread_proxy<func_and_args>,
p.get(), "caf_thread"); p.get(), "caf_thread");
...@@ -197,11 +205,12 @@ thread::thread(F&& f, Args&&... args) { ...@@ -197,11 +205,12 @@ thread::thread(F&& f, Args&&... args) {
} }
inline thread& thread::operator=(thread&& other) noexcept { inline thread& thread::operator=(thread&& other) noexcept {
if (m_handle != 0) { if (m_handle != thread_uninitialized || m_stack) {
std::terminate(); std::terminate();
} }
m_handle = other.m_handle; m_handle = other.m_handle;
other.m_handle = 0; other.m_handle = thread_uninitialized;
std::swap(m_stack, other.m_stack);
return *this; return *this;
} }
......
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