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/os/os.h" |
34 | |
35 | void PhysicsServer3DWrapMT::thread_exit() { |
36 | exit = true; |
37 | } |
38 | |
39 | void PhysicsServer3DWrapMT::thread_step(real_t p_delta) { |
40 | physics_server_3d->step(p_delta); |
41 | step_sem.post(); |
42 | } |
43 | |
44 | void PhysicsServer3DWrapMT::_thread_callback(void *_instance) { |
45 | PhysicsServer3DWrapMT *vsmt = reinterpret_cast<PhysicsServer3DWrapMT *>(_instance); |
46 | |
47 | vsmt->thread_loop(); |
48 | } |
49 | |
50 | void PhysicsServer3DWrapMT::thread_loop() { |
51 | server_thread = Thread::get_caller_id(); |
52 | |
53 | physics_server_3d->init(); |
54 | |
55 | exit = false; |
56 | step_thread_up = true; |
57 | while (!exit) { |
58 | // flush commands one by one, until exit is requested |
59 | command_queue.wait_and_flush(); |
60 | } |
61 | |
62 | command_queue.flush_all(); // flush all |
63 | |
64 | physics_server_3d->finish(); |
65 | } |
66 | |
67 | /* EVENT QUEUING */ |
68 | |
69 | void PhysicsServer3DWrapMT::step(real_t p_step) { |
70 | if (create_thread) { |
71 | command_queue.push(this, &PhysicsServer3DWrapMT::thread_step, p_step); |
72 | } else { |
73 | command_queue.flush_all(); //flush all pending from other threads |
74 | physics_server_3d->step(p_step); |
75 | } |
76 | } |
77 | |
78 | void PhysicsServer3DWrapMT::sync() { |
79 | if (create_thread) { |
80 | if (first_frame) { |
81 | first_frame = false; |
82 | } else { |
83 | step_sem.wait(); //must not wait if a step was not issued |
84 | } |
85 | } |
86 | physics_server_3d->sync(); |
87 | } |
88 | |
89 | void PhysicsServer3DWrapMT::flush_queries() { |
90 | physics_server_3d->flush_queries(); |
91 | } |
92 | |
93 | void PhysicsServer3DWrapMT::end_sync() { |
94 | physics_server_3d->end_sync(); |
95 | } |
96 | |
97 | void PhysicsServer3DWrapMT::init() { |
98 | if (create_thread) { |
99 | //OS::get_singleton()->release_rendering_thread(); |
100 | thread.start(_thread_callback, this); |
101 | while (!step_thread_up) { |
102 | OS::get_singleton()->delay_usec(1000); |
103 | } |
104 | } else { |
105 | physics_server_3d->init(); |
106 | } |
107 | } |
108 | |
109 | void PhysicsServer3DWrapMT::finish() { |
110 | if (thread.is_started()) { |
111 | command_queue.push(this, &PhysicsServer3DWrapMT::thread_exit); |
112 | thread.wait_to_finish(); |
113 | } else { |
114 | physics_server_3d->finish(); |
115 | } |
116 | } |
117 | |
118 | PhysicsServer3DWrapMT::PhysicsServer3DWrapMT(PhysicsServer3D *p_contained, bool p_create_thread) : |
119 | command_queue(p_create_thread) { |
120 | physics_server_3d = p_contained; |
121 | create_thread = p_create_thread; |
122 | |
123 | pool_max_size = GLOBAL_GET("memory/limits/multithreaded_server/rid_pool_prealloc" ); |
124 | |
125 | if (!p_create_thread) { |
126 | server_thread = Thread::get_caller_id(); |
127 | } else { |
128 | server_thread = 0; |
129 | } |
130 | |
131 | main_thread = Thread::get_caller_id(); |
132 | } |
133 | |
134 | PhysicsServer3DWrapMT::~PhysicsServer3DWrapMT() { |
135 | memdelete(physics_server_3d); |
136 | //finish(); |
137 | } |
138 | |