使用C构建一个框架,让它有一些未来,比如erlang
我使用sigsetjmp/siglongjmp来更改progame堆栈。这是演示:使用C构建一个框架,让它有一些未来,比如erlang,c,frameworks,erlang,C,Frameworks,Erlang,我使用sigsetjmp/siglongjmp来更改progame堆栈。这是演示: #include <stdio.h> #include <stddef.h> #include <setjmp.h> #include <stdlib.h> #include <signal.h> #include <string.h> #include <unistd.h> #define POOLSIZE 4096 int a
#include <stdio.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#define POOLSIZE 4096
int active = 0;
int total = 0;
struct thread
{
int tid;
sigjmp_buf env;
char buf[4096];
int state;
ssize_t size;
};
struct thread *thread_pool = 0L;
char* anchor_beg = 0L;
char* anchor_end = 0L;
void(*new_thread)(int) = 0L;
void sig_call(int sig)
{
char anchor;
anchor_end = &anchor;
if(sigsetjmp(thread_pool[active].env, 0) == 0)
{
thread_pool[active].size = anchor_beg - anchor_end;
memcpy(thread_pool[active].buf, anchor_end, thread_pool[active].size);
siglongjmp(thread_pool[0].env, 1);
}
else
{
memcpy(anchor_beg - thread_pool[active].size, thread_pool[active].buf, thread_pool[active].size);
}
}
void thread_new(void(*pfn)(int))
{
alarm(0);
new_thread = pfn;
thread_pool[0].state = 2;
// printf("create new thread:%d\n", total + 1);
raise(SIGUSR1);
}
void test(int thread)
{
int i = 0;
for(;i != 1000000; i++)
{
}
}
void thread_main(int thread)
{
int i = 0;
for(i = 0; i < 4000; i++)
thread_new(test);
}
void call(void(*pfn)(int))
{
active = ++ total;
thread_pool[active].tid = active;
thread_pool[active].state = 1;
ualarm(500, 0);
pfn(active);
thread_pool[active].state = 0;
}
void dispatcher()
{
thread_pool = (struct thread*)malloc(sizeof(struct thread) * POOLSIZE);
char anchor;
anchor_beg = &anchor;
thread_pool[0].tid = 0;
thread_pool[0].state = 1;
if(sigsetjmp(thread_pool[0].env, 0) == 0)
{
signal(SIGUSR1, sig_call);
signal(SIGALRM, sig_call);
call(thread_main);
}
else if(thread_pool[0].state == -1)
{
return;
}
else if(thread_pool[0].state == 2)
{
thread_pool[0].state = 1;
call(new_thread);
}
while(1)
{
int i, alive = 0;
for(i = 1; i <= total; i++)
{
if(thread_pool[i].state == 1)
{
alive ++;
ualarm(500, 0);
active = thread_pool[i].tid;
siglongjmp(thread_pool[i].env, 1);
}
}
if(alive == 0)
return;
}
}
int main()
{
dispatcher();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义池大小4096
int active=0;
int-total=0;
结构线程
{
国际贸易署;
sigjmp_buf env;
char-buf[4096];
int状态;
大小;
};
结构线程*线程池=0L;
char*anchor_beg=0L;
字符*锚定端=0升;
空(*新螺纹)(内部)=0L;
无效信号调用(int sig)
{
炭锚;
锚定\结束=&锚定;
if(sigsetjmp(线程池[active].env,0)==0)
{
线程池[active].size=anchor\u beg-anchor\u end;
memcpy(线程池[active].buf、锚定池[U]端、线程池[active].size);
siglongjmp(线程池[0].env,1);
}
其他的
{
memcpy(锚定线程池[active].size,线程池[active].buf,线程池[active].size);
}
}
空心螺纹\新(空心(*pfn)(内部))
{
报警(0);
新螺纹=pfn;
线程池[0]。状态为2;
//printf(“创建新线程:%d\n”,总计+1);
升高(SIGUSR1);
}
无效测试(内螺纹)
{
int i=0;
对于(;i!=1000000;i++)
{
}
}
空螺纹(内螺纹)
{
int i=0;
对于(i=0;i<4000;i++)
新螺纹(测试);
}
作废通知(作废(*pfn)(内部))
{
活动=++总数;
线程池[active].tid=active;
线程池[活动]。状态=1;
乌拉拉姆(500,0);
pfn(主动);
线程池[活动]。状态为0;
}
void调度程序()
{
线程池=(结构线程*)malloc(sizeof(结构线程)*池大小);
炭锚;
anchor_beg=&anchor;
线程池[0]。tid=0;
线程池[0]。状态=1;
if(sigsetjmp(线程池[0].env,0)==0)
{
信号(SIGUSR1、sig_呼叫);
信号(信号机、信号呼叫);
调用(线程\主线程);
}
else if(线程池[0]。状态==-1)
{
返回;
}
else if(线程池[0]。状态==2)
{
线程池[0]。状态=1;
调用(新线程);
}
而(1)
{
int i,活动=0;
对于(i=1;i不幸的是,您试图做的事情不起作用,因为(根据setjmp
手册):
这是因为setjmp
/longjmp
函数系列(包括sig
变量)没有保留进程堆栈的全部内容。不幸的是,您尝试的操作不起作用,因为(根据setjmp
手册):
这是因为setjmp
/longjmp
函数系列(包括sig
变体)不要保留进程堆栈的全部内容。我通过在jmp之前将堆栈复制到堆中来备份堆栈,这样我就可以在另一个jmp之后恢复它。这当然是有创意的,但它仍然不能可靠地工作。看看ucontext函数(makecontext
,getcontext
,setcontext
)如果您想这样做。我通过在jmp之前将堆栈复制到heap来备份堆栈,这样我可以在另一个jmp之后恢复堆栈。这当然是有创意的,但仍然无法可靠地工作。如果您想这样做,请查看ucontext函数(makecontext
,getcontext
,setcontext
)。
The longjmp() routines may not be called after the routine which called
the setjmp() routines returns.