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-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. Error MessageQueue::push_callable(const Callable &p_callable, VARIANT_ARG_DECLARE) {
  124. VARIANT_ARGPTRS;
  125. int argc = 0;
  126. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  127. if (argptr[i]->get_type() == Variant::NIL) {
  128. break;
  129. }
  130. argc++;
  131. }
  132. return push_callable(p_callable, argptr, argc);
  133. }
  134. void MessageQueue::statistics() {
  135. Map<StringName, int> set_count;
  136. Map<int, int> notify_count;
  137. Map<Callable, int> call_count;
  138. int null_count = 0;
  139. uint32_t read_pos = 0;
  140. while (read_pos < buffer_end) {
  141. Message *message = (Message *)&buffer[read_pos];
  142. Object *target = message->callable.get_object();
  143. if (target != nullptr) {
  144. switch (message->type & FLAG_MASK) {
  145. case TYPE_CALL: {
  146. if (!call_count.has(message->callable)) {
  147. call_count[message->callable] = 0;
  148. }
  149. call_count[message->callable]++;
  150. } break;
  151. case TYPE_NOTIFICATION: {
  152. if (!notify_count.has(message->notification)) {
  153. notify_count[message->notification] = 0;
  154. }
  155. notify_count[message->notification]++;
  156. } break;
  157. case TYPE_SET: {
  158. StringName t = message->callable.get_method();
  159. if (!set_count.has(t)) {
  160. set_count[t] = 0;
  161. }
  162. set_count[t]++;
  163. } break;
  164. }
  165. } else {
  166. //object was deleted
  167. print_line("Object was deleted while awaiting a callback");
  168. null_count++;
  169. }
  170. read_pos += sizeof(Message);
  171. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  172. read_pos += sizeof(Variant) * message->args;
  173. }
  174. }
  175. print_line("TOTAL BYTES: " + itos(buffer_end));
  176. print_line("NULL count: " + itos(null_count));
  177. for (Map<StringName, int>::Element *E = set_count.front(); E; E = E->next()) {
  178. print_line("SET " + E->key() + ": " + itos(E->get()));
  179. }
  180. for (Map<Callable, int>::Element *E = call_count.front(); E; E = E->next()) {
  181. print_line("CALL " + E->key() + ": " + itos(E->get()));
  182. }
  183. for (Map<int, int>::Element *E = notify_count.front(); E; E = E->next()) {
  184. print_line("NOTIFY " + itos(E->key()) + ": " + itos(E->get()));
  185. }
  186. }
  187. int MessageQueue::get_max_buffer_usage() const {
  188. return buffer_max_used;
  189. }
  190. void MessageQueue::_call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error) {
  191. const Variant **argptrs = nullptr;
  192. if (p_argcount) {
  193. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount);
  194. for (int i = 0; i < p_argcount; i++) {
  195. argptrs[i] = &p_args[i];
  196. }
  197. }
  198. Callable::CallError ce;
  199. Variant ret;
  200. p_callable.call(argptrs, p_argcount, ret, ce);
  201. if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
  202. ERR_PRINT("Error calling deferred method: " + Variant::get_callable_error_text(p_callable, argptrs, p_argcount, ce) + ".");
  203. }
  204. }
  205. void MessageQueue::flush() {
  206. if (buffer_end > buffer_max_used) {
  207. buffer_max_used = buffer_end;
  208. }
  209. uint32_t read_pos = 0;
  210. //using reverse locking strategy
  211. _THREAD_SAFE_LOCK_
  212. if (flushing) {
  213. _THREAD_SAFE_UNLOCK_
  214. ERR_FAIL_COND(flushing); //already flushing, you did something odd
  215. }
  216. flushing = true;
  217. while (read_pos < buffer_end) {
  218. //lock on each iteration, so a call can re-add itself to the message queue
  219. Message *message = (Message *)&buffer[read_pos];
  220. uint32_t advance = sizeof(Message);
  221. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  222. advance += sizeof(Variant) * message->args;
  223. }
  224. //pre-advance so this function is reentrant
  225. read_pos += advance;
  226. _THREAD_SAFE_UNLOCK_
  227. Object *target = message->callable.get_object();
  228. if (target != nullptr) {
  229. switch (message->type & FLAG_MASK) {
  230. case TYPE_CALL: {
  231. Variant *args = (Variant *)(message + 1);
  232. // messages don't expect a return value
  233. _call_function(message->callable, args, message->args, message->type & FLAG_SHOW_ERROR);
  234. } break;
  235. case TYPE_NOTIFICATION: {
  236. // messages don't expect a return value
  237. target->notification(message->notification);
  238. } break;
  239. case TYPE_SET: {
  240. Variant *arg = (Variant *)(message + 1);
  241. // messages don't expect a return value
  242. target->set(message->callable.get_method(), *arg);
  243. } break;
  244. }
  245. }
  246. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  247. Variant *args = (Variant *)(message + 1);
  248. for (int i = 0; i < message->args; i++) {
  249. args[i].~Variant();
  250. }
  251. }
  252. message->~Message();
  253. _THREAD_SAFE_LOCK_
  254. }
  255. buffer_end = 0; // reset buffer
  256. flushing = false;
  257. _THREAD_SAFE_UNLOCK_
  258. }
  259. bool MessageQueue::is_flushing() const {
  260. return flushing;
  261. }
  262. MessageQueue::MessageQueue() {
  263. ERR_FAIL_COND_MSG(singleton != nullptr, "A MessageQueue singleton already exists.");
  264. singleton = this;
  265. buffer_size = GLOBAL_DEF_RST("memory/limits/message_queue/max_size_kb", DEFAULT_QUEUE_SIZE_KB);
  266. 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"));
  267. buffer_size *= 1024;
  268. buffer = memnew_arr(uint8_t, buffer_size);
  269. }
  270. MessageQueue::~MessageQueue() {
  271. uint32_t read_pos = 0;
  272. while (read_pos < buffer_end) {
  273. Message *message = (Message *)&buffer[read_pos];
  274. Variant *args = (Variant *)(message + 1);
  275. int argc = message->args;
  276. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  277. for (int i = 0; i < argc; i++) {
  278. args[i].~Variant();
  279. }
  280. }
  281. message->~Message();
  282. read_pos += sizeof(Message);
  283. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  284. read_pos += sizeof(Variant) * message->args;
  285. }
  286. }
  287. singleton = nullptr;
  288. memdelete_arr(buffer);
  289. }