bool ThreadMgr::StartNewThread(ThreadType type, void * Parameter) { WorkThread * work; switch (type) { case TUN_THREAD: m_Tun.Start(); break; case SOCK_THREAD: m_Sock.Start(); break; case WORK_THREAD: work = new WorkThread; work->Start(); m_WorkList.push_back(work); break; case INSTRUCT_THREAD: m_Inst.Start(); break; default: assert(0); break; } return true; }
void* WorkThread::threadStartFunc(void *inData) { // Called from newly created pthread, starts the run loop of our thread WorkThread* thread = static_cast<WorkThread*>(inData); thread->run(); }
void PoolThread::run() { for(;;) { WakeUpType wakeUpType = sleep(40000); // 40 sec of timeout for(;;) { WorkThread* pWork; { ScopedLock<FastMutex> lock(_mutex); if(_jobs.empty()) { if(wakeUpType!=WAKEUP) { // STOP or TIMEOUT if(wakeUpType==TIMEOUT) stop(); return; } break; } pWork = _jobs.front(); } setPriority(pWork->priority); pWork->run(); { ScopedLock<FastMutex> lock(_mutex); _jobs.pop_front(); } --_queue; } } }
WorkQueue::WorkQueue(int workerThreads) : mIsReleased(false) { for (int i=0; i<workerThreads; ++i) { WorkThread* thread = new WorkThread(this); mThreads.push_back(thread); thread->startThread(); } }
void StreamSource::tickThread() { /* * Demon thread. */ while (streamIsOn) { WorkThread *wt = curThreadNode->workThread; wt->wakeUpThread(); curThreadNode = curThreadNode->next; usleep(video.tpf); } }
void ThreadPool::wakeUp() { ////////////////////////////////////////////////////////////////////////// ///这里应该考虑是否加锁 ///线程池线程剩余容量 unsigned int notRealizedThreadsNum = m_uiMaxThreadsNum - m_ThreadsRefList.size(); //如果没有任务或者没有空闲的工作线程则返回 if ( TasksManager::getTasksNum() == 0 || getIdleThreadsQueueNum() + notRealizedThreadsNum == 0 ) { return; } WorkThread* pTempWorkThread = NULL; //要同时执行所有任务,还需要的线程数 unsigned int threadsWanted = TasksManager::getTasksNum() - getIdleThreadsQueueNum(); //如果任务数大于空闲线程数,而且线程数未达最大值,则增加工作线程 if ( threadsWanted > 0 && m_ThreadsRefList.size() < m_uiMaxThreadsNum ) { //在线程池容量范围内,准备新增的线程 unsigned int newThreadsNum = threadsWanted >= notRealizedThreadsNum ? notRealizedThreadsNum : threadsWanted; for ( unsigned int i = 0; i < newThreadsNum; i++ ) { pTempWorkThread = new WorkThread( this ); m_ThreadsRefList.push_front( pTempWorkThread ); addAnIdleThread( pTempWorkThread ); } } ///唤醒空闲线程进行工作 while( ( pTempWorkThread = getAnIdleThread(), pTempWorkThread != NULL ) && TasksManager::getTasksNum() > 0 ) { pTempWorkThread->start(); } }
int main(int argc, char** argv) #endif { /***************/ /*INITIALIZAION*/ /***************/ //WARNING: ORDER IMPORTANT! //set defaults progstate.setDevNSmp(2048); progstate.setSampleGenBuffer(4); //FIXME progstate.setSampleGenLen(2048); progstate.setNChan(1); progstate.setSampleRate(44100); master_res.d_run_fps=INITIAL_FPS; progstate.setFps(INITIAL_FPS); progstate.setGraphColor(255,0,0); #ifdef DATA_PREFIX progstate.setDataPrefix(DATA_PREFIX); #else char* buffer = get_current_dir_name(); progstate.setDataPrefix(string(buffer) + "/data/"); free(buffer); #endif autoexec_path = progstate.getDataPrefix() + "autoexec.cfg"; //parse args, FIXME: this makes autoexec override command line! #ifndef CONSOLE_APP int argc = 0; char** argv = 0; #endif int arg_ret=parseArgs(argc, argv); if(arg_ret==-1)return 1; //some error if(arg_ret==1)return 0; //showed help //set exit function int ex_err = atexit(quit); if(ex_err != 0) { cout << "atext() failed" << endl; return 1; } //SDL int err = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK); /*SDL_INIT_EVERYTHING*/ if(err==-1) { cout << "SDL init failed" << endl; cout << SDL_GetError() << endl; return 1; } //if(progstate.getGrabInput())SDL_WM_GrabInput(SDL_GRAB_ON); initAllShared(); //WORKFIX: remove(for cmd now) /*int ret =*/ err = master_res.initInput(); if(err==-1) { cout << "init input failed" << endl; cout << SDL_GetError() << endl; exit(1); } err = master_res.initVideo(screen_w, screen_h, useGl, useFullscreen); if(err==-1) { cout << "init video failed" << endl; cout << SDL_GetError() << endl; exit(1); } string bmdata = progstate.getDataPrefix() + "bitmaps/"; cout << bmdata << endl; err = loadPixmaps( bmdata ); if(err==-1) { cout << "load pixmaps failed" << endl; exit(1); } SDL_WM_SetCaption("multiverk",NULL); SDL_WM_SetIcon(pix["icon"], NULL); err = master_res.loadFonts(); if(err==-1) { cout << "load fonts failed" << endl; exit(1); } initCon(); //FIXME: old:"after first takerMaster" still valid? hope not for(uint i=0;i<num_threads;i++) { WorkThread* th = new WorkThread(); //must have pointers: need to control destructor th->init(master_res.screen, fnt["std"]/*master_res.f*/, master_res.con); w_threads.push_back(th); } WorkThread* th_by_main = w_threads[0]; //thread run by main() //cmd_caller_th=th_by_main; //initial caller th_by_main->takeMaster(); err = shared_audio.initAudioDev(); if(err==-1) { cout << "audio dev failed" << endl; cout << SDL_GetError() << endl; exit(1); } if(err==2) { cout << "audio dev: settings not accepted(continue)" << endl; } shared_audio.build(w_threads.size(), progstate.getSampleGenBuffer(), progstate.getSampleGenLen()); //FIXM: (used by cmd now) con = master_res.con; //f = master_res.f; screen = master_res.screen; //must be last init, and before threads start! initialCmds(); //entering queued exec master_res.con->setDirectExec(false); cout << "init ok [" << progstate.getSampleRate() << " hz, " << progstate.getNChan() << " chan]" << endl; //START THREADS //ok to start draw, master_res init finished //draw_th = SDL_CreateThread(draw_th_fun, NULL); for(uint i=1;i<num_threads;i++) { w_threads[i]->startThread(); } w_th_exec(th_by_main); //SHUTDOWN waitAllThreads(); freeAllShared(); for(uint i=0;i<num_threads;i++) { w_threads[i]->free(); delete w_threads[i]; } freeCon(); master_res.freeFonts(); master_res.freeVideo(); master_res.freeInput(); shared_audio.freeAudioDev(); shared_audio.free(); //cout << SDL_GetError() << endl; return 0; }
void WorkThread::mainThreadCallback(void* data) { WorkThread* w = (WorkThread*)data; w->notifyCallbacks(); }