Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to fix Posix context switching after lazy init changes #3800

Merged
merged 2 commits into from Apr 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 22 additions & 13 deletions hpx/runtime/threads/coroutines/detail/context_posix.hpp
Expand Up @@ -216,8 +216,7 @@ namespace hpx { namespace threads { namespace coroutines
};

template <typename CoroutineImpl>
class ucontext_context_impl
: public ucontext_context_impl_base
class ucontext_context_impl : public ucontext_context_impl_base
{
public:
HPX_NON_COPYABLE(ucontext_context_impl);
Expand All @@ -231,16 +230,28 @@ namespace hpx { namespace threads { namespace coroutines
* Create a context that on restore invokes Functor on
* a new stack. The stack size can be optionally specified.
*/
explicit ucontext_context_impl(std::ptrdiff_t stack_size)
: m_stack_size(stack_size == -1 ? (std::ptrdiff_t)default_stack_size
: stack_size),
m_stack(alloc_stack(m_stack_size)),
cb_(&cb)
explicit ucontext_context_impl(std::ptrdiff_t stack_size = -1)
: m_stack_size(stack_size == -1
? static_cast<std::ptrdiff_t>(default_stack_size)
: stack_size),
m_stack(nullptr),
funp_(&trampoline<CoroutineImpl>)
{
HPX_ASSERT(m_stack);
funp_ = &trampoline<Functor>;
}

void init()
{
if (m_stack != nullptr) return;

m_stack = alloc_stack(static_cast<std::size_t>(m_stack_size));
if (m_stack == nullptr)
{
throw std::runtime_error("could not allocate memory for stack");
}

int error = HPX_COROUTINE_MAKE_CONTEXT(
&m_ctx, m_stack, m_stack_size, funp_, cb_, nullptr);
&m_ctx, m_stack, m_stack_size, funp_, this, nullptr);

HPX_UNUSED(error);
HPX_ASSERT(error == 0);

Expand Down Expand Up @@ -351,7 +362,7 @@ namespace hpx { namespace threads { namespace coroutines
// the stack start
increment_stack_recycle_count();
int error = HPX_COROUTINE_MAKE_CONTEXT(
&m_ctx, m_stack, m_stack_size, funp_, cb_, nullptr);
&m_ctx, m_stack, m_stack_size, funp_, this, nullptr);
HPX_UNUSED(error);
HPX_ASSERT(error == 0);
}
Expand Down Expand Up @@ -396,15 +407,13 @@ namespace hpx { namespace threads { namespace coroutines
// declare m_stack_size first so we can use it to initialize m_stack
std::ptrdiff_t m_stack_size;
void * m_stack;
void * cb_;
hkaiser marked this conversation as resolved.
Show resolved Hide resolved
void(*funp_)(void*);

#if defined(HPX_HAVE_STACKOVERFLOW_DETECTION)
struct sigaction action;
stack_t segv_stack;
#endif
};
typedef ucontext_context_impl context_impl;
}}
}}}

Expand Down