physics_2d_server_wrap_mt.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*************************************************************************/
  2. /* physics_2d_server_wrap_mt.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "physics_2d_server_wrap_mt.h"
  31. #include "core/os/os.h"
  32. void Physics2DServerWrapMT::thread_exit() {
  33. exit.set();
  34. }
  35. void Physics2DServerWrapMT::thread_step(real_t p_delta) {
  36. physics_2d_server->step(p_delta);
  37. step_sem.post();
  38. }
  39. void Physics2DServerWrapMT::_thread_callback(void *_instance) {
  40. Physics2DServerWrapMT *vsmt = reinterpret_cast<Physics2DServerWrapMT *>(_instance);
  41. vsmt->thread_loop();
  42. }
  43. void Physics2DServerWrapMT::thread_loop() {
  44. server_thread = Thread::get_caller_id();
  45. physics_2d_server->init();
  46. exit.clear();
  47. step_thread_up.set();
  48. while (!exit.is_set()) {
  49. // flush commands one by one, until exit is requested
  50. command_queue.wait_and_flush_one();
  51. }
  52. command_queue.flush_all(); // flush all
  53. physics_2d_server->finish();
  54. }
  55. /* EVENT QUEUING */
  56. void Physics2DServerWrapMT::step(real_t p_step) {
  57. if (create_thread) {
  58. command_queue.push(this, &Physics2DServerWrapMT::thread_step, p_step);
  59. } else {
  60. command_queue.flush_all(); //flush all pending from other threads
  61. physics_2d_server->step(p_step);
  62. }
  63. }
  64. void Physics2DServerWrapMT::sync() {
  65. if (create_thread) {
  66. if (first_frame)
  67. first_frame = false;
  68. else
  69. step_sem.wait(); //must not wait if a step was not issued
  70. }
  71. physics_2d_server->sync();
  72. }
  73. void Physics2DServerWrapMT::flush_queries() {
  74. physics_2d_server->flush_queries();
  75. }
  76. void Physics2DServerWrapMT::end_sync() {
  77. physics_2d_server->end_sync();
  78. }
  79. void Physics2DServerWrapMT::init() {
  80. if (create_thread) {
  81. //OS::get_singleton()->release_rendering_thread();
  82. thread.start(_thread_callback, this);
  83. while (!step_thread_up.is_set()) {
  84. OS::get_singleton()->delay_usec(1000);
  85. }
  86. } else {
  87. physics_2d_server->init();
  88. }
  89. }
  90. void Physics2DServerWrapMT::finish() {
  91. if (create_thread) {
  92. command_queue.push(this, &Physics2DServerWrapMT::thread_exit);
  93. thread.wait_to_finish();
  94. } else {
  95. physics_2d_server->finish();
  96. }
  97. line_shape_free_cached_ids();
  98. ray_shape_free_cached_ids();
  99. segment_shape_free_cached_ids();
  100. circle_shape_free_cached_ids();
  101. rectangle_shape_free_cached_ids();
  102. capsule_shape_free_cached_ids();
  103. convex_polygon_shape_free_cached_ids();
  104. concave_polygon_shape_free_cached_ids();
  105. space_free_cached_ids();
  106. area_free_cached_ids();
  107. body_free_cached_ids();
  108. }
  109. Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool p_create_thread) :
  110. command_queue(p_create_thread) {
  111. physics_2d_server = p_contained;
  112. create_thread = p_create_thread;
  113. step_pending = 0;
  114. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  115. if (!p_create_thread) {
  116. server_thread = Thread::get_caller_id();
  117. } else {
  118. server_thread = 0;
  119. }
  120. main_thread = Thread::get_caller_id();
  121. first_frame = true;
  122. }
  123. Physics2DServerWrapMT::~Physics2DServerWrapMT() {
  124. memdelete(physics_2d_server);
  125. //finish();
  126. }