message_queue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. ERR_PRINT("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id) + ". Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
  51. statistics();
  52. return ERR_OUT_OF_MEMORY;
  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. ERR_PRINT("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id) + ". Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
  70. statistics();
  71. return ERR_OUT_OF_MEMORY;
  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. int room_needed = sizeof(Message) + sizeof(Variant) * p_argcount;
  93. if ((buffer_end + room_needed) >= buffer_size) {
  94. ERR_PRINT("Failed method: " + p_callable + ". Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
  95. statistics();
  96. return ERR_OUT_OF_MEMORY;
  97. }
  98. Message *msg = memnew_placement(&buffer[buffer_end], Message);
  99. msg->args = p_argcount;
  100. msg->callable = p_callable;
  101. msg->type = TYPE_CALL;
  102. if (p_show_error) {
  103. msg->type |= FLAG_SHOW_ERROR;
  104. }
  105. buffer_end += sizeof(Message);
  106. for (int i = 0; i < p_argcount; i++) {
  107. Variant *v = memnew_placement(&buffer[buffer_end], Variant);
  108. buffer_end += sizeof(Variant);
  109. *v = *p_args[i];
  110. }
  111. return OK;
  112. }
  113. void MessageQueue::statistics() {
  114. HashMap<StringName, int> set_count;
  115. HashMap<int, int> notify_count;
  116. HashMap<Callable, int> call_count;
  117. int null_count = 0;
  118. uint32_t read_pos = 0;
  119. while (read_pos < buffer_end) {
  120. Message *message = (Message *)&buffer[read_pos];
  121. Object *target = message->callable.get_object();
  122. if (target != nullptr) {
  123. switch (message->type & FLAG_MASK) {
  124. case TYPE_CALL: {
  125. if (!call_count.has(message->callable)) {
  126. call_count[message->callable] = 0;
  127. }
  128. call_count[message->callable]++;
  129. } break;
  130. case TYPE_NOTIFICATION: {
  131. if (!notify_count.has(message->notification)) {
  132. notify_count[message->notification] = 0;
  133. }
  134. notify_count[message->notification]++;
  135. } break;
  136. case TYPE_SET: {
  137. StringName t = message->callable.get_method();
  138. if (!set_count.has(t)) {
  139. set_count[t] = 0;
  140. }
  141. set_count[t]++;
  142. } break;
  143. }
  144. } else {
  145. //object was deleted
  146. print_line("Object was deleted while awaiting a callback");
  147. null_count++;
  148. }
  149. read_pos += sizeof(Message);
  150. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  151. read_pos += sizeof(Variant) * message->args;
  152. }
  153. }
  154. print_line("TOTAL BYTES: " + itos(buffer_end));
  155. print_line("NULL count: " + itos(null_count));
  156. for (const KeyValue<StringName, int> &E : set_count) {
  157. print_line("SET " + E.key + ": " + itos(E.value));
  158. }
  159. for (const KeyValue<Callable, int> &E : call_count) {
  160. print_line("CALL " + E.key + ": " + itos(E.value));
  161. }
  162. for (const KeyValue<int, int> &E : notify_count) {
  163. print_line("NOTIFY " + itos(E.key) + ": " + itos(E.value));
  164. }
  165. }
  166. int MessageQueue::get_max_buffer_usage() const {
  167. return buffer_max_used;
  168. }
  169. void MessageQueue::_call_function(const Callable &p_callable, const Variant *p_args, int p_argcount, bool p_show_error) {
  170. const Variant **argptrs = nullptr;
  171. if (p_argcount) {
  172. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount);
  173. for (int i = 0; i < p_argcount; i++) {
  174. argptrs[i] = &p_args[i];
  175. }
  176. }
  177. Callable::CallError ce;
  178. Variant ret;
  179. p_callable.callp(argptrs, p_argcount, ret, ce);
  180. if (p_show_error && ce.error != Callable::CallError::CALL_OK) {
  181. ERR_PRINT("Error calling deferred method: " + Variant::get_callable_error_text(p_callable, argptrs, p_argcount, ce) + ".");
  182. }
  183. }
  184. void MessageQueue::flush() {
  185. if (buffer_end > buffer_max_used) {
  186. buffer_max_used = buffer_end;
  187. }
  188. uint32_t read_pos = 0;
  189. //using reverse locking strategy
  190. _THREAD_SAFE_LOCK_
  191. if (flushing) {
  192. _THREAD_SAFE_UNLOCK_
  193. ERR_FAIL_COND(flushing); //already flushing, you did something odd
  194. }
  195. flushing = true;
  196. while (read_pos < buffer_end) {
  197. //lock on each iteration, so a call can re-add itself to the message queue
  198. Message *message = (Message *)&buffer[read_pos];
  199. uint32_t advance = sizeof(Message);
  200. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  201. advance += sizeof(Variant) * message->args;
  202. }
  203. //pre-advance so this function is reentrant
  204. read_pos += advance;
  205. _THREAD_SAFE_UNLOCK_
  206. Object *target = message->callable.get_object();
  207. if (target != nullptr) {
  208. switch (message->type & FLAG_MASK) {
  209. case TYPE_CALL: {
  210. Variant *args = (Variant *)(message + 1);
  211. // messages don't expect a return value
  212. _call_function(message->callable, args, message->args, message->type & FLAG_SHOW_ERROR);
  213. } break;
  214. case TYPE_NOTIFICATION: {
  215. // messages don't expect a return value
  216. target->notification(message->notification);
  217. } break;
  218. case TYPE_SET: {
  219. Variant *arg = (Variant *)(message + 1);
  220. // messages don't expect a return value
  221. target->set(message->callable.get_method(), *arg);
  222. } break;
  223. }
  224. }
  225. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  226. Variant *args = (Variant *)(message + 1);
  227. for (int i = 0; i < message->args; i++) {
  228. args[i].~Variant();
  229. }
  230. }
  231. message->~Message();
  232. _THREAD_SAFE_LOCK_
  233. }
  234. buffer_end = 0; // reset buffer
  235. flushing = false;
  236. _THREAD_SAFE_UNLOCK_
  237. }
  238. bool MessageQueue::is_flushing() const {
  239. return flushing;
  240. }
  241. MessageQueue::MessageQueue() {
  242. ERR_FAIL_COND_MSG(singleton != nullptr, "A MessageQueue singleton already exists.");
  243. singleton = this;
  244. 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);
  245. buffer_size *= 1024;
  246. buffer = memnew_arr(uint8_t, buffer_size);
  247. }
  248. MessageQueue::~MessageQueue() {
  249. uint32_t read_pos = 0;
  250. while (read_pos < buffer_end) {
  251. Message *message = (Message *)&buffer[read_pos];
  252. Variant *args = (Variant *)(message + 1);
  253. int argc = message->args;
  254. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  255. for (int i = 0; i < argc; i++) {
  256. args[i].~Variant();
  257. }
  258. }
  259. message->~Message();
  260. read_pos += sizeof(Message);
  261. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  262. read_pos += sizeof(Variant) * message->args;
  263. }
  264. }
  265. singleton = nullptr;
  266. memdelete_arr(buffer);
  267. }