ThreadLoop.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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();
  17. threadloop_node_t *GetAPC(); // returns a node for you to fill out
  18. void Schedule(threadloop_node_t *apc);
  19. void Step();
  20. void Step(unsigned int milliseconds);
  21. void Run();
  22. void Kill();
  23. private:
  24. void RefillCache();
  25. HANDLE procedure_notification;
  26. HANDLE kill_switch;
  27. mpscq_t procedure_queue;
  28. /* Memory cache to be able to run APCs without having the memory manager lock
  29. we'll allocate 100 at a time (#defined by PROCEDURE_CACHE_SEED)
  30. and allocate new ones only if the cache is empty (which unfortunately will lock)
  31. cache_bases holds the pointers we've allocated (to free on destruction of this object)
  32. and procedure_cache holds the individual pointers */
  33. static lifo_t procedure_cache;
  34. static lifo_t cache_bases;
  35. };