Skip to content

Instantly share code, notes, and snippets.

@rue
Created September 8, 2008 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rue/9542 to your computer and use it in GitHub Desktop.
Save rue/9542 to your computer and use it in GitHub Desktop.
#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