| 1 | /**************************************************************************/ |
| 2 | /* message_queue.cpp */ |
| 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 | #include "message_queue.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/object/class_db.h" |
| 35 | #include "core/object/script_language.h" |
| 36 | |
| 37 | #include <cstdio> |
| 38 | |
| 39 | #ifdef DEV_ENABLED |
| 40 | // Includes safety checks to ensure that a queue set as a thread singleton override |
| 41 | // is only ever called from the thread it was set for. |
| 42 | #define LOCK_MUTEX \ |
| 43 | if (this != MessageQueue::thread_singleton) { \ |
| 44 | DEV_ASSERT(!is_current_thread_override); \ |
| 45 | mutex.lock(); \ |
| 46 | } else { \ |
| 47 | DEV_ASSERT(is_current_thread_override); \ |
| 48 | } |
| 49 | #else |
| 50 | #define LOCK_MUTEX \ |
| 51 | if (this != MessageQueue::thread_singleton) { \ |
| 52 | mutex.lock(); \ |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | #define UNLOCK_MUTEX \ |
| 57 | if (this != MessageQueue::thread_singleton) { \ |
| 58 | mutex.unlock(); \ |
| 59 | } |
| 60 | |
| 61 | void CallQueue::_add_page() { |
| 62 | if (pages_used == page_bytes.size()) { |
| 63 | pages.push_back(allocator->alloc()); |
| 64 | page_bytes.push_back(0); |
| 65 | } |
| 66 | page_bytes[pages_used] = 0; |
| 67 | pages_used++; |
| 68 | } |
| 69 | |
| 70 | Error CallQueue::push_callp(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) { |
| 71 | return push_callablep(Callable(p_id, p_method), p_args, p_argcount, p_show_error); |
| 72 | } |
| 73 | |
| 74 | Error CallQueue::push_callp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) { |
| 75 | return push_callp(p_object->get_instance_id(), p_method, p_args, p_argcount, p_show_error); |
| 76 | } |
| 77 | |
| 78 | Error CallQueue::push_notification(Object *p_object, int p_notification) { |
| 79 | return push_notification(p_object->get_instance_id(), p_notification); |
| 80 | } |
| 81 | |
| 82 | Error CallQueue::push_set(Object *p_object, const StringName &p_prop, const Variant &p_value) { |
| 83 | return push_set(p_object->get_instance_id(), p_prop, p_value); |
| 84 | } |
| 85 | |
| 86 | Error CallQueue::push_callablep(const Callable &p_callable, const Variant **p_args, int p_argcount, bool p_show_error) { |
| 87 | uint32_t room_needed = sizeof(Message) + sizeof(Variant) * p_argcount; |
| 88 | |
| 89 | ERR_FAIL_COND_V_MSG(room_needed > uint32_t(PAGE_SIZE_BYTES), ERR_INVALID_PARAMETER, "Message is too large to fit on a page (" + itos(PAGE_SIZE_BYTES) + " bytes), consider passing less arguments."); |
| 90 | |
| 91 | LOCK_MUTEX; |
| 92 | |
| 93 | _ensure_first_page(); |
| 94 | |
| 95 | if ((page_bytes[pages_used - 1] + room_needed) > uint32_t(PAGE_SIZE_BYTES)) { |
| 96 | if (pages_used == max_pages) { |
| 97 | fprintf(stderr, "Failed method: %s. Message queue out of memory. %s\n", String(p_callable).utf8().get_data(), error_text.utf8().get_data()); |
| 98 | statistics(); |
| 99 | UNLOCK_MUTEX; |
| 100 | return ERR_OUT_OF_MEMORY; |
| 101 | } |
| 102 | _add_page(); |
| 103 | } |
| 104 | |
| 105 | Page *page = pages[pages_used - 1]; |
| 106 | |
| 107 | uint8_t *buffer_end = &page->data[page_bytes[pages_used - 1]]; |
| 108 | |
| 109 | Message *msg = memnew_placement(buffer_end, Message); |
| 110 | msg->args = p_argcount; |
| 111 | msg->callable = p_callable; |
| 112 | msg->type = TYPE_CALL; |
| 113 | if (p_show_error) { |
| 114 | msg->type |= FLAG_SHOW_ERROR; |
| 115 | } |
| 116 | // Support callables of static methods. |
| 117 | if (p_callable.get_object_id().is_null() && p_callable.is_valid()) { |
| 118 | msg->type |= FLAG_NULL_IS_OK; |
| 119 | } |
| 120 | |
| 121 | buffer_end += sizeof(Message); |
| 122 | |
| 123 | for (int i = 0; i < p_argcount; i++) { |
| 124 | Variant *v = memnew_placement(buffer_end, Variant); |
| 125 | buffer_end += sizeof(Variant); |
| 126 | *v = *p_args[i]; |
| 127 | } |
| 128 | |
| 129 | page_bytes[pages_used - 1] += room_needed; |
| 130 | |
| 131 | UNLOCK_MUTEX; |
| 132 | |
| 133 | return OK; |
| 134 | } |
| 135 | |
| 136 | Error CallQueue::push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value) { |
| 137 | LOCK_MUTEX; |
| 138 | uint32_t room_needed = sizeof(Message) + sizeof(Variant); |
| 139 | |
| 140 | _ensure_first_page(); |
| 141 | |
| 142 | if ((page_bytes[pages_used - 1] + room_needed) > uint32_t(PAGE_SIZE_BYTES)) { |
| 143 | if (pages_used == max_pages) { |
| 144 | String type; |
| 145 | if (ObjectDB::get_instance(p_id)) { |
| 146 | type = ObjectDB::get_instance(p_id)->get_class(); |
| 147 | } |
| 148 | fprintf(stderr, "Failed set: %s: %s target ID: %s. Message queue out of memory. %s\n", type.utf8().get_data(), String(p_prop).utf8().get_data(), itos(p_id).utf8().get_data(), error_text.utf8().get_data()); |
| 149 | statistics(); |
| 150 | |
| 151 | UNLOCK_MUTEX; |
| 152 | return ERR_OUT_OF_MEMORY; |
| 153 | } |
| 154 | _add_page(); |
| 155 | } |
| 156 | |
| 157 | Page *page = pages[pages_used - 1]; |
| 158 | uint8_t *buffer_end = &page->data[page_bytes[pages_used - 1]]; |
| 159 | |
| 160 | Message *msg = memnew_placement(buffer_end, Message); |
| 161 | msg->args = 1; |
| 162 | msg->callable = Callable(p_id, p_prop); |
| 163 | msg->type = TYPE_SET; |
| 164 | |
| 165 | buffer_end += sizeof(Message); |
| 166 | |
| 167 | Variant *v = memnew_placement(buffer_end, Variant); |
| 168 | *v = p_value; |
| 169 | |
| 170 | page_bytes[pages_used - 1] += room_needed; |
| 171 | UNLOCK_MUTEX; |
| 172 | |
| 173 | return OK; |
| 174 | } |
| 175 | |
| 176 | Error CallQueue::push_notification(ObjectID p_id, int p_notification) { |
| 177 | ERR_FAIL_COND_V(p_notification < 0, ERR_INVALID_PARAMETER); |
| 178 | LOCK_MUTEX; |
| 179 | uint32_t room_needed = sizeof(Message); |
| 180 | |
| 181 | _ensure_first_page(); |
| 182 | |
| 183 | if ((page_bytes[pages_used - 1] + room_needed) > uint32_t(PAGE_SIZE_BYTES)) { |
| 184 | if (pages_used == max_pages) { |
| 185 | fprintf(stderr, "Failed notification: %d target ID: %s. Message queue out of memory. %s\n", p_notification, itos(p_id).utf8().get_data(), error_text.utf8().get_data()); |
| 186 | statistics(); |
| 187 | UNLOCK_MUTEX; |
| 188 | return ERR_OUT_OF_MEMORY; |
| 189 | } |
| 190 | _add_page(); |
| 191 | } |
| 192 | |
| 193 | Page *page = pages[pages_used - 1]; |
| 194 | uint8_t *buffer_end = &page->data[page_bytes[pages_used - 1]]; |
| 195 | |
| 196 | Message *msg = memnew_placement(buffer_end, Message); |
| 197 | |
| 198 | msg->type = TYPE_NOTIFICATION; |
| 199 | msg->callable = Callable(p_id, CoreStringName(notification)); //name is meaningless but callable needs it |
| 200 | //msg->target; |
| 201 | msg->notification = p_notification; |
| 202 | |
| 203 | page_bytes[pages_used - 1] += room_needed; |
| 204 | UNLOCK_MUTEX; |
| 205 | |
| 206 | return OK; |
| 207 | } |
| 208 | |
| 209 | void CallQueue::_call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error) { |
| 210 | const Variant **argptrs = nullptr; |
| 211 | if (p_argcount) { |
| 212 | argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount); |
| 213 | for (int i = 0; i < p_argcount; i++) { |
| 214 | argptrs[i] = &p_args[i]; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | Callable::CallError ce; |
| 219 | Variant ret; |
| 220 | p_callable.callp(argptrs, p_argcount, ret, ce); |
| 221 | if (p_show_error && ce.error != Callable::CallError::CALL_OK) { |
| 222 | ERR_PRINT("Error calling deferred method: " + Variant::get_callable_error_text(p_callable, argptrs, p_argcount, ce) + "."); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | Error CallQueue::flush() { |
| 227 | LOCK_MUTEX; |
| 228 | |
| 229 | if (pages.is_empty()) { |
| 230 | // Never allocated |
| 231 | UNLOCK_MUTEX; |
| 232 | return OK; // Do nothing. |
| 233 | } |
| 234 | |
| 235 | if (flushing) { |
| 236 | UNLOCK_MUTEX; |
| 237 | return ERR_BUSY; |
| 238 | } |
| 239 | |
| 240 | flushing = true; |
| 241 | |
| 242 | uint32_t i = 0; |
| 243 | uint32_t offset = 0; |
| 244 | |
| 245 | while (i < pages_used && offset < page_bytes[i]) { |
| 246 | Page *page = pages[i]; |
| 247 | |
| 248 | //lock on each iteration, so a call can re-add itself to the message queue |
| 249 | |
| 250 | Message *message = (Message *)&page->data[offset]; |
| 251 | |
| 252 | uint32_t advance = sizeof(Message); |
| 253 | if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { |
| 254 | advance += sizeof(Variant) * message->args; |
| 255 | } |
| 256 | |
| 257 | //pre-advance so this function is reentrant |
| 258 | offset += advance; |
| 259 | |
| 260 | Object *target = message->callable.get_object(); |
| 261 | |
| 262 | UNLOCK_MUTEX; |
| 263 | |
| 264 | switch (message->type & FLAG_MASK) { |
| 265 | case TYPE_CALL: { |
| 266 | if (target || (message->type & FLAG_NULL_IS_OK)) { |
| 267 | Variant *args = (Variant *)(message + 1); |
| 268 | _call_function(message->callable, args, message->args, message->type & FLAG_SHOW_ERROR); |
| 269 | } |
| 270 | } break; |
| 271 | case TYPE_NOTIFICATION: { |
| 272 | if (target) { |
| 273 | target->notification(message->notification); |
| 274 | } |
| 275 | } break; |
| 276 | case TYPE_SET: { |
| 277 | if (target) { |
| 278 | Variant *arg = (Variant *)(message + 1); |
| 279 | target->set(message->callable.get_method(), *arg); |
| 280 | } |
| 281 | } break; |
| 282 | } |
| 283 | |
| 284 | if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { |
| 285 | Variant *args = (Variant *)(message + 1); |
| 286 | for (int k = 0; k < message->args; k++) { |
| 287 | args[k].~Variant(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | message->~Message(); |
| 292 | |
| 293 | LOCK_MUTEX; |
| 294 | if (offset == page_bytes[i]) { |
| 295 | i++; |
| 296 | offset = 0; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | page_bytes[0] = 0; |
| 301 | pages_used = 1; |
| 302 | |
| 303 | flushing = false; |
| 304 | UNLOCK_MUTEX; |
| 305 | return OK; |
| 306 | } |
| 307 | |
| 308 | void CallQueue::clear() { |
| 309 | LOCK_MUTEX; |
| 310 | |
| 311 | if (pages.is_empty()) { |
| 312 | UNLOCK_MUTEX; |
| 313 | return; // Nothing to clear. |
| 314 | } |
| 315 | |
| 316 | for (uint32_t i = 0; i < pages_used; i++) { |
| 317 | uint32_t offset = 0; |
| 318 | while (offset < page_bytes[i]) { |
| 319 | Page *page = pages[i]; |
| 320 | |
| 321 | //lock on each iteration, so a call can re-add itself to the message queue |
| 322 | |
| 323 | Message *message = (Message *)&page->data[offset]; |
| 324 | |
| 325 | uint32_t advance = sizeof(Message); |
| 326 | if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { |
| 327 | advance += sizeof(Variant) * message->args; |
| 328 | } |
| 329 | |
| 330 | offset += advance; |
| 331 | |
| 332 | if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { |
| 333 | Variant *args = (Variant *)(message + 1); |
| 334 | for (int k = 0; k < message->args; k++) { |
| 335 | args[k].~Variant(); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | message->~Message(); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | pages_used = 1; |
| 344 | page_bytes[0] = 0; |
| 345 | |
| 346 | UNLOCK_MUTEX; |
| 347 | } |
| 348 | |
| 349 | void CallQueue::statistics() { |
| 350 | LOCK_MUTEX; |
| 351 | HashMap<StringName, int> set_count; |
| 352 | HashMap<int, int> notify_count; |
| 353 | HashMap<Callable, int> call_count; |
| 354 | int null_count = 0; |
| 355 | |
| 356 | for (uint32_t i = 0; i < pages_used; i++) { |
| 357 | uint32_t offset = 0; |
| 358 | while (offset < page_bytes[i]) { |
| 359 | Page *page = pages[i]; |
| 360 | |
| 361 | //lock on each iteration, so a call can re-add itself to the message queue |
| 362 | |
| 363 | Message *message = (Message *)&page->data[offset]; |
| 364 | |
| 365 | uint32_t advance = sizeof(Message); |
| 366 | if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { |
| 367 | advance += sizeof(Variant) * message->args; |
| 368 | } |
| 369 | |
| 370 | Object *target = message->callable.get_object(); |
| 371 | |
| 372 | bool null_target = true; |
| 373 | switch (message->type & FLAG_MASK) { |
| 374 | case TYPE_CALL: { |
| 375 | if (target || (message->type & FLAG_NULL_IS_OK)) { |
| 376 | if (!call_count.has(message->callable)) { |
| 377 | call_count[message->callable] = 0; |
| 378 | } |
| 379 | |
| 380 | call_count[message->callable]++; |
| 381 | null_target = false; |
| 382 | } |
| 383 | } break; |
| 384 | case TYPE_NOTIFICATION: { |
| 385 | if (target) { |
| 386 | if (!notify_count.has(message->notification)) { |
| 387 | notify_count[message->notification] = 0; |
| 388 | } |
| 389 | |
| 390 | notify_count[message->notification]++; |
| 391 | null_target = false; |
| 392 | } |
| 393 | } break; |
| 394 | case TYPE_SET: { |
| 395 | if (target) { |
| 396 | StringName t = message->callable.get_method(); |
| 397 | if (!set_count.has(t)) { |
| 398 | set_count[t] = 0; |
| 399 | } |
| 400 | |
| 401 | set_count[t]++; |
| 402 | null_target = false; |
| 403 | } |
| 404 | } break; |
| 405 | } |
| 406 | if (null_target) { |
| 407 | // Object was deleted. |
| 408 | fprintf(stdout, "Object was deleted while awaiting a callback.\n"); |
| 409 | |
| 410 | null_count++; |
| 411 | } |
| 412 | |
| 413 | offset += advance; |
| 414 | |
| 415 | if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) { |
| 416 | Variant *args = (Variant *)(message + 1); |
| 417 | for (int k = 0; k < message->args; k++) { |
| 418 | args[k].~Variant(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | message->~Message(); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | fprintf(stdout, "TOTAL PAGES: %d (%d bytes).\n", pages_used, pages_used * PAGE_SIZE_BYTES); |
| 427 | fprintf(stdout, "NULL count: %d.\n", null_count); |
| 428 | |
| 429 | for (const KeyValue<StringName, int> &E : set_count) { |
| 430 | fprintf(stdout, "SET %s: %d.\n", String(E.key).utf8().get_data(), E.value); |
| 431 | } |
| 432 | |
| 433 | for (const KeyValue<Callable, int> &E : call_count) { |
| 434 | fprintf(stdout, "CALL %s: %d.\n", String(E.key).utf8().get_data(), E.value); |
| 435 | } |
| 436 | |
| 437 | for (const KeyValue<int, int> &E : notify_count) { |
| 438 | fprintf(stdout, "NOTIFY %d: %d.\n", E.key, E.value); |
| 439 | } |
| 440 | |
| 441 | UNLOCK_MUTEX; |
| 442 | } |
| 443 | |
| 444 | bool CallQueue::is_flushing() const { |
| 445 | return flushing; |
| 446 | } |
| 447 | |
| 448 | bool CallQueue::has_messages() const { |
| 449 | if (pages_used == 0) { |
| 450 | return false; |
| 451 | } |
| 452 | if (pages_used == 1 && page_bytes[0] == 0) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | int CallQueue::get_max_buffer_usage() const { |
| 460 | return pages.size() * PAGE_SIZE_BYTES; |
| 461 | } |
| 462 | |
| 463 | CallQueue::CallQueue(Allocator *p_custom_allocator, uint32_t p_max_pages, const String &p_error_text) { |
| 464 | if (p_custom_allocator) { |
| 465 | allocator = p_custom_allocator; |
| 466 | allocator_is_custom = true; |
| 467 | } else { |
| 468 | allocator = memnew(Allocator(16)); // 16 elements per allocator page, 64kb per allocator page. Anything small will do, though. |
| 469 | allocator_is_custom = false; |
| 470 | } |
| 471 | max_pages = p_max_pages; |
| 472 | error_text = p_error_text; |
| 473 | } |
| 474 | |
| 475 | CallQueue::~CallQueue() { |
| 476 | clear(); |
| 477 | // Let go of pages. |
| 478 | for (uint32_t i = 0; i < pages.size(); i++) { |
| 479 | allocator->free(pages[i]); |
| 480 | } |
| 481 | if (!allocator_is_custom) { |
| 482 | memdelete(allocator); |
| 483 | } |
| 484 | DEV_ASSERT(!is_current_thread_override); |
| 485 | } |
| 486 | |
| 487 | ////////////////////// |
| 488 | |
| 489 | CallQueue *MessageQueue::main_singleton = nullptr; |
| 490 | thread_local CallQueue *MessageQueue::thread_singleton = nullptr; |
| 491 | |
| 492 | void MessageQueue::set_thread_singleton_override(CallQueue *p_thread_singleton) { |
| 493 | #ifdef DEV_ENABLED |
| 494 | if (thread_singleton) { |
| 495 | thread_singleton->is_current_thread_override = false; |
| 496 | } |
| 497 | #endif |
| 498 | thread_singleton = p_thread_singleton; |
| 499 | #ifdef DEV_ENABLED |
| 500 | if (thread_singleton) { |
| 501 | thread_singleton->is_current_thread_override = true; |
| 502 | } |
| 503 | #endif |
| 504 | } |
| 505 | |
| 506 | MessageQueue::MessageQueue() : |
| 507 | CallQueue(nullptr, |
| 508 | int(GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "memory/limits/message_queue/max_size_mb", PROPERTY_HINT_RANGE, "1,512,1,or_greater"), 32)) * 1024 * 1024 / PAGE_SIZE_BYTES, |
| 509 | "Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_mb' in project settings.") { |
| 510 | ERR_FAIL_COND_MSG(main_singleton != nullptr, "A MessageQueue singleton already exists."); |
| 511 | main_singleton = this; |
| 512 | } |
| 513 | |
| 514 | MessageQueue::~MessageQueue() { |
| 515 | main_singleton = nullptr; |
| 516 | } |