use _setjmp/_longjmp to speed up coroutine switching

As described in http://www.1024cores.net/home/lock-free-algorithms/tricks/fibers
This commit is contained in:
Marc-André Lureau 2011-08-12 16:32:20 +02:00
parent a73f841b59
commit 1cbfe2b8fc
2 changed files with 13 additions and 1 deletions

View File

@ -21,6 +21,7 @@
#include <config.h>
#include "continuation.h"
#undef _FORTIFY_SOURCE
/*
* va_args to makecontext() must be type 'int', so passing
@ -40,6 +41,11 @@ static void continuation_trampoline(int i0, int i1)
arg.i[1] = i1;
cc = arg.p;
if (_setjmp(cc->jmp) == 0) {
ucontext_t tmp;
swapcontext(&tmp, &cc->last);
}
cc->entry(cc);
}
@ -56,6 +62,7 @@ int cc_init(struct continuation *cc)
cc->uc.uc_stack.ss_flags = 0;
makecontext(&cc->uc, (void *)continuation_trampoline, 2, arg.i[0], arg.i[1]);
swapcontext(&cc->last, &cc->uc);
return 0;
}
@ -78,7 +85,10 @@ int cc_swap(struct continuation *from, struct continuation *to)
else if (to->exited == 1)
return 1;
return swapcontext(&from->uc, &to->uc);
if (_setjmp(from->jmp) == 0)
_longjmp(to->jmp, 1);
return 0;
}
/*
* Local variables:

View File

@ -23,6 +23,7 @@
#include <stddef.h>
#include <ucontext.h>
#include <setjmp.h>
struct continuation
{
@ -35,6 +36,7 @@ struct continuation
ucontext_t uc;
ucontext_t last;
int exited;
jmp_buf jmp;
};
int cc_init(struct continuation *cc);