//----------------------------------------------------------------------------------------------- void WorkerThreadEntryFunc( void* data ) { WorkerThread* workerThread = static_cast< WorkerThread* >( data ); while( !g_isQuitting ) { Job* job = JobManager::GetJobFromTodoList( workerThread->m_jobTypeToHandle ); if( job ) { job->Execute(); JobManager::ReportCompletedJob( job ); } else { Sleep( 0 ); } } }
void ThreadPool::WorkerThread::operator()() { Job *job = NULL; while (true) { { std::unique_lock<std::mutex> lock(pool.queue_mutex); // look for a work item while (!pool.stop && pool.jobs.empty()) { pool.condition.wait(lock); } if (pool.stop && pool.jobs.empty()) // exit if the pool is stopped and we have no more jobs { return; } job = pool.jobs.front(); pool.jobs.pop_front(); } job->Execute(); if (job->GetJobBatch() != NULL) { std::unique_lock<std::mutex> lock(pool.batch_mutex); JobBatch *jobBatch = job->GetJobBatch(); jobBatch->CompletedJob(job); if (jobBatch->GetNumJobs() == 0) { jobBatch->SetCompletedAllJobs(); } } } }
///===================================================== /// ///===================================================== bool JobManager::ProcessJob(ThreadType type){ Job* job; for (int priority = JobPriority::PriorityCount - 1; priority >= 0; --priority){ if (m_jobs[priority].Dequeue(&job)){ if (job->m_intendedThreadType != type && type != ThreadType::AnyType){ m_jobs[priority].Enqueue(job); return false; } job->Execute(); if (job->m_callback == nullptr) delete job; else m_finishedJobs.Enqueue(job); return true; } } return false; }
//----------------------------------------------------------------------------------------------- STATIC void JobManager::Update() { if( s_workerThreads.size() == 0 ) { Job* job = GetJobOfAnyTypeFromTodoList(); if( job ) { job->Execute(); ReportCompletedJob( job ); } } EnterCriticalSection( &s_cs ); while( s_jobsCompleted.size() > 0 ) { Job* completedJob = s_jobsCompleted.front(); completedJob->FireCallbackEvent(); delete completedJob; completedJob = nullptr; s_jobsCompleted.erase( s_jobsCompleted.begin() ); } LeaveCriticalSection( &s_cs ); }