示例#1
0
//---------------------------------------
void JobManager::OnUpdate()
{
	// If there are no workers to do jobs, do them ourself
	if ( mMaxNumberOfWorkers == 0 )
	{
		Job* job = NULL;

		// Try to distribute work across all types evenly
		static int nextType = 0;
		++nextType;
		if ( nextType >= Job::JOB_TYPE_COUNT ) nextType = 0;

		if ( !mPendingJobQueue[ nextType ].empty() )
		{
			job = mPendingJobQueue[ nextType ].back();
			mPendingJobQueue[ nextType ].pop_back();
		}
		// There was not job of the next 'even' type, look for another job of any type
		else
		{
			for ( int i = 0; i < Job::JOB_TYPE_COUNT; ++i )
			{
				if ( !mPendingJobQueue[ i ].empty() )
				{
					job = mPendingJobQueue[ i ].back();
					mPendingJobQueue[ i ].pop_back();
					break;
				}
			}
		}

		// Execute job if there is one
		if ( job )
		{
			job->OnExecute();
			SubmitCompletedJob( job );
		}
	}

	// Fire callbacks for completed jobs
	std::list< Job* > completedJobs;

	BeginCriticalSection( mJobCompleteMutex );
	
	completedJobs = mCompletedJobList;
	mCompletedJobList.clear();
	
	EndCriticalSection();

	for ( auto jobItr = completedJobs.begin(); jobItr != completedJobs.end(); ++ jobItr )
	{
		(*jobItr)->OnCompletetion();
		delete *jobItr;
	}
}