physics_2d_server_wrap_mt.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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) 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 "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. }
  72. physics_2d_server->sync();
  73. }
  74. void Physics2DServerWrapMT::flush_queries() {
  75. physics_2d_server->flush_queries();
  76. }
  77. void Physics2DServerWrapMT::end_sync() {
  78. physics_2d_server->end_sync();
  79. }
  80. void Physics2DServerWrapMT::init() {
  81. if (create_thread) {
  82. //OS::get_singleton()->release_rendering_thread();
  83. thread.start(_thread_callback, this);
  84. while (!step_thread_up.is_set()) {
  85. OS::get_singleton()->delay_usec(1000);
  86. }
  87. } else {
  88. physics_2d_server->init();
  89. }
  90. }
  91. void Physics2DServerWrapMT::finish() {
  92. if (create_thread) {
  93. command_queue.push(this, &Physics2DServerWrapMT::thread_exit);
  94. thread.wait_to_finish();
  95. } else {
  96. physics_2d_server->finish();
  97. }
  98. line_shape_free_cached_ids();
  99. ray_shape_free_cached_ids();
  100. segment_shape_free_cached_ids();
  101. circle_shape_free_cached_ids();
  102. rectangle_shape_free_cached_ids();
  103. capsule_shape_free_cached_ids();
  104. convex_polygon_shape_free_cached_ids();
  105. concave_polygon_shape_free_cached_ids();
  106. space_free_cached_ids();
  107. area_free_cached_ids();
  108. body_free_cached_ids();
  109. }
  110. Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool p_create_thread) :
  111. command_queue(p_create_thread) {
  112. physics_2d_server = p_contained;
  113. create_thread = p_create_thread;
  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. }