Path: blob/master/servers/physics_server_2d_wrap_mt.cpp
9887 views
/**************************************************************************/1/* physics_server_2d_wrap_mt.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "physics_server_2d_wrap_mt.h"3132void PhysicsServer2DWrapMT::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {33server_thread = Thread::get_caller_id();34server_task_id = p_pump_task_id;35}3637void PhysicsServer2DWrapMT::_thread_exit() {38exit = true;39}4041void PhysicsServer2DWrapMT::_thread_loop() {42while (!exit) {43WorkerThreadPool::get_singleton()->yield();4445if (!doing_sync.is_set()) {46command_queue.flush_all();47}48}49}5051void PhysicsServer2DWrapMT::_thread_sync() {52doing_sync.set();53}5455/* EVENT QUEUING */5657void PhysicsServer2DWrapMT::step(real_t p_step) {58if (create_thread) {59command_queue.push(physics_server_2d, &PhysicsServer2D::step, p_step);60} else {61physics_server_2d->step(p_step);62}63}6465void PhysicsServer2DWrapMT::sync() {66if (create_thread) {67command_queue.push_and_sync(this, &PhysicsServer2DWrapMT::_thread_sync);68} else {69command_queue.flush_all(); // Flush all pending from other threads.70}71physics_server_2d->sync();72}7374void PhysicsServer2DWrapMT::flush_queries() {75physics_server_2d->flush_queries();76}7778void PhysicsServer2DWrapMT::end_sync() {79physics_server_2d->end_sync();8081if (create_thread) {82doing_sync.clear();83}84}8586void PhysicsServer2DWrapMT::init() {87if (create_thread) {88WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &PhysicsServer2DWrapMT::_thread_loop), true, "Physics server 2D pump task", true);89command_queue.set_pump_task_id(tid);90command_queue.push(this, &PhysicsServer2DWrapMT::_assign_mt_ids, tid);91command_queue.push_and_sync(physics_server_2d, &PhysicsServer2D::init);92DEV_ASSERT(server_task_id == tid);93} else {94server_thread = Thread::MAIN_ID;95physics_server_2d->init();96}97}9899void PhysicsServer2DWrapMT::finish() {100if (create_thread) {101command_queue.push(physics_server_2d, &PhysicsServer2D::finish);102command_queue.push(this, &PhysicsServer2DWrapMT::_thread_exit);103if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {104WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);105server_task_id = WorkerThreadPool::INVALID_TASK_ID;106}107server_thread = Thread::MAIN_ID;108} else {109physics_server_2d->finish();110}111}112113PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) {114physics_server_2d = p_contained;115create_thread = p_create_thread;116}117118PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() {119memdelete(physics_server_2d);120}121122123