message_queue.cpp 11 KB

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