Skip to content

Commit c7a0761

Browse files
committed
Merge pull request #109591 from mihe/no-async-physics-while-processing
Pause physics command queue during physics processing
2 parents b947e5f + 70979ae commit c7a0761

File tree

6 files changed

+103
-66
lines changed

6 files changed

+103
-66
lines changed

servers/physics_server_2d_wrap_mt.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ void PhysicsServer2DWrapMT::_thread_exit() {
4242
void 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

5158
void PhysicsServer2DWrapMT::step(real_t p_step) {
@@ -58,7 +65,7 @@ void PhysicsServer2DWrapMT::step(real_t p_step) {
5865

5966
void 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

7279
void PhysicsServer2DWrapMT::end_sync() {
7380
physics_server_2d->end_sync();
81+
82+
if (create_thread) {
83+
doing_sync.clear();
84+
}
7485
}
7586

7687
void PhysicsServer2DWrapMT::init() {

servers/physics_server_2d_wrap_mt.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
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

6672
public:
6773
#define ServerName PhysicsServer2D

servers/physics_server_3d_wrap_mt.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,17 @@ void PhysicsServer3DWrapMT::_thread_exit() {
4242
void 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

5158
void PhysicsServer3DWrapMT::step(real_t p_step) {
@@ -58,7 +65,7 @@ void PhysicsServer3DWrapMT::step(real_t p_step) {
5865

5966
void 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

7279
void PhysicsServer3DWrapMT::end_sync() {
7380
physics_server_3d->end_sync();
81+
82+
if (create_thread) {
83+
doing_sync.clear();
84+
}
7485
}
7586

7687
void PhysicsServer3DWrapMT::init() {

servers/physics_server_3d_wrap_mt.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
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

6874
public:
6975
#define ServerName PhysicsServer3D

servers/rendering/rendering_server_default.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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__));

0 commit comments

Comments
 (0)