threading.h 4.2 KB

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