threading.h 3.7 KB

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