threading.h 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef DFPSR_THREADING
  24. #define DFPSR_THREADING
  25. #include "../../DFPSR/collection/List.h"
  26. #include "../../DFPSR/math/IRect.h"
  27. #include <functional>
  28. namespace dsr {
  29. // Get the number of threads available.
  30. int getThreadCount();
  31. // Calls the same job function with indices 0 to jobIndex - 1.
  32. // This removes the need for capturing the same data over and over again when each task is identical with a different index.
  33. void threadedWorkByIndex(std::function<void(void *context, int jobIndex)> job, void *context, int jobCount, int maxThreadCount = 0);
  34. // Executes every function in the array of jobs from jobs[0] to jobs[jobCount - 1].
  35. // The maxThreadCount argument is the maximum number of threads to use when enough threads are available.
  36. // Letting maxThreadCount be 0 removes the limit and uses as many threads as possible, limited only by getThreadCount() - 1 and jobCount.
  37. // Letting maxThreadCount be 1 forces single-threaded execution on the calling thread.
  38. // Useful when each job to execute is different.
  39. void threadedWorkFromArray(SafePointer<std::function<void()>> jobs, int jobCount, int maxThreadCount = 0);
  40. void threadedWorkFromArray(std::function<void()>* jobs, int jobCount, int maxThreadCount = 0);
  41. // Executes every function in the list of jobs.
  42. // Also clears the list when done.
  43. void threadedWorkFromList(List<std::function<void()>> jobs, int maxThreadCount = 0);
  44. // Calling the given function with sub-sets of the interval using multiple threads in parallel.
  45. // Useful when you have lots of tiny jobs that can be grouped together into larger jobs.
  46. // Otherwise the time to start a thread may exceed the cost of the computation.
  47. // startIndex is inclusive but stopIndex is exclusive.
  48. // X is within the interval iff startIndex <= X < stopIndex.
  49. // Warning!
  50. // * Only write to non-overlapping memory regions.
  51. // This may require aligning the data or using padding depending on how cache works on the target platform.
  52. // The longer the distance is, the safer it is against race conditions causing weird results.
  53. // You may however read from write-protected shared input in any way you want.
  54. // Because data that doesn't change cannot have race conditions.
  55. // * Do not use for manipulation of pointers, stack memory from the calling thread or anything where corrupted output may lead to a crash.
  56. // Drawing pixel values is okay, because a race condition would only be some noisy pixels that can be spotted and fixed.
  57. // Race conditions cannot be tested nor proven away, so assume that they will happen and do your best to avoid them.
  58. void threadedSplit(int startIndex, int stopIndex, std::function<void(int startIndex, int stopIndex)> task, int minimumJobSize = 128, int jobsPerThread = 2);
  59. // Use as a place-holder if you want to disable multi-threading but easily turn it on and off for comparing performance
  60. void threadedSplit_disabled(int startIndex, int stopIndex, std::function<void(int startIndex, int stopIndex)> task);
  61. // A more convenient version for images looping over a rectangular bound of pixels.
  62. // The same left and right sides are given to each sub-bound to make memory alignment easy.
  63. // The top and bottoms are subdivided so that memory access is simple for cache prediction.
  64. void threadedSplit(const IRect& bound, std::function<void(const IRect& bound)> task, int minimumRowsPerJob = 128, int jobsPerThread = 2);
  65. // Use as a place-holder if you want to disable multi-threading but easily turn it on and off for comparing performance
  66. void threadedSplit_disabled(const IRect& bound, std::function<void(const IRect& bound)> task);
  67. }
  68. #endif