diff --git a/Source/Engine/Threading/JobSystem.cpp b/Source/Engine/Threading/JobSystem.cpp index de3d1c301..c16a1596d 100644 --- a/Source/Engine/Threading/JobSystem.cpp +++ b/Source/Engine/Threading/JobSystem.cpp @@ -212,6 +212,23 @@ int32 JobSystemThread::Run() #endif +void JobSystem::Execute(const Function& job, int32 jobCount) +{ + // TODO: disable async if called on job thread? or maybe Wait should handle waiting in job thread to do the processing? + if (jobCount > 1) + { + // Async + const int64 jobWaitHandle = Dispatch(job, jobCount); + Wait(jobWaitHandle); + } + else if (jobCount > 0) + { + // Sync + for (int32 i = 0; i < jobCount; i++) + job(i); + } +} + int64 JobSystem::Dispatch(const Function& job, int32 jobCount) { PROFILE_CPU(); diff --git a/Source/Engine/Threading/JobSystem.h b/Source/Engine/Threading/JobSystem.h index 2b6545156..e009e2f29 100644 --- a/Source/Engine/Threading/JobSystem.h +++ b/Source/Engine/Threading/JobSystem.h @@ -9,7 +9,14 @@ /// API_CLASS(Static) class FLAXENGINE_API JobSystem { -DECLARE_SCRIPTING_TYPE_MINIMAL(JobSystem); + DECLARE_SCRIPTING_TYPE_MINIMAL(JobSystem); + + /// + /// Executes the job (utility to call dispatch and wait for the end). + /// + /// The job. Argument is an index of the job execution. + /// The job executions count. + API_FUNCTION() static void Execute(const Function& job, int32 jobCount = 1); /// /// Dispatches the job for the execution.