File tree Expand file tree Collapse file tree 6 files changed +103
-66
lines changed Expand file tree Collapse file tree 6 files changed +103
-66
lines changed Original file line number Diff line number Diff line change @@ -42,10 +42,17 @@ void PhysicsServer2DWrapMT::_thread_exit() {
4242void PhysicsServer2DWrapMT::_thread_loop () {
4343 while (!exit) {
4444 WorkerThreadPool::get_singleton ()->yield ();
45- command_queue.flush_all ();
45+
46+ if (!doing_sync.is_set ()) {
47+ command_queue.flush_all ();
48+ }
4649 }
4750}
4851
52+ void PhysicsServer2DWrapMT::_thread_sync () {
53+ doing_sync.set ();
54+ }
55+
4956/* EVENT QUEUING */
5057
5158void PhysicsServer2DWrapMT::step (real_t p_step) {
@@ -58,7 +65,7 @@ void PhysicsServer2DWrapMT::step(real_t p_step) {
5865
5966void PhysicsServer2DWrapMT::sync () {
6067 if (create_thread) {
61- command_queue.sync ( );
68+ command_queue.push_and_sync ( this , &PhysicsServer2DWrapMT::_thread_sync );
6269 } else {
6370 command_queue.flush_all (); // Flush all pending from other threads.
6471 }
@@ -71,6 +78,10 @@ void PhysicsServer2DWrapMT::flush_queries() {
7178
7279void PhysicsServer2DWrapMT::end_sync () {
7380 physics_server_2d->end_sync ();
81+
82+ if (create_thread) {
83+ doing_sync.clear ();
84+ }
7485}
7586
7687void PhysicsServer2DWrapMT::init () {
Original file line number Diff line number Diff line change 3535#include " core/templates/command_queue_mt.h"
3636#include " servers/physics_server_2d.h"
3737
38+ #define ASYNC_COND_PUSH (Thread::get_caller_id() != server_thread)
39+ #define ASYNC_COND_PUSH_AND_RET (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))
40+ #define ASYNC_COND_PUSH_AND_SYNC (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))
41+
3842#ifdef DEBUG_SYNC
3943#define SYNC_DEBUG print_line (" sync on: " + String(__FUNCTION__));
4044#else
@@ -58,10 +62,12 @@ class PhysicsServer2DWrapMT : public PhysicsServer2D {
5862 WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;
5963 bool exit = false ;
6064 bool create_thread = false ;
65+ SafeFlag doing_sync;
6166
6267 void _assign_mt_ids (WorkerThreadPool::TaskID p_pump_task_id);
6368 void _thread_exit ();
6469 void _thread_loop ();
70+ void _thread_sync ();
6571
6672public:
6773#define ServerName PhysicsServer2D
Original file line number Diff line number Diff line change @@ -42,10 +42,17 @@ void PhysicsServer3DWrapMT::_thread_exit() {
4242void PhysicsServer3DWrapMT::_thread_loop () {
4343 while (!exit) {
4444 WorkerThreadPool::get_singleton ()->yield ();
45- command_queue.flush_all ();
45+
46+ if (!doing_sync.is_set ()) {
47+ command_queue.flush_all ();
48+ }
4649 }
4750}
4851
52+ void PhysicsServer3DWrapMT::_thread_sync () {
53+ doing_sync.set ();
54+ }
55+
4956/* EVENT QUEUING */
5057
5158void PhysicsServer3DWrapMT::step (real_t p_step) {
@@ -58,7 +65,7 @@ void PhysicsServer3DWrapMT::step(real_t p_step) {
5865
5966void PhysicsServer3DWrapMT::sync () {
6067 if (create_thread) {
61- command_queue.sync ( );
68+ command_queue.push_and_sync ( this , &PhysicsServer3DWrapMT::_thread_sync );
6269 } else {
6370 command_queue.flush_all (); // Flush all pending from other threads.
6471 }
@@ -71,6 +78,10 @@ void PhysicsServer3DWrapMT::flush_queries() {
7178
7279void PhysicsServer3DWrapMT::end_sync () {
7380 physics_server_3d->end_sync ();
81+
82+ if (create_thread) {
83+ doing_sync.clear ();
84+ }
7485}
7586
7687void PhysicsServer3DWrapMT::init () {
Original file line number Diff line number Diff line change 3636#include " core/templates/command_queue_mt.h"
3737#include " servers/physics_server_3d.h"
3838
39+ #define ASYNC_COND_PUSH (Thread::get_caller_id() != server_thread)
40+ #define ASYNC_COND_PUSH_AND_RET (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))
41+ #define ASYNC_COND_PUSH_AND_SYNC (Thread::get_caller_id() != server_thread && !(doing_sync.is_set() && Thread::is_main_thread()))
42+
3943#ifdef DEBUG_SYNC
4044#define SYNC_DEBUG print_line (" sync on: " + String(__FUNCTION__));
4145#else
@@ -59,11 +63,13 @@ class PhysicsServer3DWrapMT : public PhysicsServer3D {
5963 WorkerThreadPool::TaskID server_task_id = WorkerThreadPool::INVALID_TASK_ID;
6064 bool exit = false ;
6165 bool create_thread = false ;
66+ SafeFlag doing_sync;
6267
6368 void _assign_mt_ids (WorkerThreadPool::TaskID p_pump_task_id);
6469 void _thread_exit ();
6570 void _thread_step (real_t p_delta);
6671 void _thread_loop ();
72+ void _thread_sync ();
6773
6874public:
6975#define ServerName PhysicsServer3D
Original file line number Diff line number Diff line change @@ -109,6 +109,9 @@ class RenderingServerDefault : public RenderingServer {
109109#endif
110110
111111#define WRITE_ACTION redraw_request ();
112+ #define ASYNC_COND_PUSH (Thread::get_caller_id() != server_thread)
113+ #define ASYNC_COND_PUSH_AND_RET (Thread::get_caller_id() != server_thread)
114+ #define ASYNC_COND_PUSH_AND_SYNC (Thread::get_caller_id() != server_thread)
112115
113116#ifdef DEBUG_SYNC
114117#define SYNC_DEBUG print_line (" sync on: " + String(__FUNCTION__));
You can’t perform that action at this time.
0 commit comments