我正在使用常规的makecontext / swapcontext例程在C语言中编写一个小型的概念证明光纤库,但这给我带来了麻烦(我的平台是使用clang-503.0.40
的OSX 10.9 Mavericks)。
这是我要处理的数据结构:
typedef enum {
/// Fiber is waiting to start execution
FIBER_PENDING,
/// Fiber is in the midst of executing
FIBER_EXECUTING,
/// Fiber has finished executing
FIBER_FINISHED,
/// Fiber is in the process of yielding
FIBER_YIELDING
} fiber_state;
typedef struct {
char *stack;
fiber_state state;
ucontext_t context;
} fiber;
这是小型库(三个功能,
fiber_init
,fiber_run
和fiber_yield
:#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <assert.h>
#include <stdio.h>
// Generic log w/ file name and line number
#define LOG(x) fprintf(stderr, "%s:%d |-> %s\n", __FILE__, __LINE__, x)
// the current executing fiber in this thread (or NULL of none are executing)
// (TODO: make this TLS)
static fiber *current_fiber = NULL;
/// prepare fiber 'f' to be run
void fiber_init(fiber *f, void(* fiber_func)()) {
// zero out the fiber
memset(f, 0, sizeof(fiber));
f->state = FIBER_PENDING;
f->stack = (char*) malloc(SIGSTKSZ);
// init stack config of the fiber context
ucontext_t *f_context = &(f->context);
getcontext(f_context);
f_context->uc_stack.ss_sp = f->stack;
f_context->uc_stack.ss_size = SIGSTKSZ;
f_context->uc_stack.ss_flags = 0;
// initialize the context
makecontext(f_context, fiber_func, 0);
}
/// Deallocate resources associated with 'f'
void fiber_destroy(fiber *f) {
free(f->stack);
}
/// Start or resume fiber 'f'
void fiber_run(fiber *f) {
// context to switch back to when yielding, or when the fiber returns
ucontext_t return_context;
f->context.uc_link = &return_context;
// save the fiber being swapped away from (or NULL)
fiber *old_fiber = current_fiber;
current_fiber = f;
LOG("Swapping into fiber context");
getcontext(&return_context);
int status = swapcontext(
&return_context,
&(f->context));
assert(status == 0 && "Failed to swap to fiber context");
LOG("Back to parent context from swap");
if(f->state == FIBER_YIELDING) {
f->state = FIBER_EXECUTING;
LOG("Fiber yielded");
}
else {
LOG("Fiber done executing; marking as finished");
current_fiber->state = FIBER_FINISHED;
}
// restore old fiber
current_fiber = old_fiber;
}
/// from witin a fiber, yield control to the caller's context
void fiber_yield() {
assert(current_fiber && "No fiber is currently running!");
current_fiber->state = FIBER_YIELDING;
LOG("Yielding back to caller context");
int status = swapcontext(
&(current_fiber->context),
current_fiber->context.uc_link);
assert(status == 0 && "Failed to swap to parent context");
LOG("Swapped back into fiber context (post-yield)");
}
/// query fiber state
int fiber_is_pending(const fiber *const f) {
return f->state == FIBER_PENDING;
}
int fiber_is_finished(const fiber *const f) {
return f->state == FIBER_FINISHED;
}
int fiber_is_executing(const fiber *const f) {
return f->state == FIBER_EXECUTING;
}
不过,似乎在光纤中调用fiber_yield()不能正确地将上下文与调用者的上下文交换(其引用存储在光纤上下文的uc_link中,请参见
current_fiber->context.uc_link
中的fiber_yield
)运行该程序的痕迹:
void my_func() {
LOG(" ------- I'm the fiber function! yielding");
fiber_yield();
LOG(" ------- End of my_func");
}
int main() {
fiber f;
fiber_init(&f, my_func);
while(!fiber_is_finished(&f)) {
fiber_run(&f);
LOG("Back in main run loop");
}
fiber_destroy(&f);
return 0;
}
产生输出:
fibers.c:70 |-> Swapping into fiber context
test_harness.c:5 |-> ------- I'm the fiber function! yielding
fibers.c:99 |-> Yielding back to caller context
Segmentation fault: 11
我已经了解到OSX具有堆栈对齐限制(限制为16个字节的边界),但是我正在使用
malloc
分配堆栈,这将返回一个与16字节边界对齐的块(或者我已经阅读了) 。也就是说,似乎重新排列声明的顺序有时可能会导致段错误发生,但这是非常虚假的,并且难以复制。在调用swapcontext之前检查
fiber_yield
会显示current_fiber->context
具有非常大的堆栈大小。比应该的大得多。也许这是腐败的征兆:(lldb) p current_fiber->context
(ucontext_t) $3 = {
uc_onstack = 0
uc_sigmask = 0
uc_stack = (ss_sp = 0x00007fff5fbff720, ss_size = 140734799804176, ss_flags = 0)
uc_link = 0x00007fff5fbff780
uc_mcsize = 0
uc_mcontext = 0x00007fff5fbff828
}
(lldb) p *(current_fiber->context.uc_link)
(__darwin_ucontext) $4 = {
uc_onstack = -541067328
uc_sigmask = 0
uc_stack = (ss_sp = 0x00007fff5fbff700, ss_size = 8388608, ss_flags = 0)
uc_link = 0x0000000000000000
uc_mcsize = 140734799804400
uc_mcontext = 0x00007fff5fbff7b8
}
任何线索可能会发生什么?谢谢!
最佳答案
我能够使用您的代码在OS X 10.6.8上使用Apple的gcc-4.2.1进行编译,从而重现相同的问题。
我注意到您不包含ucontext.h。使用-Wall
进行编译会使编译器警告ucontext函数的隐式声明。
添加#include <ucontext.h>
导致错误:
In file included from foo.c:6:
/usr/include/ucontext.h:42:2: error: #error ucontext routines are deprecated, and require _XOPEN_SOURCE to be defined
首先添加
#define _XOPEN_SOURCE
包括修复该问题以及该程序的行为。显然,该宏更改了相关结构的布局,以匹配这些功能的期望和要求。对于这些功能已被弃用,我不确定该如何告诉您。我知道没有支持的替代品。
关于c - 交换到ucontext_t的uc_link时,swapcontext segfaults,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23852522/