message_queue.cpp 11 KB

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