#include #include #include #include #include #include #include #include #include "my_thread.h" static cyg_mbox mbox0,mbox1; static cyg_handle_t m0,m1; cyg_thread tdeamon,tcreater; cyg_handle_t h_deamon,h_creater; static int living_thread; #define THREADNAME "testthread" #define THREAD_STACK_SIZE 4096 char stack[2][THREAD_STACK_SIZE]; extern void * Malloc_Thread(int size) { int true_size; void *p; Extend_Thread_Param *tp; // the true_size will include the thread parameter true_size=size+sizeof(Extend_Thread_Param); p=malloc(true_size); tp=(Extend_Thread_Param *) p; tp->memp=p; tp->stack_base=p+sizeof(Extend_Thread_Param); return p; } void test( Extend_Thread_Param *p) { Extend_Thread_Param my_thread_param; client_data *clidata; char net_addr[16],*addr,str[20]; int i,j; sprintf(str,"0123456789\n"); bzero(net_addr,sizeof(net_addr)); my_thread_param=*p; clidata=my_thread_param.private; addr=inet_ntoa(clidata->cliaddr.sin_addr); sprintf(net_addr,"%s",addr); //printf("the stack is %x\n",(unsigned int ) p); //diag_printf("connect by %s,0x%x\n",net_addr,clidata->cliaddr.sin_port); i=rand()%50; for(j=0;jsfd,str,11); close(clidata->sfd); free(clidata); cyg_mbox_put(m0,(void *)&my_thread_param); /* //printf("%s Param_thread_param address is %x\n",p->thread_name, // (cyg_addrword_t)&my_thread_param); cyg_thread_delay(100+rand()%2000); //printf("%s is end \n",my_thread_param.thread_name); cyg_mbox_put(m0,(void *)&my_thread_param); */ } void deamon_thread(void) { Extend_Thread_Param *p,ThreadParam; while(1) { p=(Extend_Thread_Param *) cyg_mbox_get(m0); ThreadParam=*p; cyg_thread_kill(ThreadParam.handle); free(ThreadParam.memp); living_thread--; } } void thread_creater() { Extend_Thread_Param *tp; void *p; int sfd; struct sockaddr_in sock_host; init_all_network_interfaces(); // TNR_INIT(); diag_printf("now server come in\n"); sfd=socket(AF_INET,SOCK_STREAM,0); sock_host.sin_family=AF_INET; sock_host.sin_port=htons(9001); sock_host.sin_addr.s_addr=htonl(INADDR_ANY); bind(sfd,(struct sockaddr *)&sock_host,sizeof(sock_host)); listen(sfd,8); while(1) { client_data *client; int cliaddr_len=sizeof(client->cliaddr),i; client=malloc(sizeof(client_data)); client->sfd=accept(sfd,(struct sockaddr *)&(client->cliaddr),&cliaddr_len); { p=Malloc_Thread(THREAD_STACK_SIZE); tp=(Extend_Thread_Param *)p; sprintf(tp->thread_name,"client%d",i); i++; tp->private=client; cyg_thread_create(14,test,(cyg_addrword_t)tp, tp->thread_name,tp->stack_base,THREAD_STACK_SIZE, &(tp->handle),&(tp->thread_instance)); cyg_thread_resume(tp->handle); living_thread++; } } // TNR_PRINT_ACTIVITY(); /* int i=0,j; living_thread=0; for(;;) { for(j=0;j<100;j++){ p=Malloc_Thread(THREAD_STACK_SIZE); tp=(Extend_Thread_Param *)p; sprintf(tp->thread_name,"%s%d",THREADNAME,i); cyg_thread_create(14,test,(cyg_addrword_t)tp, tp->thread_name,tp->stack_base,THREAD_STACK_SIZE, &(tp->handle),&(tp->thread_instance)); cyg_thread_resume(tp->handle); i++;living_thread++; } printf("thread created %d,leave %d\n",i,living_thread); cyg_thread_delay(1000); } */ } void cyg_user_start(void) { cyg_mbox_create(&m0,&mbox0); cyg_mbox_create(&m1,&mbox1); cyg_thread_create(11,thread_creater,(cyg_addrword_t)0, "Thread create",(void *)stack[0],THREAD_STACK_SIZE, &h_creater,&tcreater); cyg_thread_create(10,deamon_thread,(cyg_addrword_t)0, "Thread deamon",(void *)stack[1],THREAD_STACK_SIZE, &h_deamon,&tdeamon); cyg_thread_resume(h_deamon); cyg_thread_resume(h_creater); }