3232
3333#include " core/os/os.h"
3434
35- void PhysicsServer2DWrapMT::thread_exit () {
36- exit = true ;
35+ void PhysicsServer2DWrapMT::_assign_mt_ids (WorkerThreadPool::TaskID p_pump_task_id) {
36+ server_thread = Thread::get_caller_id ();
37+ server_task_id = p_pump_task_id;
3738}
3839
39- void PhysicsServer2DWrapMT::thread_step (real_t p_delta) {
40- physics_server_2d->step (p_delta);
41- step_sem.post ();
40+ void PhysicsServer2DWrapMT::_thread_exit () {
41+ exit = true ;
4242}
4343
44- void PhysicsServer2DWrapMT::thread_loop () {
45- server_thread = Thread::get_caller_id ();
46-
47- physics_server_2d->init ();
48-
49- command_queue.set_pump_task_id (server_task_id);
44+ void PhysicsServer2DWrapMT::_thread_loop () {
5045 while (!exit) {
5146 WorkerThreadPool::get_singleton ()->yield ();
5247 command_queue.flush_all ();
5348 }
54-
55- command_queue.flush_all ();
56-
57- physics_server_2d->finish ();
5849}
5950
6051/* EVENT QUEUING */
6152
6253void PhysicsServer2DWrapMT::step (real_t p_step) {
6354 if (create_thread) {
64- command_queue.push (this , &PhysicsServer2DWrapMT::thread_step , p_step);
55+ command_queue.push (physics_server_2d , &PhysicsServer2D::step , p_step);
6556 } else {
66- command_queue.flush_all (); // Flush all pending from other threads.
6757 physics_server_2d->step (p_step);
6858 }
6959}
7060
7161void PhysicsServer2DWrapMT::sync () {
7262 if (create_thread) {
73- step_sem.wait ();
63+ command_queue.sync ();
64+ } else {
65+ command_queue.flush_all (); // Flush all pending from other threads.
7466 }
7567 physics_server_2d->sync ();
7668}
@@ -85,21 +77,26 @@ void PhysicsServer2DWrapMT::end_sync() {
8577
8678void PhysicsServer2DWrapMT::init () {
8779 if (create_thread) {
88- exit = false ;
89- server_task_id = WorkerThreadPool::get_singleton ()->add_task (callable_mp (this , &PhysicsServer2DWrapMT::thread_loop), true );
90- step_sem.post ();
80+ WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton ()->add_task (callable_mp (this , &PhysicsServer2DWrapMT::_thread_loop), true );
81+ command_queue.set_pump_task_id (tid);
82+ command_queue.push (this , &PhysicsServer2DWrapMT::_assign_mt_ids, tid);
83+ command_queue.push_and_sync (physics_server_2d, &PhysicsServer2D::init);
84+ DEV_ASSERT (server_task_id == tid);
9185 } else {
86+ server_thread = Thread::MAIN_ID;
9287 physics_server_2d->init ();
9388 }
9489}
9590
9691void PhysicsServer2DWrapMT::finish () {
9792 if (create_thread) {
98- command_queue.push (this , &PhysicsServer2DWrapMT::thread_exit);
93+ command_queue.push (physics_server_2d, &PhysicsServer2D::finish);
94+ command_queue.push (this , &PhysicsServer2DWrapMT::_thread_exit);
9995 if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {
10096 WorkerThreadPool::get_singleton ()->wait_for_task_completion (server_task_id);
10197 server_task_id = WorkerThreadPool::INVALID_TASK_ID;
10298 }
99+ server_thread = Thread::MAIN_ID;
103100 } else {
104101 physics_server_2d->finish ();
105102 }
@@ -108,9 +105,6 @@ void PhysicsServer2DWrapMT::finish() {
108105PhysicsServer2DWrapMT::PhysicsServer2DWrapMT (PhysicsServer2D *p_contained, bool p_create_thread) {
109106 physics_server_2d = p_contained;
110107 create_thread = p_create_thread;
111- if (!create_thread) {
112- server_thread = Thread::MAIN_ID;
113- }
114108}
115109
116110PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT () {
0 commit comments