Sorry for the confusing title, I couldn’t find a short title to explain my case.
so, I have a system running in the InitializationSystemGroup that starts several heavy jobs that will be awaited in the PresentationSystemGroup.
the plan is to create multiple entities, each one will contain a component with data and an IJobHandle to access that data when the job is done.
some jobs take longer than others due to their input size.
I would like to start mainthread data processing every time one of the Jobhandles finishes instead of wasting time waiting for random job when others have already finished.
I can think of a very hacky solution to this (using SharedStatic + atomic variables and/or checking JobHandle.IsCompleted property in a loop), or a very complex solution using events that isn’t worth the effort unless you have dozens of such jobs. But it would be nice to have some way to provide a main thread message pump (eg wpf dispatcher) to do main thread cleanups at end of frame.
So what you do is have a system that runs after all your job-scheduling systems run. It loops through all the JobHandles of all jobs and checks the IsCompleted flag on each one. If one of them is completed, it calls Complete and then dispatches the main thread work. If none are complete, it completes one and dispatches the main thread work. It then repeats this process until all jobs are completed.
Not really, since we can’t schedule main thread data, you either check the handles built in system groups (I think there are about 6, you need one system per sync point whose only reason to exist is checking the handles) or once every frame.
I wouldn’t be actually be storing the data in entities unless I actually need the data to be in entities at the end. I would just use static NativeArrays to share data between jobs and the main thread.
If you don’t like using static stuff, just get a reference to all relevant systems in the System OnCreate, which is a very verbose way to accomplish the same thing.
I will be forever a proponent of main thread scheduling, the same way we can toss ECS safety out of the window with all kinds of attributes, we should be able to schedule main thread work regardless of what kind of consequences it might have.