有没有办法将 boost context make_fcontext/jump_fcontext 与共享堆栈一起使用,通过保存/恢复堆栈来共享协程内存?
似乎make_fcontext和jump_fcontext自己写在堆栈上,当我尝试在 yield/resume 上保存/恢复堆栈时会崩溃,但我真的很难理解由于 make_fcontext/jump_fcontext 是纯汇编代码会发生什么。
这是触发分段错误的协程方法(如果我为每个协程使用不同的堆栈并且我不使用 saveStack/restoreStack,则相同的代码效果很好)
void resume()
{
if (yielded)
{
restoreStack();
yielded = false;
}
else
{
running = true;
thisContext = boost::context::make_fcontext(
(char*)sharedStackPtr + sharedStackSize ,
sharedStackSize,
my_entry_func);
}
boost::context::jump_fcontext(&yieldContext, thisContext, reinterpret_cast<intptr_t>(this));
}
void yield()
{
yielded = true;
saveStack();
boost::context::jump_fcontext(&thisContext, yieldContext, 0);
}
void restoreStack()
{
char* stackTop = (char*)sharedStackPtr + sharedStackSize ;
memcpy(stackTop - savedStackSize, savedStackPtr, savedStackSize);
}
void saveStack()
{
char dummy = 0;
char* stackPointer = &dummy;
char* stackTop = (char*)sharedStackPtr + sharedStackSize ;
assert((stackPointer < stackTop) && (stackPointer >= sharedStackPtr ));
savedStackSize = stackTop - stackPointer;
if (savedStackPtr == nullptr)
{
savedStackPtr = coroutine_stack_alloc(savedStackSize);
}
else
{
savedStackPtr = coroutine_stack_realloc(savedStackPtr, savedStackSize);
}
memcpy(savedStackPtr, stackPointer, savedStackSize);
}
任何想法 ?我在某处做错了什么吗?