message_queue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**************************************************************************/
  2. /* message_queue.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/project_settings.h"
  32. #include "core/core_string_names.h"
  33. #include "core/object/class_db.h"
  34. #include "core/object/script_language.h"
  35. MessageQueue *MessageQueue::singleton = nullptr;
  36. MessageQueue *MessageQueue::get_singleton() {
  37. return singleton;
  38. }
  39. Error MessageQueue::push_callp(ObjectID p_id, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
  40. return push_callablep(Callable(p_id, p_method), p_args, p_argcount, p_show_error);
  41. }
  42. Error MessageQueue::push_set(ObjectID p_id, const StringName &p_prop, const Variant &p_value) {
  43. _THREAD_SAFE_METHOD_
  44. uint8_t room_needed = sizeof(Message) + sizeof(Variant);
  45. if ((buffer_end + room_needed) >= buffer_size) {
  46. String type;
  47. if (ObjectDB::get_instance(p_id)) {
  48. type = ObjectDB::get_instance(p_id)->get_class();
  49. }
  50. print_line("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id));
  51. statistics();
  52. 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.");
  53. }
  54. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  55. msg->args = 1;
  56. msg->callable = Callable(p_id, p_prop);
  57. msg->type = TYPE_SET;
  58. buffer_end += sizeof(Message);
  59. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  60. buffer_end += sizeof(Variant);
  61. *v = p_value;
  62. return OK;
  63. }
  64. Error MessageQueue::push_notification(ObjectID p_id, int p_notification) {
  65. _THREAD_SAFE_METHOD_
  66. ERR_FAIL_COND_V(p_notification < 0, ERR_INVALID_PARAMETER);
  67. uint8_t room_needed = sizeof(Message);
  68. if ((buffer_end + room_needed) >= buffer_size) {
  69. print_line("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id));
  70. statistics();
  71. 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.");
  72. }
  73. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  74. msg->type = TYPE_NOTIFICATION;
  75. msg->callable = Callable(p_id, CoreStringNames::get_singleton()->notification); //name is meaningless but callable needs it
  76. //msg->target;
  77. msg->notification = p_notification;
  78. buffer_end += sizeof(Message);
  79. return OK;
  80. }
  81. Error MessageQueue::push_callp(Object *p_object, const StringName &p_method, const Variant **p_args, int p_argcount, bool p_show_error) {
  82. return push_callp(p_object->get_instance_id(), p_method, p_args, p_argcount, p_show_error);
  83. }
  84. Error MessageQueue::push_notification(Object *p_object, int p_notification) {
  85. return push_notification(p_object->get_instance_id(), p_notification);
  86. }
  87. Error MessageQueue::push_set(Object *p_object, const StringName &p_prop, const Variant &p_value) {
  88. return push_set(p_object->get_instance_id(), p_prop, p_value);
  89. }
  90. Error MessageQueue::push_callablep(const Callable &p_callable, const Variant **p_args, int p_argcount, bool p_show_error) {
  91. _THREAD_SAFE_METHOD_
  92. Vector<Variant> bound_arguments;
  93. int bound_argcount = 0;
  94. p_callable.get_bound_arguments_ref(bound_arguments, bound_argcount);
  95. int room_needed = sizeof(Message) + sizeof(Variant) * (bound_argcount + p_argcount);
  96. if ((buffer_end + room_needed) >= buffer_size) {
  97. print_line("Failed method: " + p_callable);
  98. statistics();
  99. 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.");
  100. }
  101. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  102. msg->args = bound_argcount + p_argcount;
  103. msg->callable = p_callable;
  104. msg->type = TYPE_CALL;
  105. if (p_show_error) {
  106. msg->type |= FLAG_SHOW_ERROR;
  107. }
  108. buffer_end += sizeof(Message);
  109. for (int i = 0; i < bound_argcount; i++) {
  110. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  111. buffer_end += sizeof(Variant);
  112. *v = bound_arguments[i];
  113. }
  114. for (int i = 0; i < p_argcount; i++) {
  115. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  116. buffer_end += sizeof(Variant);
  117. *v = *p_args[i];
  118. }
  119. return OK;
  120. }
  121. void MessageQueue::statistics() {
  122. HashMap<StringName, int> set_count;
  123. HashMap<int, int> notify_count;
  124. HashMap<Callable, 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 = message->callable.get_object();
  130. if (target != nullptr) {
  131. switch (message->type & FLAG_MASK) {
  132. case TYPE_CALL: {
  133. if (!call_count.has(message->callable)) {
  134. call_count[message->callable] = 0;
  135. }
  136. call_count[message->callable]++;
  137. } break;
  138. case TYPE_NOTIFICATION: {
  139. if (!notify_count.has(message->notification)) {
  140. notify_count[message->notification] = 0;
  141. }
  142. notify_count[message->notification]++;
  143. } break;
  144. case TYPE_SET: {
  145. StringName t = message->callable.get_method();
  146. if (!set_count.has(t)) {
  147. set_count[t] = 0;
  148. }
  149. set_count[t]++;
  150. } break;
  151. }
  152. } else {
  153. //object was deleted
  154. print_line("Object was deleted while awaiting a callback");
  155. null_count++;
  156. }
  157. read_pos += sizeof(Message);
  158. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  159. read_pos += sizeof(Variant) * message->args;
  160. }
  161. }
  162. print_line("TOTAL BYTES: " + itos(buffer_end));
  163. print_line("NULL count: " + itos(null_count));
  164. for (const KeyValue<StringName, int> &E : set_count) {
  165. print_line("SET " + E.key + ": " + itos(E.value));
  166. }
  167. for (const KeyValue<Callable, int> &E : call_count) {
  168. print_line("CALL " + E.key + ": " + itos(E.value));
  169. }
  170. for (const KeyValue<int, int> &E : notify_count) {
  171. print_line("NOTIFY " + itos(E.key) + ": " + itos(E.value));
  172. }
  173. }
  174. int MessageQueue::get_max_buffer_usage() const {
  175. return buffer_max_used;
  176. }
  177. void MessageQueue::_call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error) {
  178. const Variant **argptrs = nullptr;
  179. if (p_argcount) {
  180. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount);
  181. for (int i = 0; i < p_argcount; i++) {
  182. argptrs[i] = &p_args[i];
  183. }
  184. }
  185. Callable::CallError ce;
  186. Variant ret;
  187. p_callable.callp(argptrs, p_argcount, ret, ce);
  188. if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
  189. ERR_PRINT("Error calling deferred method: " + Variant::get_callable_error_text(p_callable, 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. if (flushing) {
  200. _THREAD_SAFE_UNLOCK_
  201. ERR_FAIL_COND(flushing); //already flushing, you did something odd
  202. }
  203. flushing = true;
  204. while (read_pos < buffer_end) {
  205. //lock on each iteration, so a call can re-add itself to the message queue
  206. Message *message = (Message *)&buffer[read_pos];
  207. uint32_t advance = sizeof(Message);
  208. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  209. advance += sizeof(Variant) * message->args;
  210. }
  211. //pre-advance so this function is reentrant
  212. read_pos += advance;
  213. _THREAD_SAFE_UNLOCK_
  214. Object *target = message->callable.get_object();
  215. if (target != nullptr) {
  216. switch (message->type & FLAG_MASK) {
  217. case TYPE_CALL: {
  218. Variant *args = (Variant *)(message + 1);
  219. // messages don't expect a return value
  220. _call_function(message->callable, args, message->args, message->type & FLAG_SHOW_ERROR);
  221. } break;
  222. case TYPE_NOTIFICATION: {
  223. // messages don't expect a return value
  224. target->notification(message->notification);
  225. } break;
  226. case TYPE_SET: {
  227. Variant *arg = (Variant *)(message + 1);
  228. // messages don't expect a return value
  229. target->set(message->callable.get_method(), *arg);
  230. } break;
  231. }
  232. }
  233. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  234. Variant *args = (Variant *)(message + 1);
  235. for (int i = 0; i < message->args; i++) {
  236. args[i].~Variant();
  237. }
  238. }
  239. message->~Message();
  240. _THREAD_SAFE_LOCK_
  241. }
  242. buffer_end = 0; // reset buffer
  243. flushing = false;
  244. _THREAD_SAFE_UNLOCK_
  245. }
  246. bool MessageQueue::is_flushing() const {
  247. return flushing;
  248. }
  249. MessageQueue::MessageQueue() {
  250. ERR_FAIL_COND_MSG(singleton != nullptr, "A MessageQueue singleton already exists.");
  251. singleton = this;
  252. buffer_size = GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "memory/limits/message_queue/max_size_kb", PROPERTY_HINT_RANGE, "1024,4096,1,or_greater"), DEFAULT_QUEUE_SIZE_KB);
  253. buffer_size *= 1024;
  254. buffer = memnew_arr(uint8_t, buffer_size);
  255. }
  256. MessageQueue::~MessageQueue() {
  257. uint32_t read_pos = 0;
  258. while (read_pos < buffer_end) {
  259. Message *message = (Message *)&buffer[read_pos];
  260. Variant *args = (Variant *)(message + 1);
  261. int argc = message->args;
  262. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  263. for (int i = 0; i < argc; i++) {
  264. args[i].~Variant();
  265. }
  266. }
  267. message->~Message();
  268. read_pos += sizeof(Message);
  269. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  270. read_pos += sizeof(Variant) * message->args;
  271. }
  272. }
  273. singleton = nullptr;
  274. memdelete_arr(buffer);
  275. }