Skip to content
0.2.0-dev Download ZIP
123 lines ·4.6 KB Raw
1/**************************************************************************/
2/* physics_server_3d_wrap_mt.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "physics_server_3d_wrap_mt.h"
32
33#include "core/object/callable_mp.h"
34
35void PhysicsServer3DWrapMT::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {
36 server_thread = Thread::get_caller_id();
37 server_task_id = p_pump_task_id;
38}
39
40void PhysicsServer3DWrapMT::_thread_exit() {
41 exit = true;
42}
43
44void PhysicsServer3DWrapMT::_thread_loop() {
45 while (!exit) {
46 WorkerThreadPool::get_singleton()->yield();
47
48 if (!doing_sync.is_set()) {
49 command_queue.flush_all();
50 }
51 }
52}
53
54void PhysicsServer3DWrapMT::_thread_sync() {
55 doing_sync.set();
56}
57
58/* EVENT QUEUING */
59
60void PhysicsServer3DWrapMT::step(real_t p_step) {
61 if (create_thread) {
62 command_queue.push(physics_server_3d, &PhysicsServer3D::step, p_step);
63 } else {
64 physics_server_3d->step(p_step);
65 }
66}
67
68void PhysicsServer3DWrapMT::sync() {
69 if (create_thread) {
70 command_queue.push_and_sync(this, &PhysicsServer3DWrapMT::_thread_sync);
71 } else {
72 command_queue.flush_all(); // Flush all pending from other threads.
73 }
74 physics_server_3d->sync();
75}
76
77void PhysicsServer3DWrapMT::flush_queries() {
78 physics_server_3d->flush_queries();
79}
80
81void PhysicsServer3DWrapMT::end_sync() {
82 physics_server_3d->end_sync();
83
84 if (create_thread) {
85 doing_sync.clear();
86 }
87}
88
89void PhysicsServer3DWrapMT::init() {
90 if (create_thread) {
91 WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &PhysicsServer3DWrapMT::_thread_loop), true, "Physics server 3D pump task", true);
92 command_queue.set_pump_task_id(tid);
93 command_queue.push(this, &PhysicsServer3DWrapMT::_assign_mt_ids, tid);
94 command_queue.push_and_sync(physics_server_3d, &PhysicsServer3D::init);
95 DEV_ASSERT(server_task_id == tid);
96 } else {
97 server_thread = Thread::MAIN_ID;
98 physics_server_3d->init();
99 }
100}
101
102void PhysicsServer3DWrapMT::finish() {
103 if (create_thread) {
104 command_queue.push(physics_server_3d, &PhysicsServer3D::finish);
105 command_queue.push(this, &PhysicsServer3DWrapMT::_thread_exit);
106 if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {
107 WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);
108 server_task_id = WorkerThreadPool::INVALID_TASK_ID;
109 }
110 server_thread = Thread::MAIN_ID;
111 } else {
112 physics_server_3d->finish();
113 }
114}
115
116PhysicsServer3DWrapMT::PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread) {
117 physics_server_3d = p_contained;
118 create_thread = p_create_thread;
119}
120
121PhysicsServer3DWrapMT::~PhysicsServer3DWrapMT() {
122 memdelete(physics_server_3d);
123}