JobSystemThreadPool.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Core/JobSystemThreadPool.h>
  5. #include <Jolt/Core/Profiler.h>
  6. #include <Jolt/Core/FPException.h>
  7. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  8. #include <algorithm>
  9. JPH_SUPPRESS_WARNINGS_STD_END
  10. #ifdef JPH_PLATFORM_WINDOWS
  11. JPH_SUPPRESS_WARNING_PUSH
  12. JPH_MSVC_SUPPRESS_WARNING(5039) // winbase.h(13179): warning C5039: 'TpSetCallbackCleanupGroup': pointer or reference to potentially throwing function passed to 'extern "C"' function under -EHc. Undefined behavior may occur if this function throws an exception.
  13. #define WIN32_LEAN_AND_MEAN
  14. #include <Windows.h>
  15. JPH_SUPPRESS_WARNING_POP
  16. #endif
  17. JPH_NAMESPACE_BEGIN
  18. JobSystemThreadPool::Semaphore::Semaphore()
  19. {
  20. #ifdef JPH_PLATFORM_WINDOWS
  21. mSemaphore = CreateSemaphore(nullptr, 0, INT_MAX, nullptr);
  22. #endif
  23. }
  24. JobSystemThreadPool::Semaphore::~Semaphore()
  25. {
  26. #ifdef JPH_PLATFORM_WINDOWS
  27. CloseHandle(mSemaphore);
  28. #endif
  29. }
  30. void JobSystemThreadPool::Semaphore::Release(uint inNumber)
  31. {
  32. JPH_ASSERT(inNumber > 0);
  33. #ifdef JPH_PLATFORM_WINDOWS
  34. int old_value = mCount.fetch_add(inNumber);
  35. if (old_value < 0)
  36. {
  37. int new_value = old_value + (int)inNumber;
  38. int num_to_release = min(new_value, 0) - old_value;
  39. ::ReleaseSemaphore(mSemaphore, num_to_release, nullptr);
  40. }
  41. #else
  42. lock_guard lock(mLock);
  43. mCount += (int)inNumber;
  44. if (inNumber > 1)
  45. mWaitVariable.notify_all();
  46. else
  47. mWaitVariable.notify_one();
  48. #endif
  49. }
  50. void JobSystemThreadPool::Semaphore::Acquire(uint inNumber)
  51. {
  52. JPH_ASSERT(inNumber > 0);
  53. #ifdef JPH_PLATFORM_WINDOWS
  54. int old_value = mCount.fetch_sub(inNumber);
  55. int new_value = old_value - (int)inNumber;
  56. if (new_value < 0)
  57. {
  58. int num_to_acquire = min(old_value, 0) - new_value;
  59. for (int i = 0; i < num_to_acquire; ++i)
  60. WaitForSingleObject(mSemaphore, INFINITE);
  61. }
  62. #else
  63. unique_lock lock(mLock);
  64. mCount -= (int)inNumber;
  65. mWaitVariable.wait(lock, [this]() { return mCount >= 0; });
  66. #endif
  67. }
  68. JobSystemThreadPool::BarrierImpl::BarrierImpl()
  69. {
  70. for (atomic<Job *> &j : mJobs)
  71. j = nullptr;
  72. }
  73. JobSystemThreadPool::BarrierImpl::~BarrierImpl()
  74. {
  75. JPH_ASSERT(IsEmpty());
  76. }
  77. void JobSystemThreadPool::BarrierImpl::AddJob(const JobHandle &inJob)
  78. {
  79. JPH_PROFILE_FUNCTION();
  80. bool release_semaphore = false;
  81. // Set the barrier on the job, this returns true if the barrier was successfully set (otherwise the job is already done and we don't need to add it to our list)
  82. Job *job = inJob.GetPtr();
  83. if (job->SetBarrier(this))
  84. {
  85. // If the job can be executed we want to release the semaphore an extra time to allow the waiting thread to start executing it
  86. mNumToAcquire++;
  87. if (job->CanBeExecuted())
  88. {
  89. release_semaphore = true;
  90. mNumToAcquire++;
  91. }
  92. // Add the job to our job list
  93. job->AddRef();
  94. uint write_index = mJobWriteIndex++;
  95. while (write_index - mJobReadIndex >= cMaxJobs)
  96. {
  97. JPH_ASSERT(false, "Barrier full, stalling!");
  98. this_thread::sleep_for(100us);
  99. }
  100. mJobs[write_index & (cMaxJobs - 1)] = job;
  101. }
  102. // Notify waiting thread that a new executable job is available
  103. if (release_semaphore)
  104. mSemaphore.Release();
  105. }
  106. void JobSystemThreadPool::BarrierImpl::AddJobs(const JobHandle *inHandles, uint inNumHandles)
  107. {
  108. JPH_PROFILE_FUNCTION();
  109. bool release_semaphore = false;
  110. for (const JobHandle *handle = inHandles, *handles_end = inHandles + inNumHandles; handle < handles_end; ++handle)
  111. {
  112. // Set the barrier on the job, this returns true if the barrier was successfully set (otherwise the job is already done and we don't need to add it to our list)
  113. Job *job = handle->GetPtr();
  114. if (job->SetBarrier(this))
  115. {
  116. // If the job can be executed we want to release the semaphore an extra time to allow the waiting thread to start executing it
  117. mNumToAcquire++;
  118. if (!release_semaphore && job->CanBeExecuted())
  119. {
  120. release_semaphore = true;
  121. mNumToAcquire++;
  122. }
  123. // Add the job to our job list
  124. job->AddRef();
  125. uint write_index = mJobWriteIndex++;
  126. while (write_index - mJobReadIndex >= cMaxJobs)
  127. {
  128. JPH_ASSERT(false, "Barrier full, stalling!");
  129. this_thread::sleep_for(100us);
  130. }
  131. mJobs[write_index & (cMaxJobs - 1)] = job;
  132. }
  133. }
  134. // Notify waiting thread that a new executable job is available
  135. if (release_semaphore)
  136. mSemaphore.Release();
  137. }
  138. void JobSystemThreadPool::BarrierImpl::OnJobFinished(Job *inJob)
  139. {
  140. JPH_PROFILE_FUNCTION();
  141. mSemaphore.Release();
  142. }
  143. void JobSystemThreadPool::BarrierImpl::Wait()
  144. {
  145. while (mNumToAcquire > 0)
  146. {
  147. {
  148. JPH_PROFILE("Execute Jobs");
  149. // Go through all jobs
  150. bool has_executed;
  151. do
  152. {
  153. has_executed = false;
  154. // Loop through the jobs and erase jobs from the beginning of the list that are done
  155. while (mJobReadIndex < mJobWriteIndex)
  156. {
  157. atomic<Job *> &job = mJobs[mJobReadIndex & (cMaxJobs - 1)];
  158. Job *job_ptr = job.load();
  159. if (job_ptr == nullptr || !job_ptr->IsDone())
  160. break;
  161. // Job is finished, release it
  162. job_ptr->Release();
  163. job = nullptr;
  164. ++mJobReadIndex;
  165. }
  166. // Loop through the jobs and execute the first executable job
  167. for (uint index = mJobReadIndex; index < mJobWriteIndex; ++index)
  168. {
  169. const atomic<Job *> &job = mJobs[index & (cMaxJobs - 1)];
  170. Job *job_ptr = job.load();
  171. if (job_ptr != nullptr && job_ptr->CanBeExecuted())
  172. {
  173. // This will only execute the job if it has not already executed
  174. job_ptr->Execute();
  175. has_executed = true;
  176. break;
  177. }
  178. }
  179. } while (has_executed);
  180. }
  181. // Wait for another thread to wake us when either there is more work to do or when all jobs have completed
  182. int num_to_acquire = max(1, mSemaphore.GetValue()); // When there have been multiple releases, we acquire them all at the same time to avoid needlessly spinning on executing jobs
  183. mSemaphore.Acquire(num_to_acquire);
  184. mNumToAcquire -= num_to_acquire;
  185. }
  186. // All jobs should be done now, release them
  187. while (mJobReadIndex < mJobWriteIndex)
  188. {
  189. atomic<Job *> &job = mJobs[mJobReadIndex & (cMaxJobs - 1)];
  190. Job *job_ptr = job.load();
  191. JPH_ASSERT(job_ptr != nullptr && job_ptr->IsDone());
  192. job_ptr->Release();
  193. job = nullptr;
  194. ++mJobReadIndex;
  195. }
  196. }
  197. JobSystemThreadPool::JobSystemThreadPool(uint inMaxJobs, uint inMaxBarriers, int inNumThreads) :
  198. mMaxBarriers(inMaxBarriers),
  199. mBarriers(new BarrierImpl [inMaxBarriers]) // Init freelist of barriers
  200. {
  201. // Init freelist of jobs
  202. mJobs.Init(inMaxJobs, inMaxJobs);
  203. // Init queue
  204. for (atomic<Job *> &j : mQueue)
  205. j = nullptr;
  206. // Start the worker threads
  207. StartThreads(inNumThreads);
  208. }
  209. void JobSystemThreadPool::StartThreads(int inNumThreads)
  210. {
  211. // Auto detect number of threads
  212. if (inNumThreads < 0)
  213. inNumThreads = thread::hardware_concurrency() - 1;
  214. // If no threads are requested we're done
  215. if (inNumThreads == 0)
  216. return;
  217. // Don't quit the threads
  218. mQuit = false;
  219. // Allocate heads
  220. mHeads = new atomic<uint> [inNumThreads];
  221. for (int i = 0; i < inNumThreads; ++i)
  222. mHeads[i] = 0;
  223. // Start running threads
  224. JPH_ASSERT(mThreads.empty());
  225. mThreads.reserve(inNumThreads);
  226. for (int i = 0; i < inNumThreads; ++i)
  227. {
  228. // Name the thread
  229. stringstream namestream;
  230. namestream << "Worker ";
  231. namestream << (i + 1);
  232. string name = namestream.str();
  233. // Create thread
  234. mThreads.emplace_back([this, name, i] { ThreadMain(name, i); });
  235. }
  236. }
  237. JobSystemThreadPool::~JobSystemThreadPool()
  238. {
  239. // Stop all worker threads
  240. StopThreads();
  241. // Ensure that none of the barriers are used
  242. #ifdef JPH_ENABLE_ASSERTS
  243. for (const BarrierImpl *b = mBarriers, *b_end = mBarriers + mMaxBarriers; b < b_end; ++b)
  244. JPH_ASSERT(!b->mInUse);
  245. #endif // JPH_ENABLE_ASSERTS
  246. delete [] mBarriers;
  247. }
  248. void JobSystemThreadPool::StopThreads()
  249. {
  250. if (mThreads.empty())
  251. return;
  252. // Signal threads that we want to stop and wake them up
  253. mQuit = true;
  254. mSemaphore.Release((uint)mThreads.size());
  255. // Wait for all threads to finish
  256. for (thread &t : mThreads)
  257. if (t.joinable())
  258. t.join();
  259. // Delete all threads
  260. mThreads.clear();
  261. // Ensure that there are no lingering jobs in the queue
  262. for (uint head = 0; head != mTail; ++head)
  263. {
  264. // Fetch job
  265. Job *job_ptr = mQueue[head & (cQueueLength - 1)].exchange(nullptr);
  266. if (job_ptr != nullptr)
  267. {
  268. // And execute it
  269. job_ptr->Execute();
  270. job_ptr->Release();
  271. }
  272. }
  273. // Destroy heads and reset tail
  274. delete [] mHeads;
  275. mHeads = nullptr;
  276. mTail = 0;
  277. }
  278. JobHandle JobSystemThreadPool::CreateJob(const char *inJobName, ColorArg inColor, const JobFunction &inJobFunction, uint32 inNumDependencies)
  279. {
  280. JPH_PROFILE_FUNCTION();
  281. // Loop until we can get a job from the free list
  282. uint32 index;
  283. for (;;)
  284. {
  285. index = mJobs.ConstructObject(inJobName, inColor, this, inJobFunction, inNumDependencies);
  286. if (index != AvailableJobs::cInvalidObjectIndex)
  287. break;
  288. JPH_ASSERT(false, "No jobs available!");
  289. this_thread::sleep_for(100us);
  290. }
  291. Job *job = &mJobs.Get(index);
  292. // Construct handle to keep a reference, the job is queued below and may immediately complete
  293. JobHandle handle(job);
  294. // If there are no dependencies, queue the job now
  295. if (inNumDependencies == 0)
  296. QueueJob(job);
  297. // Return the handle
  298. return handle;
  299. }
  300. void JobSystemThreadPool::FreeJob(Job *inJob)
  301. {
  302. mJobs.DestructObject(inJob);
  303. }
  304. JobSystem::Barrier *JobSystemThreadPool::CreateBarrier()
  305. {
  306. JPH_PROFILE_FUNCTION();
  307. // Find the first unused barrier
  308. for (uint32 index = 0; index < mMaxBarriers; ++index)
  309. {
  310. bool expected = false;
  311. if (mBarriers[index].mInUse.compare_exchange_strong(expected, true))
  312. return &mBarriers[index];
  313. }
  314. return nullptr;
  315. }
  316. void JobSystemThreadPool::DestroyBarrier(Barrier *inBarrier)
  317. {
  318. JPH_PROFILE_FUNCTION();
  319. // Check that no jobs are in the barrier
  320. JPH_ASSERT(static_cast<BarrierImpl *>(inBarrier)->IsEmpty());
  321. // Flag the barrier as unused
  322. bool expected = true;
  323. static_cast<BarrierImpl *>(inBarrier)->mInUse.compare_exchange_strong(expected, false);
  324. JPH_ASSERT(expected);
  325. }
  326. void JobSystemThreadPool::WaitForJobs(Barrier *inBarrier)
  327. {
  328. JPH_PROFILE_FUNCTION();
  329. // Let our barrier implementation wait for the jobs
  330. static_cast<BarrierImpl *>(inBarrier)->Wait();
  331. }
  332. uint JobSystemThreadPool::GetHead() const
  333. {
  334. // Find the minimal value across all threads
  335. uint head = mTail;
  336. for (size_t i = 0; i < mThreads.size(); ++i)
  337. head = min(head, mHeads[i].load());
  338. return head;
  339. }
  340. void JobSystemThreadPool::QueueJobInternal(Job *inJob)
  341. {
  342. // Add reference to job because we're adding the job to the queue
  343. inJob->AddRef();
  344. // Need to read head first because otherwise the tail can already have passed the head
  345. // We read the head outside of the loop since it involves iterating over all threads and we only need to update
  346. // it if there's not enough space in the queue.
  347. uint head = GetHead();
  348. for (;;)
  349. {
  350. // Check if there's space in the queue
  351. uint old_value = mTail;
  352. if (old_value - head >= cQueueLength)
  353. {
  354. // We calculated the head outside of the loop, update head (and we also need to update tail to prevent it from passing head)
  355. head = GetHead();
  356. old_value = mTail;
  357. // Second check if there's space in the queue
  358. if (old_value - head >= cQueueLength)
  359. {
  360. // Wake up all threads in order to ensure that they can clear any nullptrs they may not have processed yet
  361. mSemaphore.Release((uint)mThreads.size());
  362. // Sleep a little (we have to wait for other threads to update their head pointer in order for us to be able to continue)
  363. this_thread::sleep_for(100us);
  364. continue;
  365. }
  366. }
  367. // Write the job pointer if the slot is empty
  368. Job *expected_job = nullptr;
  369. bool success = mQueue[old_value & (cQueueLength - 1)].compare_exchange_strong(expected_job, inJob);
  370. // Regardless of who wrote the slot, we will update the tail (if the successful thread got scheduled out
  371. // after writing the pointer we still want to be able to continue)
  372. mTail.compare_exchange_strong(old_value, old_value + 1);
  373. // If we successfully added our job we're done
  374. if (success)
  375. break;
  376. }
  377. }
  378. void JobSystemThreadPool::QueueJob(Job *inJob)
  379. {
  380. JPH_PROFILE_FUNCTION();
  381. // If we have no worker threads, we can't queue the job either. We assume in this case that the job will be added to a barrier and that the barrier will execute the job when it's Wait() function is called.
  382. if (mThreads.empty())
  383. return;
  384. // Queue the job
  385. QueueJobInternal(inJob);
  386. // Wake up thread
  387. mSemaphore.Release();
  388. }
  389. void JobSystemThreadPool::QueueJobs(Job **inJobs, uint inNumJobs)
  390. {
  391. JPH_PROFILE_FUNCTION();
  392. JPH_ASSERT(inNumJobs > 0);
  393. // If we have no worker threads, we can't queue the job either. We assume in this case that the job will be added to a barrier and that the barrier will execute the job when it's Wait() function is called.
  394. if (mThreads.empty())
  395. return;
  396. // Queue all jobs
  397. for (Job **job = inJobs, **job_end = inJobs + inNumJobs; job < job_end; ++job)
  398. QueueJobInternal(*job);
  399. // Wake up threads
  400. mSemaphore.Release(min(inNumJobs, (uint)mThreads.size()));
  401. }
  402. #ifdef JPH_PLATFORM_WINDOWS
  403. // Sets the current thread name in MSVC debugger
  404. static void SetThreadName(const char *inName)
  405. {
  406. #pragma pack(push, 8)
  407. struct THREADNAME_INFO
  408. {
  409. DWORD dwType; // Must be 0x1000.
  410. LPCSTR szName; // Pointer to name (in user addr space).
  411. DWORD dwThreadID; // Thread ID (-1=caller thread).
  412. DWORD dwFlags; // Reserved for future use, must be zero.
  413. };
  414. #pragma pack(pop)
  415. THREADNAME_INFO info;
  416. info.dwType = 0x1000;
  417. info.szName = inName;
  418. info.dwThreadID = (DWORD)-1;
  419. info.dwFlags = 0;
  420. __try
  421. {
  422. RaiseException(0x406D1388, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR *)&info);
  423. }
  424. __except(EXCEPTION_EXECUTE_HANDLER)
  425. {
  426. }
  427. }
  428. #endif
  429. void JobSystemThreadPool::ThreadMain([[maybe_unused]] const string &inName, int inThreadIndex)
  430. {
  431. #ifdef JPH_PLATFORM_WINDOWS
  432. SetThreadName(inName.c_str());
  433. #endif
  434. // Enable floating point exceptions
  435. FPExceptionsEnable enable_exceptions;
  436. JPH_UNUSED(enable_exceptions);
  437. JPH_PROFILE_THREAD_START(inName);
  438. atomic<uint> &head = mHeads[inThreadIndex];
  439. while (!mQuit)
  440. {
  441. // Wait for jobs
  442. mSemaphore.Acquire();
  443. {
  444. JPH_PROFILE("Executing Jobs");
  445. // Loop over the queue
  446. while (head != mTail)
  447. {
  448. // Exchange any job pointer we find with a nullptr
  449. atomic<Job *> &job = mQueue[head & (cQueueLength - 1)];
  450. if (job.load() != nullptr)
  451. {
  452. Job *job_ptr = job.exchange(nullptr);
  453. if (job_ptr != nullptr)
  454. {
  455. // And execute it
  456. job_ptr->Execute();
  457. job_ptr->Release();
  458. }
  459. }
  460. head++;
  461. }
  462. }
  463. }
  464. JPH_PROFILE_THREAD_END();
  465. }
  466. JPH_NAMESPACE_END