message_queue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 = nullptr;
  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. }
  48. argc++;
  49. }
  50. return push_call(p_id, p_method, argptr, argc, false);
  51. }
  52. Error MessageQueue::push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value) {
  53. _THREAD_SAFE_METHOD_
  54. uint8_t room_needed = sizeof(Message) + sizeof(Variant);
  55. if ((buffer_end + room_needed) >= buffer_size) {
  56. String type;
  57. if (ObjectDB::get_instance(p_id)) {
  58. type = ObjectDB::get_instance(p_id)->get_class();
  59. }
  60. print_line("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id));
  61. statistics();
  62. 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.");
  63. }
  64. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  65. msg->args = 1;
  66. msg->callable = Callable(p_id, p_prop);
  67. msg->type = TYPE_SET;
  68. buffer_end += sizeof(Message);
  69. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  70. buffer_end += sizeof(Variant);
  71. *v = p_value;
  72. return OK;
  73. }
  74. Error MessageQueue::push_notification(ObjectID p_id, int p_notification) {
  75. _THREAD_SAFE_METHOD_
  76. ERR_FAIL_COND_V(p_notification < 0, ERR_INVALID_PARAMETER);
  77. uint8_t room_needed = sizeof(Message);
  78. if ((buffer_end + room_needed) >= buffer_size) {
  79. print_line("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id));
  80. statistics();
  81. 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.");
  82. }
  83. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  84. msg->type = TYPE_NOTIFICATION;
  85. msg->callable = Callable(p_id, CoreStringNames::get_singleton()->notification); //name is meaningless but callable needs it
  86. //msg->target;
  87. msg->notification = p_notification;
  88. buffer_end += sizeof(Message);
  89. return OK;
  90. }
  91. Error MessageQueue::push_call(Object *p_object, const StringName &p_method, VARIANT_ARG_DECLARE) {
  92. return push_call(p_object->get_instance_id(), p_method, VARIANT_ARG_PASS);
  93. }
  94. Error MessageQueue::push_notification(Object *p_object, int p_notification) {
  95. return push_notification(p_object->get_instance_id(), p_notification);
  96. }
  97. Error MessageQueue::push_set(Object *p_object, const StringName &p_prop, const Variant &p_value) {
  98. return push_set(p_object->get_instance_id(), p_prop, p_value);
  99. }
  100. Error MessageQueue::push_callable(const Callable &p_callable, const Variant **p_args, int p_argcount, bool p_show_error) {
  101. _THREAD_SAFE_METHOD_
  102. int room_needed = sizeof(Message) + sizeof(Variant) * p_argcount;
  103. if ((buffer_end + room_needed) >= buffer_size) {
  104. print_line("Failed method: " + p_callable);
  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->args = p_argcount;
  110. msg->callable = p_callable;
  111. msg->type = TYPE_CALL;
  112. if (p_show_error) {
  113. msg->type |= FLAG_SHOW_ERROR;
  114. }
  115. buffer_end += sizeof(Message);
  116. for (int i = 0; i < p_argcount; i++) {
  117. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  118. buffer_end += sizeof(Variant);
  119. *v = *p_args[i];
  120. }
  121. return OK;
  122. }
  123. void MessageQueue::statistics() {
  124. Map<StringName, int> set_count;
  125. Map<int, int> notify_count;
  126. Map<Callable, int> call_count;
  127. int null_count = 0;
  128. uint32_t read_pos = 0;
  129. while (read_pos < buffer_end) {
  130. Message *message = (Message *)&buffer[read_pos];
  131. Object *target = message->callable.get_object();
  132. if (target != nullptr) {
  133. switch (message->type & FLAG_MASK) {
  134. case TYPE_CALL: {
  135. if (!call_count.has(message->callable)) {
  136. call_count[message->callable] = 0;
  137. }
  138. call_count[message->callable]++;
  139. } break;
  140. case TYPE_NOTIFICATION: {
  141. if (!notify_count.has(message->notification)) {
  142. notify_count[message->notification] = 0;
  143. }
  144. notify_count[message->notification]++;
  145. } break;
  146. case TYPE_SET: {
  147. StringName t = message->callable.get_method();
  148. if (!set_count.has(t)) {
  149. set_count[t] = 0;
  150. }
  151. set_count[t]++;
  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. }
  164. print_line("TOTAL BYTES: " + itos(buffer_end));
  165. print_line("NULL count: " + itos(null_count));
  166. for (Map<StringName, int>::Element *E = set_count.front(); E; E = E->next()) {
  167. print_line("SET " + E->key() + ": " + itos(E->get()));
  168. }
  169. for (Map<Callable, int>::Element *E = call_count.front(); E; E = E->next()) {
  170. print_line("CALL " + E->key() + ": " + itos(E->get()));
  171. }
  172. for (Map<int, int>::Element *E = notify_count.front(); E; E = E->next()) {
  173. print_line("NOTIFY " + itos(E->key()) + ": " + itos(E->get()));
  174. }
  175. }
  176. int MessageQueue::get_max_buffer_usage() const {
  177. return buffer_max_used;
  178. }
  179. void MessageQueue::_call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error) {
  180. const Variant **argptrs = nullptr;
  181. if (p_argcount) {
  182. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount);
  183. for (int i = 0; i < p_argcount; i++) {
  184. argptrs[i] = &p_args[i];
  185. }
  186. }
  187. Callable::CallError ce;
  188. Variant ret;
  189. p_callable.call(argptrs, p_argcount, ret, ce);
  190. if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
  191. ERR_PRINT("Error calling deferred method: " + Variant::get_callable_error_text(p_callable, 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. if (flushing) {
  202. _THREAD_SAFE_UNLOCK_
  203. ERR_FAIL_COND(flushing); //already flushing, you did something odd
  204. }
  205. flushing = true;
  206. while (read_pos < buffer_end) {
  207. //lock on each iteration, so a call can re-add itself to the message queue
  208. Message *message = (Message *)&buffer[read_pos];
  209. uint32_t advance = sizeof(Message);
  210. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  211. advance += sizeof(Variant) * message->args;
  212. }
  213. //pre-advance so this function is reentrant
  214. read_pos += advance;
  215. _THREAD_SAFE_UNLOCK_
  216. Object *target = message->callable.get_object();
  217. if (target != nullptr) {
  218. switch (message->type & FLAG_MASK) {
  219. case TYPE_CALL: {
  220. Variant *args = (Variant *)(message + 1);
  221. // messages don't expect a return value
  222. _call_function(message->callable, args, message->args, message->type & FLAG_SHOW_ERROR);
  223. } break;
  224. case TYPE_NOTIFICATION: {
  225. // messages don't expect a return value
  226. target->notification(message->notification);
  227. } break;
  228. case TYPE_SET: {
  229. Variant *arg = (Variant *)(message + 1);
  230. // messages don't expect a return value
  231. target->set(message->callable.get_method(), *arg);
  232. } break;
  233. }
  234. }
  235. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  236. Variant *args = (Variant *)(message + 1);
  237. for (int i = 0; i < message->args; i++) {
  238. args[i].~Variant();
  239. }
  240. }
  241. message->~Message();
  242. _THREAD_SAFE_LOCK_
  243. }
  244. buffer_end = 0; // reset buffer
  245. flushing = false;
  246. _THREAD_SAFE_UNLOCK_
  247. }
  248. bool MessageQueue::is_flushing() const {
  249. return flushing;
  250. }
  251. MessageQueue::MessageQueue() {
  252. ERR_FAIL_COND_MSG(singleton != nullptr, "A MessageQueue singleton already exists.");
  253. singleton = this;
  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. }