message_queue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 "globals.h"
  32. #include "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_type();
  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_type();
  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_type();
  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. //object was deleted
  155. //WARN_PRINT("Object was deleted while awaiting a callback")
  156. //should it print a warning?
  157. } else {
  158. null_count++;
  159. }
  160. read_pos += sizeof(Message);
  161. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION)
  162. read_pos += sizeof(Variant) * message->args;
  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<StringName, 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. bool MessageQueue::print() {
  177. #if 0
  178. uint32_t read_pos=0;
  179. while (read_pos < buffer_end ) {
  180. Message *message = (Message*)&buffer[ read_pos ];
  181. Object *target = ObjectDB::get_instance(message->instance_ID);
  182. String cname;
  183. String cfunc;
  184. if (target==NULL) {
  185. //object was deleted
  186. //WARN_PRINT("Object was deleted while awaiting a callback")
  187. //should it print a warning?
  188. } else if (message->notification>=0) {
  189. // messages don't expect a return value
  190. cfunc="notification # "+itos(message->notification);
  191. cname=target->get_type();
  192. } else if (!message->target.empty()) {
  193. cfunc="property: "+message->target;
  194. cname=target->get_type();
  195. } else if (message->target) {
  196. cfunc=String(message->target)+"()";
  197. cname=target->get_type();
  198. }
  199. read_pos+=sizeof(Message);
  200. if (message->type!=TYPE_NOTIFICATION)
  201. read_pos+=sizeof(Variant)*message->args;
  202. }
  203. #endif
  204. return false;
  205. }
  206. int MessageQueue::get_max_buffer_usage() const {
  207. return buffer_max_used;
  208. }
  209. void MessageQueue::_call_function(Object *p_target, const StringName &p_func, const Variant *p_args, int p_argcount, bool p_show_error) {
  210. const Variant **argptrs = NULL;
  211. if (p_argcount) {
  212. argptrs = (const Variant **)alloca(sizeof(Variant *) * p_argcount);
  213. for (int i = 0; i < p_argcount; i++) {
  214. argptrs[i] = &p_args[i];
  215. }
  216. }
  217. Variant::CallError ce;
  218. p_target->call(p_func, argptrs, p_argcount, ce);
  219. if (p_show_error && ce.error != Variant::CallError::CALL_OK) {
  220. ERR_PRINTS("Error calling deferred method: " + Variant::get_call_error_text(p_target, p_func, argptrs, p_argcount, ce));
  221. }
  222. }
  223. void MessageQueue::flush() {
  224. if (buffer_end > buffer_max_used) {
  225. buffer_max_used = buffer_end;
  226. //statistics();
  227. }
  228. uint32_t read_pos = 0;
  229. //using reverse locking strategy
  230. _THREAD_SAFE_LOCK_
  231. while (read_pos < buffer_end) {
  232. //lock on each interation, so a call can re-add itself to the message queue
  233. Message *message = (Message *)&buffer[read_pos];
  234. uint32_t advance = sizeof(Message);
  235. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION)
  236. advance += sizeof(Variant) * message->args;
  237. //pre-advance so this function is reentrant
  238. read_pos += advance;
  239. _THREAD_SAFE_UNLOCK_
  240. Object *target = ObjectDB::get_instance(message->instance_ID);
  241. if (target != NULL) {
  242. switch (message->type & FLAG_MASK) {
  243. case TYPE_CALL: {
  244. Variant *args = (Variant *)(message + 1);
  245. // messages don't expect a return value
  246. _call_function(target, message->target, args, message->args, message->type & FLAG_SHOW_ERROR);
  247. for (int i = 0; i < message->args; i++) {
  248. args[i].~Variant();
  249. }
  250. } break;
  251. case TYPE_NOTIFICATION: {
  252. // messages don't expect a return value
  253. target->notification(message->notification);
  254. } break;
  255. case TYPE_SET: {
  256. Variant *arg = (Variant *)(message + 1);
  257. // messages don't expect a return value
  258. target->set(message->target, *arg);
  259. arg->~Variant();
  260. } break;
  261. }
  262. }
  263. message->~Message();
  264. _THREAD_SAFE_LOCK_
  265. }
  266. buffer_end = 0; // reset buffer
  267. _THREAD_SAFE_UNLOCK_
  268. }
  269. MessageQueue::MessageQueue() {
  270. ERR_FAIL_COND(singleton != NULL);
  271. singleton = this;
  272. buffer_end = 0;
  273. buffer_max_used = 0;
  274. buffer_size = GLOBAL_DEF("core/message_queue_size_kb", DEFAULT_QUEUE_SIZE_KB);
  275. buffer_size *= 1024;
  276. buffer = memnew_arr(uint8_t, buffer_size);
  277. }
  278. MessageQueue::~MessageQueue() {
  279. uint32_t read_pos = 0;
  280. while (read_pos < buffer_end) {
  281. Message *message = (Message *)&buffer[read_pos];
  282. Variant *args = (Variant *)(message + 1);
  283. int argc = message->args;
  284. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION) {
  285. for (int i = 0; i < argc; i++)
  286. args[i].~Variant();
  287. }
  288. message->~Message();
  289. read_pos += sizeof(Message);
  290. if ((message->type & FLAG_MASK) != TYPE_NOTIFICATION)
  291. read_pos += sizeof(Variant) * message->args;
  292. }
  293. singleton = NULL;
  294. memdelete_arr(buffer);
  295. }