physics_2d_server_wrap_mt.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "os/os.h"
  32. void Physics2DServerWrapMT::thread_exit() {
  33. exit = true;
  34. }
  35. void Physics2DServerWrapMT::thread_step(float 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(float 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. print_line("Creating physics 2D thread");
  84. //OS::get_singleton()->release_rendering_thread();
  85. if (create_thread) {
  86. thread = Thread::create(_thread_callback, this);
  87. print_line("Starting physics 2D thread");
  88. }
  89. while (!step_thread_up) {
  90. OS::get_singleton()->delay_usec(1000);
  91. }
  92. print_line("Done physics 2D thread");
  93. } else {
  94. physics_2d_server->init();
  95. }
  96. }
  97. void Physics2DServerWrapMT::finish() {
  98. if (thread) {
  99. command_queue.push(this, &Physics2DServerWrapMT::thread_exit);
  100. Thread::wait_to_finish(thread);
  101. memdelete(thread);
  102. /*
  103. shape_free_cached_ids();
  104. area_free_cached_ids();
  105. body_free_cached_ids();
  106. pin_joint_free_cached_ids();
  107. groove_joint_free_cached_ids();
  108. damped_string_free_cached_ids();
  109. */
  110. thread = NULL;
  111. } else {
  112. physics_2d_server->finish();
  113. }
  114. if (step_sem)
  115. memdelete(step_sem);
  116. }
  117. Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool p_create_thread) :
  118. command_queue(p_create_thread) {
  119. physics_2d_server = p_contained;
  120. create_thread = p_create_thread;
  121. thread = NULL;
  122. step_sem = NULL;
  123. step_pending = 0;
  124. step_thread_up = false;
  125. alloc_mutex = Mutex::create();
  126. shape_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
  127. area_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
  128. body_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
  129. pin_joint_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
  130. groove_joint_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
  131. damped_spring_joint_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
  132. if (!p_create_thread) {
  133. server_thread = Thread::get_caller_ID();
  134. } else {
  135. server_thread = 0;
  136. }
  137. main_thread = Thread::get_caller_ID();
  138. first_frame = true;
  139. }
  140. Physics2DServerWrapMT::~Physics2DServerWrapMT() {
  141. memdelete(physics_2d_server);
  142. memdelete(alloc_mutex);
  143. //finish();
  144. }