physics_2d_server_wrap_mt.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 = true;
  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. OS::get_singleton()->make_rendering_thread();
  46. physics_2d_server->init();
  47. exit = false;
  48. step_thread_up = true;
  49. while (!exit) {
  50. // flush commands one by one, until exit is requested
  51. command_queue.wait_and_flush_one();
  52. }
  53. command_queue.flush_all(); // flush all
  54. physics_2d_server->finish();
  55. }
  56. /* EVENT QUEUING */
  57. void Physics2DServerWrapMT::step(real_t p_step) {
  58. if (create_thread) {
  59. command_queue.push(this, &Physics2DServerWrapMT::thread_step, p_step);
  60. } else {
  61. command_queue.flush_all(); //flush all pending from other threads
  62. physics_2d_server->step(p_step);
  63. }
  64. }
  65. void Physics2DServerWrapMT::sync() {
  66. if (step_sem) {
  67. if (first_frame)
  68. first_frame = false;
  69. else
  70. step_sem->wait(); //must not wait if a step was not issued
  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. step_sem = Semaphore::create();
  83. //OS::get_singleton()->release_rendering_thread();
  84. if (create_thread) {
  85. thread = Thread::create(_thread_callback, this);
  86. }
  87. while (!step_thread_up) {
  88. OS::get_singleton()->delay_usec(1000);
  89. }
  90. } else {
  91. physics_2d_server->init();
  92. }
  93. }
  94. void Physics2DServerWrapMT::finish() {
  95. if (thread) {
  96. command_queue.push(this, &Physics2DServerWrapMT::thread_exit);
  97. Thread::wait_to_finish(thread);
  98. memdelete(thread);
  99. thread = NULL;
  100. } else {
  101. physics_2d_server->finish();
  102. }
  103. line_shape_free_cached_ids();
  104. ray_shape_free_cached_ids();
  105. segment_shape_free_cached_ids();
  106. circle_shape_free_cached_ids();
  107. rectangle_shape_free_cached_ids();
  108. convex_polygon_shape_free_cached_ids();
  109. concave_polygon_shape_free_cached_ids();
  110. space_free_cached_ids();
  111. area_free_cached_ids();
  112. body_free_cached_ids();
  113. if (step_sem)
  114. memdelete(step_sem);
  115. }
  116. Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool p_create_thread) :
  117. command_queue(p_create_thread) {
  118. physics_2d_server = p_contained;
  119. create_thread = p_create_thread;
  120. thread = NULL;
  121. step_sem = NULL;
  122. step_pending = 0;
  123. step_thread_up = false;
  124. alloc_mutex = Mutex::create();
  125. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  126. if (!p_create_thread) {
  127. server_thread = Thread::get_caller_id();
  128. } else {
  129. server_thread = 0;
  130. }
  131. main_thread = Thread::get_caller_id();
  132. first_frame = true;
  133. }
  134. Physics2DServerWrapMT::~Physics2DServerWrapMT() {
  135. memdelete(physics_2d_server);
  136. memdelete(alloc_mutex);
  137. //finish();
  138. }