2016-06-18 20:46:12 +08:00
|
|
|
/**************************************************************************/
|
2020-03-28 20:19:05 +08:00
|
|
|
/* physics_server_2d_wrap_mt.cpp */
|
2016-06-18 20:46:12 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 07:50:27 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
#include "physics_server_2d_wrap_mt.h"
|
2015-05-26 12:05:08 +08:00
|
|
|
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/os/os.h"
|
2015-05-26 12:05:08 +08:00
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::thread_exit() {
|
2024-04-04 16:18:08 +08:00
|
|
|
exit = true;
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::thread_step(real_t p_delta) {
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->step(p_delta);
|
2020-03-03 16:26:42 +08:00
|
|
|
step_sem.post();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::thread_loop() {
|
2017-08-07 18:17:31 +08:00
|
|
|
server_thread = Thread::get_caller_id();
|
2015-05-26 12:05:08 +08:00
|
|
|
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->init();
|
2015-05-26 12:05:08 +08:00
|
|
|
|
2024-04-04 16:18:08 +08:00
|
|
|
command_queue.set_pump_task_id(server_task_id);
|
|
|
|
while (!exit) {
|
|
|
|
WorkerThreadPool::get_singleton()->yield();
|
|
|
|
command_queue.flush_all();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
|
2024-04-04 16:18:08 +08:00
|
|
|
command_queue.flush_all();
|
2015-05-26 12:05:08 +08:00
|
|
|
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->finish();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EVENT QUEUING */
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::step(real_t p_step) {
|
2015-05-26 12:05:08 +08:00
|
|
|
if (create_thread) {
|
2020-03-28 02:21:27 +08:00
|
|
|
command_queue.push(this, &PhysicsServer2DWrapMT::thread_step, p_step);
|
2015-05-26 12:05:08 +08:00
|
|
|
} else {
|
2024-04-04 16:18:08 +08:00
|
|
|
command_queue.flush_all(); // Flush all pending from other threads.
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->step(p_step);
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::sync() {
|
2021-01-19 20:29:41 +08:00
|
|
|
if (create_thread) {
|
2024-04-04 16:18:08 +08:00
|
|
|
step_sem.wait();
|
2015-05-26 12:30:36 +08:00
|
|
|
}
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->sync();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::flush_queries() {
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->flush_queries();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::end_sync() {
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->end_sync();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::init() {
|
2015-05-26 12:05:08 +08:00
|
|
|
if (create_thread) {
|
2024-04-04 16:18:08 +08:00
|
|
|
exit = false;
|
|
|
|
server_task_id = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &PhysicsServer2DWrapMT::thread_loop), true);
|
|
|
|
step_sem.post();
|
2015-05-26 12:05:08 +08:00
|
|
|
} else {
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->init();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
void PhysicsServer2DWrapMT::finish() {
|
2024-04-04 16:18:08 +08:00
|
|
|
if (create_thread) {
|
2020-03-28 02:21:27 +08:00
|
|
|
command_queue.push(this, &PhysicsServer2DWrapMT::thread_exit);
|
2024-04-04 16:18:08 +08:00
|
|
|
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;
|
|
|
|
}
|
2015-05-26 12:05:08 +08:00
|
|
|
} else {
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d->finish();
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 16:18:08 +08:00
|
|
|
PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) {
|
2022-04-27 11:52:53 +08:00
|
|
|
physics_server_2d = p_contained;
|
2015-05-26 12:05:08 +08:00
|
|
|
create_thread = p_create_thread;
|
2024-04-04 16:18:08 +08:00
|
|
|
if (!create_thread) {
|
|
|
|
server_thread = Thread::MAIN_ID;
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 02:21:27 +08:00
|
|
|
PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() {
|
2022-04-27 11:52:53 +08:00
|
|
|
memdelete(physics_server_2d);
|
2015-05-26 12:05:08 +08:00
|
|
|
}
|