WorkerPool.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #pragma once
  2. // Thread pool for the audio thread to split a block's work (channels and
  3. // mixer tracks), like FL does with the mixer layers (docs/ANALISE_FL_STUDIO.md §2.2).
  4. // parallelFor neither allocates nor blocks on a mutex: workers take indices from an atomic counter; the calling
  5. // thread also works and spin-waits for the last index.
  6. //
  7. // Waking the workers (REA round 87): a worker that just finished spins for a short while (the next parallelFor of
  8. // the same block — the next mixer layer, the next chunk — comes microseconds later), then sleeps on an OS
  9. // semaphore (a Win32 semaphore on Windows, a futex-backed one elsewhere). std::counting_semaphore is NOT used:
  10. // the Windows libc++ build has no platform wait and falls back to Sleep() polling with growing intervals, so an
  11. // idle worker woke 1–8 ms late — later than a whole audio block — and the audio thread ended up doing all the
  12. // work alone.
  13. #include <atomic>
  14. #include <cstdint>
  15. #include <functional>
  16. #include <memory>
  17. #include <thread>
  18. #include <vector>
  19. namespace lynx
  20. {
  21. class WorkerPool
  22. {
  23. public:
  24. // `threads` workers besides the calling thread. `onThreadStart` runs on each worker
  25. // (e.g. raising the priority); may be empty.
  26. explicit WorkerPool (int threads, std::function<void()> onThreadStart = {});
  27. ~WorkerPool();
  28. WorkerPool (const WorkerPool&) = delete;
  29. WorkerPool& operator= (const WorkerPool&) = delete;
  30. int workers() const noexcept { return (int) threads.size(); }
  31. // Calls fn(i) for i in [0, count), in parallel. Returns when all have finished.
  32. template <typename Fn>
  33. void parallelFor (int count, Fn& fn) noexcept
  34. {
  35. run (count, &fn, [] (void* f, int i) { (*static_cast<Fn*> (f)) (i); });
  36. }
  37. // How long a worker keeps polling for new work after finishing before it sleeps (microseconds).
  38. static constexpr int spinMicros = 250;
  39. // Diagnostics: how many indices the workers (not the calling thread) ran, and how many times a worker
  40. // was woken from sleep. Relaxed counters.
  41. std::uint64_t indicesRunByWorkers() const noexcept { return byWorkers.load (std::memory_order_relaxed); }
  42. std::uint64_t wakeUps() const noexcept { return wokenUp.load (std::memory_order_relaxed); }
  43. private:
  44. using Call = void (*) (void*, int);
  45. void run (int count, void* context, Call call) noexcept;
  46. bool work (bool isWorker) noexcept;
  47. bool hasWork() const noexcept;
  48. void workerLoop (std::function<void()> onStart);
  49. struct Semaphore; // OS semaphore (WorkerPool.cpp)
  50. std::unique_ptr<Semaphore> wake;
  51. std::vector<std::thread> threads;
  52. std::atomic<bool> quit { false };
  53. std::atomic<int> sleeping { 0 }; // workers blocked (or about to block) on `wake`
  54. // current work (published before `ticket`). The ticket packs generation (bits 40+), count (bits 20…39) and the
  55. // next index (bits 0…19), so an index taken by a late worker always carries its own round's count: a worker
  56. // that stalls between taking an index and running it can never run an index of a later round (no ABA)
  57. void* context = nullptr;
  58. Call call = nullptr;
  59. static constexpr int indexBits = 20;
  60. std::atomic<std::uint64_t> ticket { 0 };
  61. std::uint64_t generation = 0; // caller thread only
  62. std::atomic<int> done { 0 };
  63. std::atomic<std::uint64_t> byWorkers { 0 }, wokenUp { 0 };
  64. };
  65. } // namespace lynx