/************************************************************************** * Create a red-black tree node; used internally by the red-black tree. **************************************************************************/ RedBlackNode* RedBlackNode_create(const float k, TrackRecord* p_val, RedBlackNode* p_p, color_t c) { RedBlackNode* p_rv = (RedBlackNode*) rb_malloc(); p_rv->p_up = p_p; p_rv->p_left = p_rv->p_right = NULL; p_rv->color = c; p_rv->key = k; LinkList_init(&p_rv->valList); LinkList_append(&(p_rv->valList), p_val); return p_rv; }
int main(int argc,char*argv[]) { if( 3 != argc ) { perror("argc error\n"); exit(-1); } //init socket int fd_server = udp_init(argv[1],atoi(argv[2])); //init client table pUSR client_table; LinkList_init(&client_table); //init struct msg MSG client_msg; bzero(&client_msg,sizeof(MSG)); //init select fd_set read_set,ready_set; FD_ZERO(&read_set); FD_SET(fd_server,&read_set); struct timeval t; //start select while(1) { t.tv_sec = 0; t.tv_usec = 1000; ready_set = read_set; if( -1 == select(fd_server+1,&ready_set,NULL,NULL,&t) ) { perror("selcet error\n"); exit(-1); } //select handle if( FD_ISSET(fd_server,&ready_set) ) { //recv msg from client,and select the way to handle SA client_addr; // bzero(&client_addr,sizeof(SA)); MSG client_msg; bzero(&client_msg,sizeof(MSG)); int len_client_addr = sizeof(SA); recvfrom(fd_server,&client_msg,sizeof(MSG),0,(struct sockaddr*)&client_addr,&len_client_addr); printf("The type is %d\n",client_msg.s_type); //choice a way to handle switch(client_msg.s_type) { case MSG_USR: msg_usr(fd_server,&client_msg); break; case MSG_GRP: msg_grp(client_table,fd_server,&client_msg); break; case MSG_ON: msg_on(&client_table,&client_addr); break; case MSG_OFF: msg_off(&client_table,&client_addr); break; } } } }
PRIVATE inline TaskQueue TaskQueue_init(TaskQueue tskq) { LinkList_init(&tskq->list); pthread_mutex_init(&tskq->lock, NULL); return tskq; }
PRIVATE void* thread_handle(void* arg) { Thread targ = (Thread)arg; /* internal task queue */ link_list_t list; LinkList tlist = LinkList_init(&list); task_result_t res; Task tsk = NULL; START: switch(targ->signal) { case TS_STOP: goto FINAL; case TS_WAIT: goto WAIT; case TS_INIT: goto INIT; } INIT: /* a simple algorithm to keep the balance of outter & inner task queue */ Thread_getTask(targ, tlist); /* iterate the internal task queue & process them */ uint32_t l = LinkList_length(tlist); while(l > 0) { tsk = (Task)LinkList_get(tlist); if(tsk->pre_process) { tsk->pre_process(tsk); } res = tsk->process(tsk); if(tsk->post_process) { tsk->post_process(tsk); } if(res == RES_PEND) { LinkList_put(tlist, tsk); } --l; } goto START; /* will not arrive here */ WAIT: /* consume all tasks inside the outter task queue */ Thread_getAllTasks(targ, tlist); while(LinkList_length(tlist) != 0) { tsk = (Task)LinkList_get(tlist); if(tsk->pre_process) { tsk->pre_process(tsk); } res = tsk->process(tsk); if(tsk->post_process) { tsk->post_process(tsk); } if(res == RES_PEND) { LinkList_put(tlist, tsk); } } pthread_exit(NULL); //return NULL; FINAL: /* pushback the incomplete tasks */ while(LinkList_length(tlist) != 0) { tsk = (Task)LinkList_get(tlist); PUSH_BACK_TASK(targ, tsk); } pthread_exit(NULL); /* will not arrive here */ //return NULL; }