static void drcomd_daemon(struct drcom_handle *h) { int s; int r; s = init_daemon_socket(); if(s < 0) exit(-1); if(setup_sig_handlers()<0){ logerr("sig handlers not setup, exit.\n"); exit(1); } loginfo("drcomd %s started.\n", DRCOM_VERSION); while (1) { int maxfd; fd_set readfds; FD_ZERO(&readfds); FD_SET(s, &readfds); FD_SET(sigusr1_pipe[READ_END], &readfds); maxfd = s; if(maxfd < sigusr1_pipe[READ_END]) maxfd = sigusr1_pipe[READ_END]; unblock_sigusr1(); r = select(maxfd+1, &readfds, NULL,NULL, NULL); if(r<0){ if(errno != EINTR) logerr("signal caught\n"); continue; } if(FD_ISSET(sigusr1_pipe[READ_END], &readfds)){ char buf[256]; int *sig = (int*)buf; read(sigusr1_pipe[READ_END], &buf, sizeof(buf)); do_signals(h, *sig); } if(!FD_ISSET(s, &readfds)) continue; block_sigusr1(); do_one_client(s, h); } /* FIXME: * drcom_clean_up(); * drcom_destroy_handle(); * close_daemon_socket(); */ }
void Scheduler::int_sig() { //收到中断信号,如果当前正在Scheduler中则不做处理 if(p_curcontext == NULL || p_curcontext == &ucontext) return; printf("%d\n",&(*p_curcontext)); //否则,将上下文切换回Scheduler swapcontext(p_curcontext,&Scheduler::ucontext); //回到coroutine,开启动SIGUSR1 unblock_sigusr1(); printf("i'm come back %d\n",&(*p_curcontext)); }
void Scheduler::sleep(uthread_id utid,int t) { if(utid == INVAID_ID) return; assert(threads[utid]); time_t now = time(NULL); now += t; printf("wake up time %u\n",now); //插入到sleep列表中 sleepList.push_back(std::make_pair(threads[utid],now)); //保存当前上下文切换回scheduler threads[utid]->SetStatus(SLEEP); ucontext_t &cur_context = threads[utid]->GetContext(); swapcontext(&cur_context,&Scheduler::ucontext); //回到coroutine,开启动SIGUSR1 unblock_sigusr1(); }
void delete_per_thread_struct(void *arg) { #ifdef MQ_HEART_BEAT block_sigusr1(); pthread_setspecific(g_msg_que_key,NULL); pts_t pts = (pts_t)arg; //从heart_beat中移除 hb_t hb = get_heart_beat(); mutex_lock(hb->mtx); dlist_remove(&pts->hnode); mutex_unlock(hb->mtx); free(pts); unblock_sigusr1(); #else pthread_setspecific(g_msg_que_key,NULL); free(arg); #endif #ifdef _DEBUG printf("delete_per_thread_struct\n"); #endif }
void delete_per_thread_que(void *arg) { #ifdef MQ_HEART_BEAT block_sigusr1(); #endif ptq_t ptq = (ptq_t)arg; if(ptq->mode == MSGQ_WRITE){ dlist_remove(&ptq->write_que.pnode); } //销毁local_que中所有消息 lnode *n; if(ptq->destroy_function){ while((n = LLIST_POP(lnode*,&ptq->local_que))!=NULL) ptq->destroy_function((void*)n); } condition_destroy(&ptq->cond); ref_decrease(&ptq->que->refbase); free(ptq); #ifdef MQ_HEART_BEAT unblock_sigusr1(); #endif printf("delete_per_thread_que\n"); }