2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* physics_2d_server_wrap_mt.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00: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. */
|
|
|
|
/*************************************************************************/
|
2015-05-26 04:05:08 +00:00
|
|
|
#include "physics_2d_server_wrap_mt.h"
|
|
|
|
|
|
|
|
#include "os/os.h"
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::thread_exit() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
exit = true;
|
2015-05-26 04:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::thread_step(float p_delta) {
|
|
|
|
|
|
|
|
physics_2d_server->step(p_delta);
|
|
|
|
step_sem->post();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::_thread_callback(void *_instance) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Physics2DServerWrapMT *vsmt = reinterpret_cast<Physics2DServerWrapMT *>(_instance);
|
2015-05-26 04:05:08 +00:00
|
|
|
|
|
|
|
vsmt->thread_loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::thread_loop() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
server_thread = Thread::get_caller_ID();
|
2015-05-26 04:05:08 +00:00
|
|
|
|
|
|
|
OS::get_singleton()->make_rendering_thread();
|
|
|
|
|
|
|
|
physics_2d_server->init();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
exit = false;
|
|
|
|
step_thread_up = true;
|
|
|
|
while (!exit) {
|
2015-05-26 04:05:08 +00: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 */
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::step(float p_step) {
|
|
|
|
|
|
|
|
if (create_thread) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
command_queue.push(this, &Physics2DServerWrapMT::thread_step, p_step);
|
2015-05-26 04:05:08 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
command_queue.flush_all(); //flush all pending from other threads
|
|
|
|
physics_2d_server->step(p_step);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::sync() {
|
|
|
|
|
2015-05-26 04:30:36 +00:00
|
|
|
if (step_sem) {
|
|
|
|
if (first_frame)
|
2017-03-18 23:36:26 +00:00
|
|
|
first_frame = false;
|
2015-05-26 04:30:36 +00:00
|
|
|
else
|
|
|
|
step_sem->wait(); //must not wait if a step was not issued
|
|
|
|
}
|
2017-01-14 17:03:38 +00:00
|
|
|
physics_2d_server->sync();
|
2015-05-26 04:05:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Physics2DServerWrapMT::flush_queries() {
|
2015-05-26 04:05:08 +00:00
|
|
|
|
|
|
|
physics_2d_server->flush_queries();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::end_sync() {
|
|
|
|
|
2017-01-14 17:03:38 +00:00
|
|
|
physics_2d_server->end_sync();
|
2015-05-26 04:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::init() {
|
|
|
|
|
|
|
|
if (create_thread) {
|
|
|
|
|
|
|
|
step_sem = Semaphore::create();
|
2017-12-11 15:59:59 +00:00
|
|
|
print_line("Creating physics 2D thread");
|
2015-05-26 04:05:08 +00:00
|
|
|
//OS::get_singleton()->release_rendering_thread();
|
|
|
|
if (create_thread) {
|
2017-03-18 23:36:26 +00:00
|
|
|
thread = Thread::create(_thread_callback, this);
|
2017-12-11 15:59:59 +00:00
|
|
|
print_line("Starting physics 2D thread");
|
2015-05-26 04:05:08 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
while (!step_thread_up) {
|
2015-05-26 04:05:08 +00:00
|
|
|
OS::get_singleton()->delay_usec(1000);
|
|
|
|
}
|
2017-12-11 15:59:59 +00:00
|
|
|
print_line("Done physics 2D thread");
|
2015-05-26 04:05:08 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
physics_2d_server->init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics2DServerWrapMT::finish() {
|
|
|
|
|
|
|
|
if (thread) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
command_queue.push(this, &Physics2DServerWrapMT::thread_exit);
|
|
|
|
Thread::wait_to_finish(thread);
|
2015-05-26 04:05:08 +00:00
|
|
|
memdelete(thread);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
/*
|
2015-05-26 04:05:08 +00:00
|
|
|
shape_free_cached_ids();
|
|
|
|
area_free_cached_ids();
|
|
|
|
body_free_cached_ids();
|
|
|
|
pin_joint_free_cached_ids();
|
|
|
|
groove_joint_free_cached_ids();
|
|
|
|
damped_string_free_cached_ids();
|
|
|
|
*/
|
2017-03-18 23:36:26 +00:00
|
|
|
thread = NULL;
|
2015-05-26 04:05:08 +00:00
|
|
|
} else {
|
|
|
|
physics_2d_server->finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (step_sem)
|
|
|
|
memdelete(step_sem);
|
|
|
|
}
|
|
|
|
|
2017-12-11 14:31:49 +00:00
|
|
|
Physics2DServerWrapMT::Physics2DServerWrapMT(Physics2DServer *p_contained, bool p_create_thread) :
|
|
|
|
command_queue(p_create_thread) {
|
2015-05-26 04:05:08 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
physics_2d_server = p_contained;
|
|
|
|
create_thread = p_create_thread;
|
|
|
|
thread = NULL;
|
|
|
|
step_sem = NULL;
|
|
|
|
step_pending = 0;
|
|
|
|
step_thread_up = false;
|
|
|
|
alloc_mutex = Mutex::create();
|
2015-05-26 04:05:08 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
shape_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
|
|
|
|
area_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
|
|
|
|
body_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
|
|
|
|
pin_joint_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
|
|
|
|
groove_joint_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
|
|
|
|
damped_spring_joint_pool_max_size = GLOBAL_DEF("core/thread_rid_pool_prealloc", 20);
|
2015-05-26 04:05:08 +00:00
|
|
|
|
|
|
|
if (!p_create_thread) {
|
2017-03-18 23:36:26 +00:00
|
|
|
server_thread = Thread::get_caller_ID();
|
2015-05-26 04:05:08 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
server_thread = 0;
|
2015-05-26 04:05:08 +00:00
|
|
|
}
|
2015-05-26 04:30:36 +00:00
|
|
|
|
|
|
|
main_thread = Thread::get_caller_ID();
|
2017-03-18 23:36:26 +00:00
|
|
|
first_frame = true;
|
2015-05-26 04:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Physics2DServerWrapMT::~Physics2DServerWrapMT() {
|
|
|
|
|
|
|
|
memdelete(physics_2d_server);
|
|
|
|
memdelete(alloc_mutex);
|
|
|
|
//finish();
|
|
|
|
}
|