void Scheduler::schedule() { while(total_count > 0) { //首先执行active列表中的uthread std::list<u_thread*>::iterator it = activeList.begin(); std::list<u_thread*>::iterator end = activeList.end(); for( ; it != end; ++it) { if(*it && (*it)->GetStatus() == ACTIVED) { ucontext_t &cur_context = (*it)->GetContext(); p_curcontext = &cur_context; swapcontext(&ucontext,&cur_context); //回到调度器,阻塞SIGUSR1 block_sigusr1(); p_curcontext = &ucontext; uthread_id uid = (*it)->GetUid(); if((*it)->GetStatus() == DIE) { printf("%d die\n",uid); delete threads[uid]; threads[uid] = 0; --total_count; activeList.erase(it); break; } else if((*it)->GetStatus() == SLEEP) { printf("%d sleep\n",uid); activeList.erase(it); break; } } } //看看Sleep列表中是否有uthread该醒来了 std::list<std::pair<u_thread*,time_t> >::iterator its = sleepList.begin(); std::list<std::pair<u_thread*,time_t> >::iterator ends = sleepList.end(); time_t now = time(NULL); for( ; its != ends; ++its) { //可以醒来了 if(now >= its->second) { u_thread *uthread = its->first; uthread->SetStatus(ACTIVED); activeList.push_back(uthread); sleepList.erase(its); break; } } } printf("scheduler end\n"); }
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 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"); }