summaryrefslogtreecommitdiff
path: root/libadhocutil/runtimeContext.cpp
blob: f4e12c25ba9d7061ace89e8148feeb66d888972f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "runtimeContext.h"
#include <stdexcept>

RuntimeContext::RuntimeContext(size_t stacksize) :
	swapped(false)
{
	stack = malloc(stacksize);
	if (getcontext(&callback) == -1)
		throw std::runtime_error("Failed to getcontext");
	callback.uc_stack.ss_sp = stack;
	callback.uc_stack.ss_size = stacksize;
	callback.uc_link = &initial;
	makecontext(&callback, (void (*)())&RuntimeContext::ccallback, 1, this);
}

RuntimeContext::~RuntimeContext()
{
	free(stack);
}

void
RuntimeContext::SwapContext()
{
	swapped = !swapped;
	if (swapped) {
		swapcontext(&initial, &callback);
	}
	else {
		swapcontext(&callback, &initial);
	}
}

void
RuntimeContext::ccallback(RuntimeContext * rc)
{
	rc->Callback();
}