2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2020-03-28 13:19:05 +01:00
|
|
|
/* physics_server_2d_wrap_mt.cpp */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* 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 00:50:27 +01:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
#include "physics_server_2d_wrap_mt.h"
|
2015-05-26 06:05:08 +02:00
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/os.h"
|
2015-05-26 06:05:08 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::thread_exit() {
|
2017-03-05 16:44:50 +01:00
|
|
|
exit = true;
|
2015-05-26 06:05:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::thread_step(real_t p_delta) {
|
2015-05-26 06:05:08 +02:00
|
|
|
physics_2d_server->step(p_delta);
|
2020-03-03 09:26:42 +01:00
|
|
|
step_sem.post();
|
2015-05-26 06:05:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::_thread_callback(void *_instance) {
|
|
|
|
PhysicsServer2DWrapMT *vsmt = reinterpret_cast<PhysicsServer2DWrapMT *>(_instance);
|
2015-05-26 06:05:08 +02:00
|
|
|
|
|
|
|
vsmt->thread_loop();
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::thread_loop() {
|
2017-08-07 12:17:31 +02:00
|
|
|
server_thread = Thread::get_caller_id();
|
2015-05-26 06:05:08 +02:00
|
|
|
|
|
|
|
physics_2d_server->init();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
exit = false;
|
|
|
|
step_thread_up = true;
|
|
|
|
while (!exit) {
|
2015-05-26 06:05:08 +02:00
|
|
|
// flush commands one by one, until exit is requested
|
|
|
|
command_queue.wait_and_flush_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
command_queue.flush_all(); // flush all
|
|
|
|
|
|
|
|
physics_2d_server->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EVENT QUEUING */
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::step(real_t p_step) {
|
2015-05-26 06:05:08 +02:00
|
|
|
if (create_thread) {
|
2020-03-27 19:21:27 +01:00
|
|
|
command_queue.push(this, &PhysicsServer2DWrapMT::thread_step, p_step);
|
2015-05-26 06:05:08 +02:00
|
|
|
} else {
|
|
|
|
command_queue.flush_all(); //flush all pending from other threads
|
|
|
|
physics_2d_server->step(p_step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::sync() {
|
2020-03-03 09:26:42 +01:00
|
|
|
if (thread) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (first_frame) {
|
2017-03-05 16:44:50 +01:00
|
|
|
first_frame = false;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2020-03-03 09:26:42 +01:00
|
|
|
step_sem.wait(); //must not wait if a step was not issued
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2015-05-26 06:30:36 +02:00
|
|
|
}
|
2017-01-14 18:03:38 +01:00
|
|
|
physics_2d_server->sync();
|
2015-05-26 06:05:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::flush_queries() {
|
2015-05-26 06:05:08 +02:00
|
|
|
physics_2d_server->flush_queries();
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::end_sync() {
|
2017-01-14 18:03:38 +01:00
|
|
|
physics_2d_server->end_sync();
|
2015-05-26 06:05:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::init() {
|
2015-05-26 06:05:08 +02:00
|
|
|
if (create_thread) {
|
|
|
|
//OS::get_singleton()->release_rendering_thread();
|
2020-03-03 09:26:42 +01:00
|
|
|
thread = Thread::create(_thread_callback, this);
|
2017-03-05 16:44:50 +01:00
|
|
|
while (!step_thread_up) {
|
2015-05-26 06:05:08 +02:00
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
physics_2d_server->init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void PhysicsServer2DWrapMT::finish() {
|
2015-05-26 06:05:08 +02:00
|
|
|
if (thread) {
|
2020-03-27 19:21:27 +01:00
|
|
|
command_queue.push(this, &PhysicsServer2DWrapMT::thread_exit);
|
2017-03-05 16:44:50 +01:00
|
|
|
Thread::wait_to_finish(thread);
|
2015-05-26 06:05:08 +02:00
|
|
|
memdelete(thread);
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
thread = nullptr;
|
2015-05-26 06:05:08 +02:00
|
|
|
} else {
|
|
|
|
physics_2d_server->finish();
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:34:01 +01:00
|
|
|
line_shape_free_cached_ids();
|
|
|
|
ray_shape_free_cached_ids();
|
|
|
|
segment_shape_free_cached_ids();
|
|
|
|
circle_shape_free_cached_ids();
|
|
|
|
rectangle_shape_free_cached_ids();
|
2019-10-24 20:12:46 +02:00
|
|
|
capsule_shape_free_cached_ids();
|
2017-11-10 03:34:01 +01:00
|
|
|
convex_polygon_shape_free_cached_ids();
|
|
|
|
concave_polygon_shape_free_cached_ids();
|
|
|
|
|
|
|
|
space_free_cached_ids();
|
|
|
|
area_free_cached_ids();
|
|
|
|
body_free_cached_ids();
|
2015-05-26 06:05:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) :
|
2017-12-06 21:36:34 +01:00
|
|
|
command_queue(p_create_thread) {
|
2017-03-05 16:44:50 +01:00
|
|
|
physics_2d_server = p_contained;
|
|
|
|
create_thread = p_create_thread;
|
2020-04-02 01:20:12 +02:00
|
|
|
thread = nullptr;
|
2017-03-05 16:44:50 +01:00
|
|
|
step_pending = 0;
|
|
|
|
step_thread_up = false;
|
2015-05-26 06:05:08 +02:00
|
|
|
|
2017-11-10 03:34:01 +01:00
|
|
|
pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc");
|
2015-05-26 06:05:08 +02:00
|
|
|
|
|
|
|
if (!p_create_thread) {
|
2017-08-07 12:17:31 +02:00
|
|
|
server_thread = Thread::get_caller_id();
|
2015-05-26 06:05:08 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
server_thread = 0;
|
2015-05-26 06:05:08 +02:00
|
|
|
}
|
2015-05-26 06:30:36 +02:00
|
|
|
|
2017-08-07 12:17:31 +02:00
|
|
|
main_thread = Thread::get_caller_id();
|
2017-03-05 16:44:50 +01:00
|
|
|
first_frame = true;
|
2015-05-26 06:05:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() {
|
2015-05-26 06:05:08 +02:00
|
|
|
memdelete(physics_2d_server);
|
|
|
|
//finish();
|
|
|
|
}
|