我正在用 C 创建纤维线程系统,遵循https://graphitemaster.github.io/ Fibers/ 。我有一个设置和恢复上下文的函数,我想要完成的是将函数作为具有自己的堆栈的 Fiber 启动。Linux、x86_64 SysV ABI。
\nextern void restore_context(struct fiber_context*);\nextern void create_context(struct fiber_context*);\n\nvoid foo_fiber()\n{\n printf("Called as a fiber");\n exit(0);\n}\n\nint main()\n{\n const uint32_t stack_size = 4096 * 16;\n const uint32_t red_zone_abi = 128;\n\n char* stack = aligned_alloc(16, stack_size);\n char* sp = stack + stack_size - red_zone_abi;\n\n struct fiber_context c = {0};\n c.rip = (void*)foo_fiber;\n c.rsp = (void*)sp;\n\n restore_context(&c);\n}\nRun Code Online (Sandbox Code Playgroud)\n其中restore_context代码如下:
\n.type restore_context, @function\n.global restore_context\nrestore_context:\n movq 8*0(%rdi), %r8\n\n # Load new stack pointer.\n movq 8*1(%rdi), %rsp\n\n …Run Code Online (Sandbox Code Playgroud)