threading.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2024 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "threading.h"
  24. #include "virtualStack.h"
  25. #include "../implementation/math/scalar.h"
  26. // Get settings from here.
  27. #include "../settings.h"
  28. #ifndef DISABLE_MULTI_THREADING
  29. // Requires -pthread for linking
  30. #include <thread>
  31. #include <mutex>
  32. #include <future>
  33. #endif
  34. namespace dsr {
  35. #ifndef DISABLE_MULTI_THREADING
  36. static std::mutex getTaskLock;
  37. #endif
  38. int getThreadCount() {
  39. #ifndef DISABLE_MULTI_THREADING
  40. return (int)std::thread::hardware_concurrency();
  41. #else
  42. return 1;
  43. #endif
  44. }
  45. void threadedWorkFromArray(std::function<void()>* jobs, int jobCount, int maxThreadCount) {
  46. #ifdef DISABLE_MULTI_THREADING
  47. // Reference implementation
  48. for (int i = 0; i < jobCount; i++) {
  49. jobs[i]();
  50. }
  51. #else
  52. if (jobCount <= 0) {
  53. return;
  54. } else if (jobCount == 1) {
  55. jobs[0]();
  56. } else {
  57. if (maxThreadCount <= 0) {
  58. // No limit.
  59. maxThreadCount = jobCount;
  60. }
  61. // When having more than one thread, one should be reserved for fast responses.
  62. // Otherwise one thread will keep the others waiting while struggling to manage interrupts with expensive context switches.
  63. int availableThreads = max(getThreadCount() - 1, 1);
  64. int workerCount = min(availableThreads, maxThreadCount, jobCount); // All used threads
  65. int helperCount = workerCount - 1; // Excluding the main thread
  66. // Multi-threaded work loop
  67. if (workerCount == 1) {
  68. // Run on the main thread if there is only one.
  69. for (int i = 0; i < jobCount; i++) {
  70. jobs[i]();
  71. }
  72. } else {
  73. // A shared counter protected by getTaskLock.
  74. int nextJobIndex = 0;
  75. VirtualStackAllocation<std::function<void()>> workers(workerCount);
  76. VirtualStackAllocation<std::future<void>> helpers(helperCount);
  77. for (int w = 0; w < workerCount; w++) {
  78. workers[w] = [&nextJobIndex, jobs, jobCount]() {
  79. while (true) {
  80. getTaskLock.lock();
  81. int taskIndex = nextJobIndex;
  82. nextJobIndex++;
  83. getTaskLock.unlock();
  84. if (taskIndex < jobCount) {
  85. jobs[taskIndex]();
  86. } else {
  87. break;
  88. }
  89. }
  90. };
  91. }
  92. // Start working in the helper threads
  93. for (int h = 0; h < helperCount; h++) {
  94. helpers[h] = std::async(std::launch::async, workers[h]);
  95. }
  96. // Perform the same work on the main thread
  97. workers[workerCount - 1]();
  98. // Wait for all helpers to complete their work once all tasks have been handed out
  99. for (int h = 0; h < helperCount; h++) {
  100. if (helpers[h].valid()) {
  101. helpers[h].wait();
  102. }
  103. }
  104. }
  105. }
  106. #endif
  107. }
  108. void threadedWorkFromArray(SafePointer<std::function<void()>> jobs, int jobCount, int maxThreadCount) {
  109. threadedWorkFromArray(jobs.getUnsafe(), jobCount, maxThreadCount);
  110. }
  111. void threadedWorkFromList(List<std::function<void()>> jobs, int maxThreadCount) {
  112. threadedWorkFromArray(&jobs[0], jobs.length(), maxThreadCount);
  113. jobs.clear();
  114. }
  115. void threadedSplit(int startIndex, int stopIndex, std::function<void(int startIndex, int stopIndex)> task, int minimumJobSize, int jobsPerThread) {
  116. #ifndef DISABLE_MULTI_THREADING
  117. int totalCount = stopIndex - startIndex;
  118. int maxJobs = totalCount / minimumJobSize;
  119. int jobCount = getThreadCount() * jobsPerThread;
  120. if (jobCount > maxJobs) { jobCount = maxJobs; }
  121. if (jobCount < 1) { jobCount = 1; }
  122. #else
  123. int jobCount = 1;
  124. #endif
  125. if (jobCount == 1) {
  126. // Too little work for multi-threading
  127. task(startIndex, stopIndex);
  128. } else {
  129. // Use multiple threads
  130. VirtualStackAllocation<std::function<void()>> jobs(jobCount);
  131. int givenRow = startIndex;
  132. for (int s = 0; s < jobCount; s++) {
  133. int remainingJobs = jobCount - s;
  134. int remainingRows = stopIndex - givenRow;
  135. int y1 = givenRow; // Inclusive
  136. int taskSize = remainingRows / remainingJobs;
  137. givenRow = givenRow + taskSize;
  138. int y2 = givenRow; // Exclusive
  139. jobs[s] = [task, y1, y2]() {
  140. task(y1, y2);
  141. };
  142. }
  143. threadedWorkFromArray(jobs, jobCount);
  144. }
  145. }
  146. void threadedSplit_disabled(int startIndex, int stopIndex, std::function<void(int startIndex, int stopIndex)> task) {
  147. task(startIndex, stopIndex);
  148. }
  149. void threadedSplit(const IRect& bound, std::function<void(const IRect& bound)> task, int minimumRowsPerJob, int jobsPerThread) {
  150. #ifndef DISABLE_MULTI_THREADING
  151. int maxJobs = bound.height() / minimumRowsPerJob;
  152. int jobCount = getThreadCount() * jobsPerThread;
  153. if (jobCount > maxJobs) { jobCount = maxJobs; }
  154. if (jobCount < 1) { jobCount = 1; }
  155. #else
  156. int jobCount = 1;
  157. #endif
  158. if (jobCount == 1) {
  159. // Too little work for multi-threading
  160. task(bound);
  161. } else {
  162. // Use multiple threads
  163. VirtualStackAllocation<std::function<void()>> jobs(jobCount);
  164. int givenRow = bound.top();
  165. for (int s = 0; s < jobCount; s++) {
  166. int remainingJobs = jobCount - s;
  167. int remainingRows = bound.bottom() - givenRow;
  168. int y1 = givenRow;
  169. int taskSize = remainingRows / remainingJobs;
  170. givenRow = givenRow + taskSize;
  171. IRect subBound = IRect(bound.left(), y1, bound.width(), taskSize);
  172. jobs[s] = [task, subBound]() {
  173. task(subBound);
  174. };
  175. }
  176. threadedWorkFromArray(jobs, jobCount);
  177. }
  178. }
  179. void threadedSplit_disabled(const IRect& bound, std::function<void(const IRect& bound)> task) {
  180. task(bound);
  181. }
  182. }