void ThreadContrl::ExecuteMonitor(Monitor *pMonitor) { if(pMonitor==NULL) return ; MonitorThread *pThread=(MonitorThread*)GetIdleThread(); if(pThread==NULL) { throw MSException("Get Idle thread error"); } pMonitor->m_isRefresh=true; puts("*****Start monitor...*****"); pThread->ExecuteMonitor(pMonitor); int i=0; while(pMonitor->GetRunning()&&i<120) { ThreadEx::sleep(1000); i++; } if(pMonitor->GetRunning()) puts("*****Timeout*****"); else puts("*****Success*****"); }
void CPoolThread::Run() { int num = 0; CEventThread* pthread = NULL; while(m_runflag) { m_mutex.lock(); num = m_dequeue.GetJobAmount(); m_mutex.unlock(); if(0 != num) { for(int i=0; i< num; i++) { pthread = GetIdleThread(); if(NULL != pthread) { pthread->SetJob(m_dequeue.GetJob()); } } } else { VLOG(1)<<"no job will block .........."; //OSSleep(1); VLOG(1)<<"job continue ........"; } } }
void SmtThreadPool::Run(SmtJob* pJob,void* pJobdata) { assert(pJob!=NULL); if(GetBusyNum() == m_unMaxNum) m_MaxNumCond.Wait(); if(m_vIdleThreads.size()<m_unAvailIdleLow) { //负载过重 if(GetAllNum()+m_unNormalIdleNum-m_vIdleThreads.size() < m_unMaxNum ) CreateThread(m_unNormalIdleNum-m_vIdleThreads.size()); else CreateThread(m_unMaxNum-GetAllNum()); } else if (m_vIdleThreads.size() > m_unAvailIdleHigh) {//负载过轻 DeleteThread(m_vIdleThreads.size() - m_unNormalIdleNum); } SmtWorkThread* pIdleThread = GetIdleThread(); if(pIdleThread !=NULL) { pIdleThread->m_WorkCSLock.Lock(); MoveToBusyList(pIdleThread); pIdleThread->SetThreadPool(this); pJob->SetWorkThread(pIdleThread); pIdleThread->SetJob(pJob,pJobdata); } }
BOOL ThreadContrl::ProcessSingleTaskQueueByIndex(int QueueIndex) { Monitor *pTa=NULL; pTa=m_pTaskQueueManager->Pop(QueueIndex); while(pTa!=NULL) { if(!CheckSubsequent(pTa)) { MonitorThread *pThread=(MonitorThread*)GetIdleThread(); if(pThread==NULL) { m_isThreadPoolFull=TRUE; m_pTaskQueueManager->Push(pTa,FALSE); return TRUE; }else pThread->ExecuteMonitor(pTa); }else { m_pTaskQueueManager->Push(pTa,FALSE); return TRUE; } pTa=m_pTaskQueueManager->Pop(QueueIndex); } return TRUE; }
void CThreadPool::Run(CJob* job,void* jobdata) { if(job==NULL) return; //assert(job!=NULL); //if the busy thread num adds to m_MaxNum,so we should wait if(GetBusyNum() == m_MaxNum) { printf(")))))))))))))))))All is busy((((((((((((((((((,wait!\n"); m_MaxNumCond.Wait(); } //if the threads num after creating is also below m_MaxNum //so we can create new idle threads,else we needn't create,just to use the idle thread //printf("GetBusyNum():%d\n",GetBusyNum()); //printf("m_IdleList.size():%d\n",m_IdleList.size()); //printf("m_InitNum:%d\n",m_InitNum); //printf("GetAllNum()+m_InitNum-m_IdleList.size():%d\n",GetAllNum()+m_InitNum-m_IdleList.size()); if(m_IdleList.size()<m_AvailLow) { if(GetAllNum()+m_InitNum-m_IdleList.size() < m_MaxNum ) { printf("m_IdleList.size() < m_AvailLow,so new thread should be created \n"); CreateIdleThread(m_InitNum-m_IdleList.size()); } else { CreateIdleThread(m_MaxNum-GetAllNum()); } } /* if(m_IdleList.size()<m_AvailLow && GetAllNum()+m_InitNum-m_IdleList.size()<m_MaxNum) { printf("m_IdleList.size() < m_AvailLow,so new thread should be created \n"); CreateIdleThread(m_InitNum-m_IdleList.size()); } else if(m_IdleList.size()<m_AvailLow && m_MaxNum-GetAllNum() < m_AvailLow) { CreateIdleThread(m_MaxNum-GetAllNum()); } */ CWorkerThread* idlethr = GetIdleThread(); if(idlethr !=NULL) { idlethr->m_WorkMutex.Lock(); MoveToBusyList(idlethr); idlethr->SetThreadPool(this); job->SetWorkThread(idlethr); //printf("Job is set to thread %d \n",idlethr->GetThreadID()); idlethr->SetJob(job,jobdata); } }
/*! \fn ThreadContrl::RefreshMonitors(CMonitorList &lstMonitor) */ void ThreadContrl::RefreshMonitors(CMonitorList &lstMonitor) { if(lstMonitor.size()==1) { puts("Enter"); Monitor *pm=lstMonitor.front(); ExecuteMonitor(pm); lstMonitor.pop_front(); delete pm; return ; } CMonitorList::iterator it; for(it=lstMonitor.begin();it!=lstMonitor.end();it++) { Monitor *pM=*it; pM->m_isRefresh=true; while(1) { MonitorThread *pThread=(MonitorThread*)GetIdleThread(); if(pThread) { pThread->ExecuteMonitor(pM); break; } else { ThreadEx::sleep(1000); continue; } } } //wait for monitor complete BOOL hasRuning; for(int n=0;n<300;n++) { hasRuning=TRUE; for(it=lstMonitor.begin();it!=lstMonitor.end();it++) { if((*it)->GetRunning()) { hasRuning=FALSE; break; } } if(hasRuning) break; ThreadEx::sleep(1000); } }
bool CPoolThread::AddJob(IJob* job) { CEventThread* thread = GetIdleThread(); if(thread != NULL){ thread->SetJob(job); return true; }else{ return false; } // ThreadSuspend(); }
void ThreadContrl::StartMonitor(Monitor *pMonitor) { MonitorThread *pThread=(MonitorThread*)GetIdleThread(); if(pThread==NULL) { m_isThreadPoolFull=TRUE; AddQueue(pMonitor); return ; } pThread->ExecuteMonitor(pMonitor); }
void CWelsThreadPool::ExecuteTask() { //fprintf(stdout, "ThreadPool: scheduled tasks: ExecuteTask\n"); CWelsTaskThread* pThread = NULL; IWelsTask* pTask = NULL; while (GetWaitedTaskNum() > 0) { pThread = GetIdleThread(); if (pThread == NULL) { break; } pTask = GetWaitedTask(); //fprintf(stdout, "ThreadPool: ExecuteTask = %x at thread %x\n", pTask, pThread); pThread->SetTask (pTask); } }
ThreadEx * ThreadContrl::GetIdleThread() { // Lock lock(m_ThreadMutex); MutexLock lock(m_ThreadMutex); if(m_IdleThreadsQueue.size()>0) { ThreadEx *pte=m_IdleThreadsQueue.front(); m_IdleThreadsQueue.pop_front(); return pte; } else { if(AddNewThreads(2)==0) return NULL; return GetIdleThread(); } }
WELS_THREAD_ERROR_CODE CWelsThreadPool::QueueTask (IWelsTask* pTask) { CWelsAutoLock cLock (m_cLockPool); //fprintf(stdout, "CWelsThreadPool::QueueTask: %d, pTask=%x\n", m_iRefCount, pTask); if (GetWaitedTaskNum() == 0) { CWelsTaskThread* pThread = GetIdleThread(); if (pThread != NULL) { //fprintf(stdout, "ThreadPool: ExecuteTask = %x at thread %x\n", pTask, pThread); pThread->SetTask (pTask); return WELS_THREAD_ERROR_OK; } } //fprintf(stdout, "ThreadPool: AddTaskToWaitedList: %x\n", pTask); if (false == AddTaskToWaitedList (pTask)){ return WELS_THREAD_ERROR_GENERAL; } //fprintf(stdout, "ThreadPool: SignalThread: %x\n", pTask); SignalThread(); return WELS_THREAD_ERROR_OK; }
BOOL ThreadContrl::ProcessSingleTask(Monitor *pMonitor) { char ebuf[256]={0}; sprintf(ebuf,"In processST,class:%s\n",(LPCSTR)pMonitor->GetMonitorClass()); //putil->ErrorLog(ebuf); if(m_isThreadPoolFull) { CheckAllTaskQueue(); return TRUE; } Monitor *pTa=NULL; if(pMonitor->GetQueueIndex()>=0) pTa=m_pTaskQueueManager->Pop(pMonitor->GetQueueIndex()); else pTa=m_pTaskQueueManager->Pop((LPCSTR)pMonitor->GetMonitorClass()); while(pTa!=NULL) { sprintf(ebuf,"Get Monitor id:%s,class:%s\n",pTa->GetMonitorID(),(LPCSTR)pMonitor->GetMonitorClass()); //putil->ErrorLog(ebuf); if((!CheckSubsequent(pTa))&&(!pTa->GetRunning())) { MonitorThread *pThread=(MonitorThread*)GetIdleThread(); if(pThread==NULL) { m_isThreadPoolFull=TRUE; m_pTaskQueueManager->Push(pTa,FALSE); //putil->ErrorLog("NO idle thread"); return TRUE; }else { if(pTa==NULL) { return FALSE; } pThread->ExecuteMonitor(pTa); //putil->ErrorLog("Execute Monitor"); } }else { if(pTa->GetRunning()) { //putil->ErrorLog("Monitor is running"); m_pTaskQueueManager->Push(pTa); Monitor *ptem=pTa; if(pTa->GetQueueIndex()>=0) pTa=m_pTaskQueueManager->Pop(pTa->GetQueueIndex()); else pTa=m_pTaskQueueManager->Pop((LPCSTR)pTa->GetMonitorClass()); if(pTa==ptem) { //putil->ErrorLog("Same monitor"); return TRUE; } //putil->ErrorLog("different monitor"); continue; } else { //putil->ErrorLog("Subsequent contrl disable"); m_pTaskQueueManager->Push(pTa,FALSE); return TRUE; } } if(pTa->GetQueueIndex()>=0) pTa=m_pTaskQueueManager->Pop(pTa->GetQueueIndex()); else pTa=m_pTaskQueueManager->Pop((LPCSTR)pTa->GetMonitorClass()); } return TRUE; }
BOOL ThreadContrl::ProcessSingleTaskEx(Monitor *pMonitor) { //char ebuf[256]={0}; //sprintf(ebuf,"In processST,class:%s\n",(LPCSTR)pMonitor->GetMonitorClass()); ////putil->ErrorLog(ebuf); if(m_isThreadPoolFull) { CheckAllTaskQueue(); return TRUE; } Monitor *pTa=NULL; if(pMonitor->GetQueueIndex()>=0) pTa=m_pTaskQueueManager->Pop(pMonitor->GetQueueIndex()); else pTa=m_pTaskQueueManager->Pop((LPCSTR)pMonitor->GetMonitorClass()); Monitor *ptemp=NULL; bool bTotal=false; bool bPer=false; while(pTa!=NULL) { if(ptemp==pTa) { ////putil->ErrorLog("---loop over exit-----"); m_pTaskQueueManager->Push(pTa,FALSE); break; } if(ptemp==NULL) ptemp=pTa; //sprintf(ebuf,"Get Monitor id:%s,class:%s\n",pTa->GetMonitorID(),(LPCSTR)pMonitor->GetMonitorClass()); // //putil->ErrorLog(ebuf); if((!CheckSubsequent(pTa,bTotal,bPer))&&(!pTa->GetRunning())) { MonitorThread *pThread=(MonitorThread*)GetIdleThread(); if(pThread==NULL) { m_isThreadPoolFull=TRUE; m_pTaskQueueManager->Push(pTa,FALSE); ////putil->ErrorLog("NO idle thread"); return TRUE; }else { if(pTa==NULL) { return FALSE; } if(pTa==ptemp) ptemp=NULL; ////putil->ErrorLog("Execute Monitor"); pThread->ExecuteMonitor(pTa); } }else { ////putil->ErrorLog("Monitor subsequent contrl"); if(bTotal) { ////putil->ErrorLog("Monitor subsequent total full"); m_pTaskQueueManager->Push(pTa,FALSE); return TRUE; } m_pTaskQueueManager->Push(pTa); } if(pTa->GetQueueIndex()>=0) pTa=m_pTaskQueueManager->Pop(pTa->GetQueueIndex()); else pTa=m_pTaskQueueManager->Pop((LPCSTR)pTa->GetMonitorClass()); ////putil->ErrorLog("Loop ..."); } ////putil->ErrorLog("....check return....."); return TRUE; }