message_queue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*************************************************************************/
  2. /* message_queue.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/core_string_names.h"
  32. #include "core/project_settings.h"
  33. #include "core/script_language.h"
  34. MessageQueue *MessageQueue::singleton = NULL;
  35. MessageQueue *MessageQueue::get_singleton() {
  36. return singleton;
  37. }
  38. Error MessageQueue::push_call(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
  39. return push_callable(Callable(p_id, p_method), p_args, p_argcount, p_show_error);
  40. }
  41. Error MessageQueue::push_call(ObjectID p_id, const StringName &p_method, VARIANT_ARG_DECLARE) {
  42. VARIANT_ARGPTRS;
  43. int argc = 0;
  44. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  45. if (argptr[i]->get_type() == Variant::NIL)
  46. break;
  47. argc++;
  48. }
  49. return push_call(p_id, p_method, argptr, argc, false);
  50. }
  51. Error MessageQueue::push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value) {
  52. _THREAD_SAFE_METHOD_
  53. uint8_t room_needed = sizeof(Message) + sizeof(Variant);
  54. if ((buffer_end + room_needed) >= buffer_size) {
  55. String type;
  56. if (ObjectDB::get_instance(p_id))
  57. type = ObjectDB::get_instance(p_id)->get_class();
  58. print_line("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id));
  59. statistics();
  60. 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.");
  61. }
  62. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  63. msg->args = 1;
  64. msg->callable = Callable(p_id, p_prop);
  65. msg->type = TYPE_SET;
  66. buffer_end += sizeof(Message);
  67. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  68. buffer_end += sizeof(Variant);
  69. *v = p_value;
  70. return OK;
  71. }
  72. Error MessageQueue::push_notification(ObjectID p_id, int p_notification) {
  73. _THREAD_SAFE_METHOD_
  74. ERR_FAIL_COND_V(p_notification < 0, ERR_INVALID_PARAMETER);
  75. uint8_t room_needed = sizeof(Message);
  76. if ((buffer_end + room_needed) >= buffer_size) {
  77. print_line("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id));
  78. statistics();
  79. 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.");
  80. }
  81. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  82. msg->type = TYPE_NOTIFICATION;
  83. msg->callable = Callable(p_id, CoreStringNames::get_singleton()->notification); //name is meaningless but callable needs it
  84. //msg->target;
  85. msg->notification = p_notification;
  86. buffer_end += sizeof(Message);
  87. return OK;
  88. }
  89. Error MessageQueue::push_call(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) {
  90. return push_call(p_object->get_instance_id(), p_method, VARIANT_ARG_PASS);
  91. }
  92. Error MessageQueue::push_notification(Object *p_object, int p_notification) {
  93. return push_notification(p_object->get_instance_id(), p_notification);
  94. }
  95. Error MessageQueue::push_set(Object *p_object, const StringName &p_prop, const Variant &p_value) {
  96. return push_set(p_object->get_instance_id(), p_prop, p_value);
  97. }
  98. Error MessageQueue::push_callable(const Callable &p_callable, const Variant **p_args, int p_argcount, bool p_show_error) {
  99. _THREAD_SAFE_METHOD_
  100. int room_needed = sizeof(Message) + sizeof(Variant) * p_argcount;
  101. if ((buffer_end + room_needed) >= buffer_size) {
  102. print_line("Failed method: " + p_callable);
  103. statistics();
  104. 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.");
  105. }
  106. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  107. msg->args = p_argcount;
  108. msg->callable = p_callable;
  109. msg->type = TYPE_CALL;
  110. if (p_show_error)
  111. msg->type |= FLAG_SHOW_ERROR;
  112. buffer_end += sizeof(Message);
  113. for (int i = 0; i < p_argcount; i++) {
  114. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  115. buffer_end += sizeof(Variant);
  116. *v = *p_args[i];
  117. }
  118. return OK;
  119. }
  120. void MessageQueue::statistics() {
  121. Map<StringName, int> set_count;
  122. Map<int, int> notify_count;
  123. Map<Callable, int> call_count;
  124. int null_count = 0;
  125. uint32_t read_pos = 0;
  126. while (read_pos < buffer_end) {
  127. Message *message = (Message *)&buffer[read_pos];
  128. Object *target = message->callable.get_object();
  129. if (target != NULL) {
  130. switch (message->type & FLAG_MASK) {
  131. case TYPE_CALL: {
  132. if (!call_count.has(message->callable))
  133. call_count[message->callable] = 0;
  134. call_count[message->callable]++;
  135. } break;
  136. case TYPE_NOTIFICATION: {
  137. if (!notify_count.has(message->notification))
  138. notify_count[message->notification] = 0;
  139. notify_count[message->notification]++;
  140. } break;
  141. case TYPE_SET: {
  142. StringName t = message->callable.get_method();
  143. if (!set_count.has(t))
  144. set_count[t] = 0;
  145. set_count[t]++;
  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<Callable, 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(const Callable &p_callable, 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. Callable::CallError ce;
  181. Variant ret;
  182. p_callable.call(argptrs, p_argcount, ret, ce);
  183. if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
  184. ERR_PRINT("Error calling deferred method: " + Variant::get_callable_error_text(p_callable, argptrs, p_argcount, ce) + ".");
  185. }
  186. }
  187. void MessageQueue::flush() {
  188. if (buffer_end > buffer_max_used) {
  189. buffer_max_used = buffer_end;
  190. }
  191. uint32_t read_pos = 0;
  192. //using reverse locking strategy
  193. _THREAD_SAFE_LOCK_
  194. if (flushing) {
  195. _THREAD_SAFE_UNLOCK_
  196. ERR_FAIL_COND(flushing); //already flushing, you did something odd
  197. }
  198. flushing = true;
  199. while (read_pos < buffer_end) {
  200. //lock on each iteration, so a call can re-add itself to the message queue
  201. Message *message = (Message *)&buffer[read_pos];
  202. uint32_t advance = sizeof(Message);
  203. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION)
  204. advance += sizeof(Variant) * message->args;
  205. //pre-advance so this function is reentrant
  206. read_pos += advance;
  207. _THREAD_SAFE_UNLOCK_
  208. Object *target = message->callable.get_object();
  209. if (target != NULL) {
  210. switch (message->type & FLAG_MASK) {
  211. case TYPE_CALL: {
  212. Variant *args = (Variant *)(message + 1);
  213. // messages don't expect a return value
  214. _call_function(message->callable, args, message->args, message->type & FLAG_SHOW_ERROR);
  215. } break;
  216. case TYPE_NOTIFICATION: {
  217. // messages don't expect a return value
  218. target->notification(message->notification);
  219. } break;
  220. case TYPE_SET: {
  221. Variant *arg = (Variant *)(message + 1);
  222. // messages don't expect a return value
  223. target->set(message->callable.get_method(), *arg);
  224. } break;
  225. }
  226. }
  227. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  228. Variant *args = (Variant *)(message + 1);
  229. for (int i = 0; i < message->args; i++) {
  230. args[i].~Variant();
  231. }
  232. }
  233. message->~Message();
  234. _THREAD_SAFE_LOCK_
  235. }
  236. buffer_end = 0; // reset buffer
  237. flushing = false;
  238. _THREAD_SAFE_UNLOCK_
  239. }
  240. bool MessageQueue::is_flushing() const {
  241. return flushing;
  242. }
  243. MessageQueue::MessageQueue() {
  244. ERR_FAIL_COND_MSG(singleton != NULL, "A MessageQueue singleton already exists.");
  245. singleton = this;
  246. flushing = false;
  247. buffer_end = 0;
  248. buffer_max_used = 0;
  249. buffer_size = GLOBAL_DEF_RST("memory/limits/message_queue/max_size_kb", DEFAULT_QUEUE_SIZE_KB);
  250. 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"));
  251. buffer_size *= 1024;
  252. buffer = memnew_arr(uint8_t, buffer_size);
  253. }
  254. MessageQueue::~MessageQueue() {
  255. uint32_t read_pos = 0;
  256. while (read_pos < buffer_end) {
  257. Message *message = (Message *)&buffer[read_pos];
  258. Variant *args = (Variant *)(message + 1);
  259. int argc = message->args;
  260. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  261. for (int i = 0; i < argc; i++)
  262. args[i].~Variant();
  263. }
  264. message->~Message();
  265. read_pos += sizeof(Message);
  266. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION)
  267. read_pos += sizeof(Variant) * message->args;
  268. }
  269. singleton = NULL;
  270. memdelete_arr(buffer);
  271. }