physics_server_2d_wrap_mt.cpp 4.3 KB

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