Apply additional fixes to servers' threading

This commit is contained in:
Pedro J. Estébanez
2024-04-23 12:01:23 +02:00
parent 6f0760beb3
commit 1589433e8f
10 changed files with 144 additions and 143 deletions

View File

@ -32,45 +32,37 @@
#include "core/os/os.h"
void PhysicsServer2DWrapMT::thread_exit() {
void PhysicsServer2DWrapMT::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {
server_thread = Thread::get_caller_id();
server_task_id = p_pump_task_id;
}
void PhysicsServer2DWrapMT::_thread_exit() {
exit = true;
}
void PhysicsServer2DWrapMT::thread_step(real_t p_delta) {
physics_server_2d->step(p_delta);
step_sem.post();
}
void PhysicsServer2DWrapMT::thread_loop() {
server_thread = Thread::get_caller_id();
physics_server_2d->init();
command_queue.set_pump_task_id(server_task_id);
void PhysicsServer2DWrapMT::_thread_loop() {
while (!exit) {
WorkerThreadPool::get_singleton()->yield();
command_queue.flush_all();
}
command_queue.flush_all();
physics_server_2d->finish();
}
/* EVENT QUEUING */
void PhysicsServer2DWrapMT::step(real_t p_step) {
if (create_thread) {
command_queue.push(this, &PhysicsServer2DWrapMT::thread_step, p_step);
command_queue.push(physics_server_2d, &PhysicsServer2D::step, p_step);
} else {
command_queue.flush_all(); // Flush all pending from other threads.
physics_server_2d->step(p_step);
}
}
void PhysicsServer2DWrapMT::sync() {
if (create_thread) {
step_sem.wait();
command_queue.sync();
} else {
command_queue.flush_all(); // Flush all pending from other threads.
}
physics_server_2d->sync();
}
@ -85,21 +77,26 @@ void PhysicsServer2DWrapMT::end_sync() {
void PhysicsServer2DWrapMT::init() {
if (create_thread) {
exit = false;
server_task_id = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &PhysicsServer2DWrapMT::thread_loop), true);
step_sem.post();
WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &PhysicsServer2DWrapMT::_thread_loop), true);
command_queue.set_pump_task_id(tid);
command_queue.push(this, &PhysicsServer2DWrapMT::_assign_mt_ids, tid);
command_queue.push_and_sync(physics_server_2d, &PhysicsServer2D::init);
DEV_ASSERT(server_task_id == tid);
} else {
server_thread = Thread::MAIN_ID;
physics_server_2d->init();
}
}
void PhysicsServer2DWrapMT::finish() {
if (create_thread) {
command_queue.push(this, &PhysicsServer2DWrapMT::thread_exit);
command_queue.push(physics_server_2d, &PhysicsServer2D::finish);
command_queue.push(this, &PhysicsServer2DWrapMT::_thread_exit);
if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {
WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);
server_task_id = WorkerThreadPool::INVALID_TASK_ID;
}
server_thread = Thread::MAIN_ID;
} else {
physics_server_2d->finish();
}
@ -108,9 +105,6 @@ void PhysicsServer2DWrapMT::finish() {
PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) {
physics_server_2d = p_contained;
create_thread = p_create_thread;
if (!create_thread) {
server_thread = Thread::MAIN_ID;
}
}
PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() {