message_queue.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**************************************************************************/
  2. /* message_queue.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef MESSAGE_QUEUE_H
  31. #define MESSAGE_QUEUE_H
  32. #include "core/object/object_id.h"
  33. #include "core/os/thread_safe.h"
  34. #include "core/templates/local_vector.h"
  35. #include "core/templates/paged_allocator.h"
  36. #include "core/variant/variant.h"
  37. class Object;
  38. class CallQueue {
  39. friend class MessageQueue;
  40. public:
  41. enum {
  42. PAGE_SIZE_BYTES = 4096
  43. };
  44. struct Page {
  45. uint8_t data[PAGE_SIZE_BYTES];
  46. };
  47. // Needs to be public to be able to define it outside the class.
  48. // Needs to lock because there can be multiple of these allocators in several threads.
  49. typedef PagedAllocator<Page, true> Allocator;
  50. private:
  51. enum {
  52. TYPE_CALL,
  53. TYPE_NOTIFICATION,
  54. TYPE_SET,
  55. TYPE_END, // End marker.
  56. FLAG_NULL_IS_OK = 1 << 13,
  57. FLAG_SHOW_ERROR = 1 << 14,
  58. FLAG_MASK = FLAG_NULL_IS_OK - 1,
  59. };
  60. Mutex mutex;
  61. Allocator *allocator = nullptr;
  62. bool allocator_is_custom = false;
  63. LocalVector<Page *> pages;
  64. LocalVector<uint32_t> page_bytes;
  65. uint32_t max_pages = 0;
  66. uint32_t pages_used = 0;
  67. bool flushing = false;
  68. #ifdef DEV_ENABLED
  69. bool is_current_thread_override = false;
  70. #endif
  71. struct Message {
  72. Callable callable;
  73. int16_t type;
  74. union {
  75. int16_t notification;
  76. int16_t args;
  77. };
  78. };
  79. _FORCE_INLINE_ void _ensure_first_page() {
  80. if (unlikely(pages.is_empty())) {
  81. pages.push_back(allocator->alloc());
  82. page_bytes.push_back(0);
  83. pages_used = 1;
  84. }
  85. }
  86. void _add_page();
  87. void _call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error);
  88. String error_text;
  89. public:
  90. Error push_callp(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
  91. template <typename... VarArgs>
  92. Error push_call(ObjectID p_id, const StringName &p_method, VarArgs... p_args) {
  93. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  94. const Variant *argptrs[sizeof...(p_args) + 1];
  95. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  96. argptrs[i] = &args[i];
  97. }
  98. return push_callp(p_id, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  99. }
  100. Error push_callablep(const Callable &p_callable, const Variant **p_args, int p_argcount, bool p_show_error = false);
  101. Error push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value);
  102. Error push_notification(ObjectID p_id, int p_notification);
  103. template <typename... VarArgs>
  104. Error push_callable(const Callable &p_callable, VarArgs... p_args) {
  105. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  106. const Variant *argptrs[sizeof...(p_args) + 1];
  107. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  108. argptrs[i] = &args[i];
  109. }
  110. return push_callablep(p_callable, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  111. }
  112. Error push_callp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
  113. template <typename... VarArgs>
  114. Error push_call(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  115. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  116. const Variant *argptrs[sizeof...(p_args) + 1];
  117. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  118. argptrs[i] = &args[i];
  119. }
  120. return push_callp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  121. }
  122. Error push_notification(Object *p_object, int p_notification);
  123. Error push_set(Object *p_object, const StringName &p_prop, const Variant &p_value);
  124. Error flush();
  125. void clear();
  126. void statistics();
  127. bool has_messages() const;
  128. bool is_flushing() const;
  129. int get_max_buffer_usage() const;
  130. CallQueue(Allocator *p_custom_allocator = 0, uint32_t p_max_pages = 8192, const String &p_error_text = String());
  131. virtual ~CallQueue();
  132. };
  133. class MessageQueue : public CallQueue {
  134. static CallQueue *main_singleton;
  135. static thread_local CallQueue *thread_singleton;
  136. friend class CallQueue;
  137. public:
  138. _FORCE_INLINE_ static CallQueue *get_singleton() { return thread_singleton ? thread_singleton : main_singleton; }
  139. static void set_thread_singleton_override(CallQueue *p_thread_singleton);
  140. MessageQueue();
  141. ~MessageQueue();
  142. };
  143. #endif // MESSAGE_QUEUE_H