message_queue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*************************************************************************/
  2. /* message_queue.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "message_queue.h"
  31. #include "core/project_settings.h"
  32. #include "core/script_language.h"
  33. MessageQueue *MessageQueue::singleton = nullptr;
  34. MessageQueue *MessageQueue::get_singleton() {
  35. return singleton;
  36. }
  37. Error MessageQueue::push_call(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
  38. _THREAD_SAFE_METHOD_
  39. int room_needed = sizeof(Message) + sizeof(Variant) * p_argcount;
  40. if ((buffer_end + room_needed) >= buffer_size) {
  41. String type;
  42. if (ObjectDB::get_instance(p_id)) {
  43. type = ObjectDB::get_instance(p_id)->get_class();
  44. }
  45. print_line("Failed method: " + type + ":" + p_method + " target ID: " + itos(p_id));
  46. statistics();
  47. ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
  48. }
  49. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  50. msg->args = p_argcount;
  51. msg->instance_id = p_id;
  52. msg->target = p_method;
  53. msg->type = TYPE_CALL;
  54. if (p_show_error) {
  55. msg->type |= FLAG_SHOW_ERROR;
  56. }
  57. buffer_end += sizeof(Message);
  58. for (int i = 0; i < p_argcount; i++) {
  59. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  60. buffer_end += sizeof(Variant);
  61. *v = *p_args[i];
  62. }
  63. return OK;
  64. }
  65. Error MessageQueue::push_call(ObjectID p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  66. VARIANT_ARGPTRS;
  67. int argc = 0;
  68. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  69. if (argptr[i]->get_type() == Variant::NIL) {
  70. break;
  71. }
  72. argc++;
  73. }
  74. return push_call(p_id, p_method, argptr, argc, false);
  75. }
  76. Error MessageQueue::push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value) {
  77. _THREAD_SAFE_METHOD_
  78. uint8_t room_needed = sizeof(Message) + sizeof(Variant);
  79. if ((buffer_end + room_needed) >= buffer_size) {
  80. String type;
  81. if (ObjectDB::get_instance(p_id)) {
  82. type = ObjectDB::get_instance(p_id)->get_class();
  83. }
  84. print_line("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id));
  85. statistics();
  86. ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
  87. }
  88. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  89. msg->args = 1;
  90. msg->instance_id = p_id;
  91. msg->target = p_prop;
  92. msg->type = TYPE_SET;
  93. buffer_end += sizeof(Message);
  94. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  95. buffer_end += sizeof(Variant);
  96. *v = p_value;
  97. return OK;
  98. }
  99. Error MessageQueue::push_notification(ObjectID p_id, int p_notification) {
  100. _THREAD_SAFE_METHOD_
  101. ERR_FAIL_COND_V(p_notification < 0, ERR_INVALID_PARAMETER);
  102. uint8_t room_needed = sizeof(Message);
  103. if ((buffer_end + room_needed) >= buffer_size) {
  104. print_line("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id));
  105. statistics();
  106. ERR_FAIL_V_MSG(ERR_OUT_OF_MEMORY, "Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
  107. }
  108. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  109. msg->type = TYPE_NOTIFICATION;
  110. msg->instance_id = p_id;
  111. //msg->target;
  112. msg->notification = p_notification;
  113. buffer_end += sizeof(Message);
  114. return OK;
  115. }
  116. Error MessageQueue::push_call(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) {
  117. return push_call(p_object->get_instance_id(), p_method, VARIANT_ARG_PASS);
  118. }
  119. Error MessageQueue::push_notification(Object *p_object, int p_notification) {
  120. return push_notification(p_object->get_instance_id(), p_notification);
  121. }
  122. Error MessageQueue::push_set(Object *p_object, const StringName &p_prop, const Variant &p_value) {
  123. return push_set(p_object->get_instance_id(), p_prop, p_value);
  124. }
  125. void MessageQueue::statistics() {
  126. Map<StringName, int> set_count;
  127. Map<int, int> notify_count;
  128. Map<StringName, int> call_count;
  129. int null_count = 0;
  130. uint32_t read_pos = 0;
  131. while (read_pos < buffer_end) {
  132. Message *message = (Message *)&buffer[read_pos];
  133. Object *target = ObjectDB::get_instance(message->instance_id);
  134. if (target != nullptr) {
  135. switch (message->type & FLAG_MASK) {
  136. case TYPE_CALL: {
  137. if (!call_count.has(message->target)) {
  138. call_count[message->target] = 0;
  139. }
  140. call_count[message->target]++;
  141. } break;
  142. case TYPE_NOTIFICATION: {
  143. if (!notify_count.has(message->notification)) {
  144. notify_count[message->notification] = 0;
  145. }
  146. notify_count[message->notification]++;
  147. } break;
  148. case TYPE_SET: {
  149. if (!set_count.has(message->target)) {
  150. set_count[message->target] = 0;
  151. }
  152. set_count[message->target]++;
  153. } break;
  154. }
  155. } else {
  156. //object was deleted
  157. print_line("Object was deleted while awaiting a callback");
  158. null_count++;
  159. }
  160. read_pos += sizeof(Message);
  161. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  162. read_pos += sizeof(Variant) * message->args;
  163. }
  164. }
  165. print_line("TOTAL BYTES: " + itos(buffer_end));
  166. print_line("NULL count: " + itos(null_count));
  167. for (Map<StringName, int>::Element *E = set_count.front(); E; E = E->next()) {
  168. print_line("SET " + E->key() + ": " + itos(E->get()));
  169. }
  170. for (Map<StringName, int>::Element *E = call_count.front(); E; E = E->next()) {
  171. print_line("CALL " + E->key() + ": " + itos(E->get()));
  172. }
  173. for (Map<int, int>::Element *E = notify_count.front(); E; E = E->next()) {
  174. print_line("NOTIFY " + itos(E->key()) + ": " + itos(E->get()));
  175. }
  176. }
  177. int MessageQueue::get_max_buffer_usage() const {
  178. return buffer_max_used;
  179. }
  180. void MessageQueue::_call_function(Object *p_target, const StringName &p_func, const Variant *p_args, int p_argcount, bool p_show_error) {
  181. const Variant **argptrs = nullptr;
  182. if (p_argcount) {
  183. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount);
  184. for (int i = 0; i < p_argcount; i++) {
  185. argptrs[i] = &p_args[i];
  186. }
  187. }
  188. Variant::CallError ce;
  189. p_target->call(p_func, argptrs, p_argcount, ce);
  190. if (p_show_error && ce.error != Variant::CallError::CALL_OK) {
  191. ERR_PRINT("Error calling deferred method: " + Variant::get_call_error_text(p_target, p_func, argptrs, p_argcount, ce) + ".");
  192. }
  193. }
  194. void MessageQueue::flush() {
  195. if (buffer_end > buffer_max_used) {
  196. buffer_max_used = buffer_end;
  197. }
  198. uint32_t read_pos = 0;
  199. //using reverse locking strategy
  200. _THREAD_SAFE_LOCK_
  201. ERR_FAIL_COND(flushing); //already flushing, you did something odd
  202. flushing = true;
  203. while (read_pos < buffer_end) {
  204. //lock on each iteration, so a call can re-add itself to the message queue
  205. Message *message = (Message *)&buffer[read_pos];
  206. uint32_t advance = sizeof(Message);
  207. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  208. advance += sizeof(Variant) * message->args;
  209. }
  210. //pre-advance so this function is reentrant
  211. read_pos += advance;
  212. _THREAD_SAFE_UNLOCK_
  213. Object *target = ObjectDB::get_instance(message->instance_id);
  214. if (target != nullptr) {
  215. switch (message->type & FLAG_MASK) {
  216. case TYPE_CALL: {
  217. Variant *args = (Variant *)(message + 1);
  218. // messages don't expect a return value
  219. _call_function(target, message->target, args, message->args, message->type & FLAG_SHOW_ERROR);
  220. } break;
  221. case TYPE_NOTIFICATION: {
  222. // messages don't expect a return value
  223. target->notification(message->notification);
  224. } break;
  225. case TYPE_SET: {
  226. Variant *arg = (Variant *)(message + 1);
  227. // messages don't expect a return value
  228. target->set(message->target, *arg);
  229. } break;
  230. }
  231. }
  232. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  233. Variant *args = (Variant *)(message + 1);
  234. for (int i = 0; i < message->args; i++) {
  235. args[i].~Variant();
  236. }
  237. }
  238. message->~Message();
  239. _THREAD_SAFE_LOCK_
  240. }
  241. buffer_end = 0; // reset buffer
  242. flushing = false;
  243. _THREAD_SAFE_UNLOCK_
  244. }
  245. bool MessageQueue::is_flushing() const {
  246. return flushing;
  247. }
  248. MessageQueue::MessageQueue() {
  249. ERR_FAIL_COND_MSG(singleton != nullptr, "A MessageQueue singleton already exists.");
  250. singleton = this;
  251. flushing = false;
  252. buffer_end = 0;
  253. buffer_max_used = 0;
  254. buffer_size = GLOBAL_DEF_RST("memory/limits/message_queue/max_size_kb", DEFAULT_QUEUE_SIZE_KB);
  255. ProjectSettings::get_singleton()->set_custom_property_info("memory/limits/message_queue/max_size_kb", PropertyInfo(Variant::INT, "memory/limits/message_queue/max_size_kb", PROPERTY_HINT_RANGE, "1024,4096,1,or_greater"));
  256. buffer_size *= 1024;
  257. buffer = memnew_arr(uint8_t, buffer_size);
  258. }
  259. MessageQueue::~MessageQueue() {
  260. uint32_t read_pos = 0;
  261. while (read_pos < buffer_end) {
  262. Message *message = (Message *)&buffer[read_pos];
  263. Variant *args = (Variant *)(message + 1);
  264. int argc = message->args;
  265. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  266. for (int i = 0; i < argc; i++) {
  267. args[i].~Variant();
  268. }
  269. }
  270. message->~Message();
  271. read_pos += sizeof(Message);
  272. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  273. read_pos += sizeof(Variant) * message->args;
  274. }
  275. }
  276. singleton = nullptr;
  277. memdelete_arr(buffer);
  278. }