EventQueue.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <cstring>
  25. #include "OsTypes.h"
  26. #include "AtomicInt.h"
  27. #define QUEUE_SIZE 1024 * 4
  28. namespace crown
  29. {
  30. struct EventQueue
  31. {
  32. //-----------------------------------------------------------------------------
  33. EventQueue()
  34. : m_size(0), m_read(0)
  35. {
  36. }
  37. int32_t space()
  38. {
  39. return QUEUE_SIZE - m_size;
  40. }
  41. bool empty()
  42. {
  43. return m_read == m_size;
  44. }
  45. //-----------------------------------------------------------------------------
  46. void push_event(uint32_t event_type, void* event_data, size_t event_size)
  47. {
  48. int32_t next_size = 4 + 4 + event_size;
  49. if (next_size >= space())
  50. {
  51. Log::d("queue full");
  52. return;
  53. }
  54. int32_t cur_size = m_size;
  55. *(&m_buffer[cur_size]) = event_type;
  56. *(&m_buffer[cur_size] + 4) = event_size;
  57. memcpy(&m_buffer[cur_size] + 8, event_data, event_size);
  58. m_size += next_size;
  59. }
  60. uint32_t event_type()
  61. {
  62. if (empty())
  63. {
  64. return 0;
  65. }
  66. int32_t cur_read = m_read;
  67. uint32_t type = (uint32_t) *(&m_buffer[cur_read]);
  68. m_read += 4;
  69. return type;
  70. }
  71. //-----------------------------------------------------------------------------
  72. void get_next_event(void* data)
  73. {
  74. if (empty())
  75. {
  76. data = NULL;
  77. }
  78. int32_t cur_read = m_read;
  79. size_t event_size = (size_t) *(&m_buffer[cur_read]);
  80. memcpy(data, &m_buffer[cur_read] + 4, event_size);
  81. m_read += 4 + event_size;
  82. }
  83. private:
  84. char m_buffer[QUEUE_SIZE];
  85. AtomicInt m_size;
  86. AtomicInt m_read;
  87. };
  88. } // namespace crown