physics_server_2d_wrap_mt.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {
  33. server_thread = Thread::get_caller_id();
  34. server_task_id = p_pump_task_id;
  35. }
  36. void PhysicsServer2DWrapMT::_thread_exit() {
  37. exit = true;
  38. }
  39. void PhysicsServer2DWrapMT::_thread_loop() {
  40. while (!exit) {
  41. WorkerThreadPool::get_singleton()->yield();
  42. command_queue.flush_all();
  43. }
  44. }
  45. /* EVENT QUEUING */
  46. void PhysicsServer2DWrapMT::step(real_t p_step) {
  47. if (create_thread) {
  48. command_queue.push(physics_server_2d, &PhysicsServer2D::step, p_step);
  49. } else {
  50. physics_server_2d->step(p_step);
  51. }
  52. }
  53. void PhysicsServer2DWrapMT::sync() {
  54. if (create_thread) {
  55. command_queue.sync();
  56. } else {
  57. command_queue.flush_all(); // Flush all pending from other threads.
  58. }
  59. physics_server_2d->sync();
  60. }
  61. void PhysicsServer2DWrapMT::flush_queries() {
  62. physics_server_2d->flush_queries();
  63. }
  64. void PhysicsServer2DWrapMT::end_sync() {
  65. physics_server_2d->end_sync();
  66. }
  67. void PhysicsServer2DWrapMT::init() {
  68. if (create_thread) {
  69. WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &PhysicsServer2DWrapMT::_thread_loop), true);
  70. command_queue.set_pump_task_id(tid);
  71. command_queue.push(this, &PhysicsServer2DWrapMT::_assign_mt_ids, tid);
  72. command_queue.push_and_sync(physics_server_2d, &PhysicsServer2D::init);
  73. DEV_ASSERT(server_task_id == tid);
  74. } else {
  75. server_thread = Thread::MAIN_ID;
  76. physics_server_2d->init();
  77. }
  78. }
  79. void PhysicsServer2DWrapMT::finish() {
  80. if (create_thread) {
  81. command_queue.push(physics_server_2d, &PhysicsServer2D::finish);
  82. command_queue.push(this, &PhysicsServer2DWrapMT::_thread_exit);
  83. if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {
  84. WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);
  85. server_task_id = WorkerThreadPool::INVALID_TASK_ID;
  86. }
  87. server_thread = Thread::MAIN_ID;
  88. } else {
  89. physics_server_2d->finish();
  90. }
  91. }
  92. PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) {
  93. physics_server_2d = p_contained;
  94. create_thread = p_create_thread;
  95. }
  96. PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() {
  97. memdelete(physics_server_2d);
  98. }