Debian Codegraph @

core/object/message_queue.h

raw symbols SCIP
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
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 // Needs to be public to be able to define it outside the class.
54 // Needs to lock because there can be multiple of these allocators in several threads.
55 typedef PagedAllocator<Page, true> Allocator;
56
57 private:
58 enum {
59 TYPE_CALL,
60 TYPE_NOTIFICATION,
61 TYPE_SET,
62 TYPE_END, // End marker.
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() }; // +1 makes sure zero sized arrays are also supported.
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() }; // +1 makes sure zero sized arrays are also supported.
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() }; // +1 makes sure zero sized arrays are also supported.
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 };