Debian Codegraph @

servers/physics_2d/physics_server_2d_wrap_mt.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* physics_server_2d_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_2d_wrap_mt.h"
32
33 void PhysicsServer2DWrapMT::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {
34 server_thread = Thread::get_caller_id();
35 server_task_id = p_pump_task_id;
36 }
37
38 void PhysicsServer2DWrapMT::_thread_exit() {
39 exit = true;
40 }
41
42 void PhysicsServer2DWrapMT::_thread_loop() {
43 while (!exit) {
44 WorkerThreadPool::get_singleton()->yield();
45
46 if (!doing_sync.is_set()) {
47 command_queue.flush_all();
48 }
49 }
50 }
51
52 void PhysicsServer2DWrapMT::_thread_sync() {
53 doing_sync.set();
54 }
55
56 /* EVENT QUEUING */
57
58 void PhysicsServer2DWrapMT::step(real_t p_step) {
59 if (create_thread) {
60 command_queue.push(physics_server_2d, &PhysicsServer2D::step, p_step);
61 } else {
62 physics_server_2d->step(p_step);
63 }
64 }
65
66 void PhysicsServer2DWrapMT::sync() {
67 if (create_thread) {
68 command_queue.push_and_sync(this, &PhysicsServer2DWrapMT::_thread_sync);
69 } else {
70 command_queue.flush_all(); // Flush all pending from other threads.
71 }
72 physics_server_2d->sync();
73 }
74
75 void PhysicsServer2DWrapMT::flush_queries() {
76 physics_server_2d->flush_queries();
77 }
78
79 void PhysicsServer2DWrapMT::end_sync() {
80 physics_server_2d->end_sync();
81
82 if (create_thread) {
83 doing_sync.clear();
84 }
85 }
86
87 void PhysicsServer2DWrapMT::init() {
88 if (create_thread) {
89 WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &PhysicsServer2DWrapMT::_thread_loop), true, "Physics server 2D pump task", true);
90 command_queue.set_pump_task_id(tid);
91 command_queue.push(this, &PhysicsServer2DWrapMT::_assign_mt_ids, tid);
92 command_queue.push_and_sync(physics_server_2d, &PhysicsServer2D::init);
93 DEV_ASSERT(server_task_id == tid);
94 } else {
95 server_thread = Thread::MAIN_ID;
96 physics_server_2d->init();
97 }
98 }
99
100 void PhysicsServer2DWrapMT::finish() {
101 if (create_thread) {
102 command_queue.push(physics_server_2d, &PhysicsServer2D::finish);
103 command_queue.push(this, &PhysicsServer2DWrapMT::_thread_exit);
104 if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {
105 WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);
106 server_task_id = WorkerThreadPool::INVALID_TASK_ID;
107 }
108 server_thread = Thread::MAIN_ID;
109 } else {
110 physics_server_2d->finish();
111 }
112 }
113
114 PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) {
115 physics_server_2d = p_contained;
116 create_thread = p_create_thread;
117 }
118
119 PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() {
120 memdelete(physics_server_2d);
121 }