1
0

MessageLoop.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include "foundation/types.h"
  3. #include "nu/lfmpscq.h"
  4. #include "nu/LockFreeLIFO.h"
  5. #include <windows.h>
  6. namespace nu
  7. {
  8. /* you can inherit from message_node_t (or combine inside a struct)
  9. but make sure that your message isn't > 64 bytes */
  10. struct message_node_t : public queue_node_t
  11. {
  12. uint32_t message;
  13. };
  14. class MessageLoop
  15. {
  16. public:
  17. MessageLoop();
  18. ~MessageLoop();
  19. /* API for Message senders */
  20. message_node_t *AllocateMessage(); // returns a message for you to fill out
  21. void PostMessage(message_node_t *message);
  22. /* API for Message receivers */
  23. void FreeMessage(message_node_t *message);
  24. message_node_t *GetMessage(); // waits forever
  25. message_node_t *PeekMessage();
  26. message_node_t *PeekMessage(unsigned int milliseconds);
  27. private:
  28. void RefillCache();
  29. HANDLE message_notification;
  30. mpscq_t message_queue;
  31. /* Memory cache to be able to run APCs without having the memory manager lock
  32. we'll allocate 100 at a time (#defined by MESSAGE_CACHE_SEED)
  33. and allocate new ones only if the cache is empty (which unfortunately will lock)
  34. cache_bases holds the pointers we've allocated (to free on destruction of this object)
  35. and message_cache holds the individual pointers */
  36. static lifo_t message_cache;
  37. static lifo_t cache_bases;
  38. };
  39. }