message_queue.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #pragma once
  31. #include "core/object/object_id.h"
  32. #include "core/os/thread_safe.h"
  33. #include "core/templates/local_vector.h"
  34. #include "core/templates/paged_allocator.h"
  35. #include "core/variant/variant.h"
  36. class Object;
  37. class CallQueue {
  38. friend class MessageQueue;
  39. public:
  40. enum {
  41. PAGE_SIZE_BYTES = 4096
  42. };
  43. struct Page {
  44. uint8_t data[PAGE_SIZE_BYTES];
  45. };
  46. // Needs to be public to be able to define it outside the class.
  47. // Needs to lock because there can be multiple of these allocators in several threads.
  48. typedef PagedAllocator<Page, true> Allocator;
  49. private:
  50. enum {
  51. TYPE_CALL,
  52. TYPE_NOTIFICATION,
  53. TYPE_SET,
  54. TYPE_END, // End marker.
  55. FLAG_NULL_IS_OK = 1 << 13,
  56. FLAG_SHOW_ERROR = 1 << 14,
  57. FLAG_MASK = FLAG_NULL_IS_OK - 1,
  58. };
  59. Mutex mutex;
  60. Allocator *allocator = nullptr;
  61. bool allocator_is_custom = false;
  62. LocalVector<Page *> pages;
  63. LocalVector<uint32_t> page_bytes;
  64. uint32_t max_pages = 0;
  65. uint32_t pages_used = 0;
  66. bool flushing = false;
  67. #ifdef DEV_ENABLED
  68. bool is_current_thread_override = false;
  69. #endif
  70. struct Message {
  71. Callable callable;
  72. int16_t type;
  73. union {
  74. int16_t notification;
  75. int16_t args;
  76. };
  77. };
  78. _FORCE_INLINE_ void _ensure_first_page() {
  79. if (unlikely(pages.is_empty())) {
  80. pages.push_back(allocator->alloc());
  81. page_bytes.push_back(0);
  82. pages_used = 1;
  83. }
  84. }
  85. void _add_page();
  86. void _call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error);
  87. String error_text;
  88. public:
  89. Error push_callp(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
  90. template <typename... VarArgs>
  91. Error push_call(ObjectID p_id, const StringName &p_method, VarArgs... p_args) {
  92. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  93. const Variant *argptrs[sizeof...(p_args) + 1];
  94. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  95. argptrs[i] = &args[i];
  96. }
  97. return push_callp(p_id, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  98. }
  99. Error push_callablep(const Callable &p_callable, const Variant **p_args, int p_argcount, bool p_show_error = false);
  100. Error push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value);
  101. Error push_notification(ObjectID p_id, int p_notification);
  102. template <typename... VarArgs>
  103. Error push_callable(const Callable &p_callable, VarArgs... p_args) {
  104. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  105. const Variant *argptrs[sizeof...(p_args) + 1];
  106. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  107. argptrs[i] = &args[i];
  108. }
  109. return push_callablep(p_callable, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  110. }
  111. Error push_callp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false);
  112. template <typename... VarArgs>
  113. Error push_call(Object *p_object, const StringName &p_method, VarArgs... p_args) {
  114. Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
  115. const Variant *argptrs[sizeof...(p_args) + 1];
  116. for (uint32_t i = 0; i < sizeof...(p_args); i++) {
  117. argptrs[i] = &args[i];
  118. }
  119. return push_callp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
  120. }
  121. Error push_notification(Object *p_object, int p_notification);
  122. Error push_set(Object *p_object, const StringName &p_prop, const Variant &p_value);
  123. Error flush();
  124. void clear();
  125. void statistics();
  126. bool has_messages() const;
  127. bool is_flushing() const;
  128. int get_max_buffer_usage() const;
  129. CallQueue(Allocator *p_custom_allocator = nullptr, uint32_t p_max_pages = 8192, const String &p_error_text = String());
  130. virtual ~CallQueue();
  131. };
  132. class MessageQueue : public CallQueue {
  133. static CallQueue *main_singleton;
  134. static thread_local CallQueue *thread_singleton;
  135. friend class CallQueue;
  136. public:
  137. _FORCE_INLINE_ static CallQueue *get_singleton() { return thread_singleton ? thread_singleton : main_singleton; }
  138. _FORCE_INLINE_ static CallQueue *get_main_singleton() { return main_singleton; }
  139. static void set_thread_singleton_override(CallQueue *p_thread_singleton);
  140. MessageQueue();
  141. ~MessageQueue();
  142. };