Created
September 8, 2008 21:03
-
-
Save rue/9542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ucontext.h> | |
#include <iostream> | |
using namespace std; | |
ucontext_t retctx; | |
std::size_t ssize = 1024 * 64; | |
char* stack = new char[ssize]; | |
extern "C" { | |
int quux(int i) | |
{ | |
cout << "quux" << i << endl; | |
return 1; | |
} | |
} | |
class Foo | |
{ | |
public: | |
static void foo() | |
{ | |
cout << "foo->" << endl; | |
int ret = quux(5); | |
cout << ret << endl << "<-foo" << endl; | |
setcontext(&retctx); | |
return; | |
} | |
static void bar() { cout << "bar" << endl; } | |
}; | |
int caller() | |
{ | |
bool again = true; | |
getcontext(&retctx); | |
if (!again) | |
return 1; | |
again = false; | |
ucontext_t ctx; | |
getcontext(&ctx); | |
ctx.uc_link = NULL; | |
ctx.uc_stack.ss_sp = stack; | |
ctx.uc_stack.ss_size = ssize; | |
ctx.uc_stack.ss_flags = 0; | |
makecontext(&ctx, &Foo::foo, 0); | |
setcontext(&ctx); | |
} | |
int main(int c, char** v) | |
{ | |
// bool again = true; | |
// | |
// getcontext(&retctx); | |
// | |
// if (!again) { | |
// delete [] stack; | |
// Foo::bar(); | |
// | |
// cout << "end" << endl; | |
// return 0; | |
// } | |
// | |
// again = false; | |
int i = caller(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment