physics_server_2d_wrap_mt.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* physics_server_2d_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_server_2d_wrap_mt.h"
  31. #include "core/os/os.h"
  32. void PhysicsServer2DWrapMT::thread_exit() {
  33. exit.set();
  34. }
  35. void PhysicsServer2DWrapMT::thread_step(real_t p_delta) {
  36. physics_server_2d->step(p_delta);
  37. step_sem.post();
  38. }
  39. void PhysicsServer2DWrapMT::_thread_callback(void *_instance) {
  40. PhysicsServer2DWrapMT *vsmt = reinterpret_cast<PhysicsServer2DWrapMT *>(_instance);
  41. vsmt->thread_loop();
  42. }
  43. void PhysicsServer2DWrapMT::thread_loop() {
  44. server_thread = Thread::get_caller_id();
  45. physics_server_2d->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();
  51. }
  52. command_queue.flush_all(); // flush all
  53. physics_server_2d->finish();
  54. }
  55. /* EVENT QUEUING */
  56. void PhysicsServer2DWrapMT::step(real_t p_step) {
  57. if (create_thread) {
  58. command_queue.push(this, &PhysicsServer2DWrapMT::thread_step, p_step);
  59. } else {
  60. command_queue.flush_all(); //flush all pending from other threads
  61. physics_server_2d->step(p_step);
  62. }
  63. }
  64. void PhysicsServer2DWrapMT::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_server_2d->sync();
  73. }
  74. void PhysicsServer2DWrapMT::flush_queries() {
  75. physics_server_2d->flush_queries();
  76. }
  77. void PhysicsServer2DWrapMT::end_sync() {
  78. physics_server_2d->end_sync();
  79. }
  80. void PhysicsServer2DWrapMT::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_server_2d->init();
  89. }
  90. }
  91. void PhysicsServer2DWrapMT::finish() {
  92. if (thread.is_started()) {
  93. command_queue.push(this, &PhysicsServer2DWrapMT::thread_exit);
  94. thread.wait_to_finish();
  95. } else {
  96. physics_server_2d->finish();
  97. }
  98. }
  99. PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) :
  100. command_queue(p_create_thread) {
  101. physics_server_2d = p_contained;
  102. create_thread = p_create_thread;
  103. pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
  104. if (!p_create_thread) {
  105. server_thread = Thread::get_caller_id();
  106. } else {
  107. server_thread = 0;
  108. }
  109. main_thread = Thread::get_caller_id();
  110. }
  111. PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() {
  112. memdelete(physics_server_2d);
  113. //finish();
  114. }