| 147 | } |
| 148 | |
| 149 | int32 JobSystemThread::Run() |
| 150 | { |
| 151 | // Pin thread to the physical core |
| 152 | Platform::SetThreadAffinityMask(1ull << Index); |
| 153 | |
| 154 | bool attachCSharpThread = true; |
| 155 | MONO_THREAD_INFO_TYPE* monoThreadInfo = nullptr; |
| 156 | while (Platform::AtomicRead(&ExitFlag) == 0) |
| 157 | { |
| 158 | // Try to get a job |
| 159 | int32 jobIndex; |
| 160 | JobContext* jobContext = nullptr; |
| 161 | { |
| 162 | int64 jobOffset = 0; |
| 163 | RETRY: |
| 164 | int64 jobStartLabel = Platform::AtomicRead(&JobStartLabel) + jobOffset; |
| 165 | int64 jobEndLabel = Platform::AtomicRead(&JobEndLabel); |
| 166 | if (jobStartLabel <= jobEndLabel && jobEndLabel > 0) |
| 167 | { |
| 168 | jobContext = &JobContexts[GET_CONTEXT_INDEX(jobStartLabel)]; |
| 169 | if (Platform::AtomicRead(&jobContext->DependenciesLeft) > 0) |
| 170 | { |
| 171 | // This job still waits for dependency so skip it for now and try the next one |
| 172 | jobOffset++; |
| 173 | jobContext = nullptr; |
| 174 | goto RETRY; |
| 175 | } |
| 176 | |
| 177 | // Move forward with index for a job |
| 178 | jobIndex = (int32)(Platform::InterlockedIncrement(&jobContext->JobIndex) - 1); |
| 179 | if (jobIndex < jobContext->JobsCount) |
| 180 | { |
| 181 | // Index is valid |
| 182 | } |
| 183 | else if (jobStartLabel < jobEndLabel && jobOffset == 0) |
| 184 | { |
| 185 | // No more jobs inside this context, move to the next one |
| 186 | Platform::InterlockedCompareExchange(&JobStartLabel, jobStartLabel + 1, jobStartLabel); |
| 187 | jobContext = nullptr; |
| 188 | goto RETRY; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | // No more jobs |
| 193 | jobContext = nullptr; |
| 194 | if (jobStartLabel < jobEndLabel) |
| 195 | { |
| 196 | // Try with a different one before going to sleep |
| 197 | jobOffset++; |
| 198 | goto RETRY; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (jobContext) |
| 205 | { |
| 206 | #if USE_CSHARP |
nothing calls this directly
no test coverage detected