| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/object/object_id.h" |
| 34 |
#include "core/os/thread_safe.h" |
| 35 |
#include "core/templates/local_vector.h" |
| 36 |
#include "core/templates/paged_allocator.h" |
| 37 |
#include "core/variant/variant.h" |
| 38 |
|
| 39 |
class Object; |
| 40 |
|
| 41 |
class CallQueue { |
| 42 |
friend class MessageQueue; |
| 43 |
|
| 44 |
public: |
| 45 |
enum { |
| 46 |
PAGE_SIZE_BYTES = 4096 |
| 47 |
}; |
| 48 |
|
| 49 |
struct Page { |
| 50 |
uint8_t data[PAGE_SIZE_BYTES]; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
typedef PagedAllocator<Page, true> Allocator; |
| 56 |
|
| 57 |
private: |
| 58 |
enum { |
| 59 |
TYPE_CALL, |
| 60 |
TYPE_NOTIFICATION, |
| 61 |
TYPE_SET, |
| 62 |
TYPE_END, |
| 63 |
FLAG_NULL_IS_OK = 1 << 13, |
| 64 |
FLAG_SHOW_ERROR = 1 << 14, |
| 65 |
FLAG_MASK = FLAG_NULL_IS_OK - 1, |
| 66 |
}; |
| 67 |
|
| 68 |
Mutex mutex; |
| 69 |
|
| 70 |
Allocator *allocator = nullptr; |
| 71 |
bool allocator_is_custom = false; |
| 72 |
|
| 73 |
LocalVector<Page *> pages; |
| 74 |
LocalVector<uint32_t> page_bytes; |
| 75 |
uint32_t max_pages = 0; |
| 76 |
uint32_t pages_used = 0; |
| 77 |
bool flushing = false; |
| 78 |
|
| 79 |
#ifdef DEV_ENABLED |
| 80 |
bool is_current_thread_override = false; |
| 81 |
#endif |
| 82 |
|
| 83 |
struct Message { |
| 84 |
Callable callable; |
| 85 |
int16_t type; |
| 86 |
union { |
| 87 |
int16_t notification; |
| 88 |
int16_t args; |
| 89 |
}; |
| 90 |
}; |
| 91 |
|
| 92 |
_FORCE_INLINE_ void _ensure_first_page() { |
| 93 |
if (unlikely(pages.is_empty())) { |
| 94 |
pages.push_back(allocator->alloc()); |
| 95 |
page_bytes.push_back(0); |
| 96 |
pages_used = 1; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void _add_page(); |
| 101 |
|
| 102 |
void _call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error); |
| 103 |
|
| 104 |
String error_text; |
| 105 |
|
| 106 |
public: |
| 107 |
Error push_callp(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false); |
| 108 |
template <typename... VarArgs> |
| 109 |
Error push_call(ObjectID p_id, const StringName &p_method, VarArgs... p_args) { |
| 110 |
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; |
| 111 |
const Variant *argptrs[sizeof...(p_args) + 1]; |
| 112 |
for (uint32_t i = 0; i < sizeof...(p_args); i++) { |
| 113 |
argptrs[i] = &args[i]; |
| 114 |
} |
| 115 |
return push_callp(p_id, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); |
| 116 |
} |
| 117 |
|
| 118 |
Error push_callablep(const Callable &p_callable, const Variant **p_args, int p_argcount, bool p_show_error = false); |
| 119 |
Error push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value); |
| 120 |
Error push_notification(ObjectID p_id, int p_notification); |
| 121 |
|
| 122 |
template <typename... VarArgs> |
| 123 |
Error push_callable(const Callable &p_callable, VarArgs... p_args) { |
| 124 |
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; |
| 125 |
const Variant *argptrs[sizeof...(p_args) + 1]; |
| 126 |
for (uint32_t i = 0; i < sizeof...(p_args); i++) { |
| 127 |
argptrs[i] = &args[i]; |
| 128 |
} |
| 129 |
return push_callablep(p_callable, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); |
| 130 |
} |
| 131 |
|
| 132 |
Error push_callp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error = false); |
| 133 |
template <typename... VarArgs> |
| 134 |
Error push_call(Object *p_object, const StringName &p_method, VarArgs... p_args) { |
| 135 |
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; |
| 136 |
const Variant *argptrs[sizeof...(p_args) + 1]; |
| 137 |
for (uint32_t i = 0; i < sizeof...(p_args); i++) { |
| 138 |
argptrs[i] = &args[i]; |
| 139 |
} |
| 140 |
return push_callp(p_object, p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args)); |
| 141 |
} |
| 142 |
|
| 143 |
Error push_notification(Object *p_object, int p_notification); |
| 144 |
Error push_set(Object *p_object, const StringName &p_prop, const Variant &p_value); |
| 145 |
|
| 146 |
Error flush(); |
| 147 |
void clear(); |
| 148 |
void statistics(); |
| 149 |
|
| 150 |
bool has_messages() const; |
| 151 |
|
| 152 |
bool is_flushing() const; |
| 153 |
int get_max_buffer_usage() const; |
| 154 |
|
| 155 |
CallQueue(Allocator *p_custom_allocator = nullptr, uint32_t p_max_pages = 8192, const String &p_error_text = String()); |
| 156 |
virtual ~CallQueue(); |
| 157 |
}; |
| 158 |
|
| 159 |
class MessageQueue : public CallQueue { |
| 160 |
static CallQueue *main_singleton; |
| 161 |
static thread_local CallQueue *thread_singleton; |
| 162 |
friend class CallQueue; |
| 163 |
|
| 164 |
public: |
| 165 |
_FORCE_INLINE_ static CallQueue *get_singleton() { return thread_singleton ? thread_singleton : main_singleton; } |
| 166 |
_FORCE_INLINE_ static CallQueue *get_main_singleton() { return main_singleton; } |
| 167 |
|
| 168 |
static void set_thread_singleton_override(CallQueue *p_thread_singleton); |
| 169 |
|
| 170 |
MessageQueue(); |
| 171 |
~MessageQueue(); |
| 172 |
}; |