44 static TProcess * current_tprocess = 0;
46 unsigned long TProcess::DefaultStackSize = 8 * 1024;
49 static ucontext_t resume_ctx;
51 static jmp_buf resume_ctx;
54 TProcess::TProcess(
unsigned long size) {
56 mystack =
new char[mystack_size];
59 TProcess::TProcess() {
60 mystack_size = DefaultStackSize;
61 mystack =
new char[mystack_size];
64 TProcess::~TProcess() {
68 void TProcess::resume() {
70 if (swapcontext(&resume_ctx, &running_ctx) < 0) {
75 perror(
"TProcess::resume: swapcontext failed");
78 if (!setjmp(resume_ctx)) {
79 longjmp(running_ctx, 1);
87 void TProcess::pause() {
89 if (swapcontext(&running_ctx, &resume_ctx) < 0) {
94 perror(
"TProcess::pause: swapcontext failed");
98 if (!setjmp(running_ctx)) {
99 longjmp(resume_ctx, 1);
105 current_tprocess =
this;
109 void TProcess::starter() {
110 TProcess * p = current_tprocess;
120 void TProcess::starter(
int) {
121 TProcess * p = current_tprocess;
122 if (setjmp(p->running_ctx)) {
134 void TProcess::initialize() {
148 if (getcontext(&running_ctx)) {
149 perror(
"TProcess::initialize: getcontext failed for running_ctx");
152 running_ctx.uc_link = NULL;
153 running_ctx.uc_stack.ss_sp = mystack;
154 running_ctx.uc_stack.ss_size = mystack_size;
155 running_ctx.uc_stack.ss_flags = 0;
156 current_tprocess =
this;
157 makecontext(&running_ctx, TProcess::starter, 0);
159 if (swapcontext(&resume_ctx, &running_ctx) < 0) {
160 perror(
"TProcess::initialize: swapcontext failed");
171 stack_descr.ss_flags = 0;
172 stack_descr.ss_size = mystack_size;
173 stack_descr.ss_sp = mystack;
174 sigaltstack(&stack_descr, 0);
176 sa.sa_handler = TProcess::starter;
177 sa.sa_flags = SA_ONSTACK;
178 sigemptyset(&sa.sa_mask);
179 sigaction(SIGUSR1, &sa, 0);
181 current_tprocess =
this;
191 void TProcess::process_event(
const Event * msg) {
196 const Event * TProcess::wait_for_event(
Time timeout) {
204 current_tprocess->pause();
205 msg = current_tprocess->ev;
206 current_tprocess->ev = 0;
210 }
while (
dynamic_cast<const Timeout *
>(msg) != 0 && msg != te);
215 void TProcess::stop() { };
219 #endif // TPROCESS_IMPL == 0