1
0

ThreadLoop.h 973 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #pragma once
  2. #include "nu/lfmpscq.h"
  3. #include "nu/LockFreeLIFO.h"
  4. #include <windows.h>
  5. struct threadloop_node_t : public queue_node_t
  6. {
  7. void (*func)(void *param1, void *param2, double real_value);
  8. void *param1;
  9. void *param2;
  10. double real_value;
  11. };
  12. class ThreadLoop
  13. {
  14. public:
  15. ThreadLoop();
  16. threadloop_node_t *GetAPC(); // returns a node for you to fill out
  17. void Schedule(threadloop_node_t *apc);
  18. void Run();
  19. void Kill();
  20. private:
  21. void RefillCache();
  22. HANDLE procedure_notification;
  23. HANDLE kill_switch;
  24. mpscq_t procedure_queue;
  25. /* Memory cache to be able to run APCs without having the memory manager lock
  26. we'll allocate 100 at a time (#defined by PROCEDURE_CACHE_SEED)
  27. and allocate new ones only if the cache is empty (which unfortunately will lock)
  28. cache_bases holds the pointers we've allocated (to free on destruction of this object)
  29. and procedure_cache holds the individual pointers */
  30. static lifo_t procedure_cache;
  31. static lifo_t cache_bases;
  32. };