1
0

ThreadLoop.h 1001 B

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