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 | #include "core/os/os.h" |
34 | |
35 | void PhysicsServer2DWrapMT::thread_exit() { |
36 | exit.set(); |
37 | } |
38 | |
39 | void PhysicsServer2DWrapMT::thread_step(real_t p_delta) { |
40 | physics_server_2d->step(p_delta); |
41 | step_sem.post(); |
42 | } |
43 | |
44 | void PhysicsServer2DWrapMT::_thread_callback(void *_instance) { |
45 | PhysicsServer2DWrapMT *vsmt = reinterpret_cast<PhysicsServer2DWrapMT *>(_instance); |
46 | |
47 | vsmt->thread_loop(); |
48 | } |
49 | |
50 | void PhysicsServer2DWrapMT::thread_loop() { |
51 | server_thread = Thread::get_caller_id(); |
52 | |
53 | physics_server_2d->init(); |
54 | |
55 | exit.clear(); |
56 | step_thread_up.set(); |
57 | while (!exit.is_set()) { |
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_2d->finish(); |
65 | } |
66 | |
67 | /* EVENT QUEUING */ |
68 | |
69 | void PhysicsServer2DWrapMT::step(real_t p_step) { |
70 | if (create_thread) { |
71 | command_queue.push(this, &PhysicsServer2DWrapMT::thread_step, p_step); |
72 | } else { |
73 | command_queue.flush_all(); //flush all pending from other threads |
74 | physics_server_2d->step(p_step); |
75 | } |
76 | } |
77 | |
78 | void PhysicsServer2DWrapMT::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_2d->sync(); |
87 | } |
88 | |
89 | void PhysicsServer2DWrapMT::flush_queries() { |
90 | physics_server_2d->flush_queries(); |
91 | } |
92 | |
93 | void PhysicsServer2DWrapMT::end_sync() { |
94 | physics_server_2d->end_sync(); |
95 | } |
96 | |
97 | void PhysicsServer2DWrapMT::init() { |
98 | if (create_thread) { |
99 | //OS::get_singleton()->release_rendering_thread(); |
100 | thread.start(_thread_callback, this); |
101 | while (!step_thread_up.is_set()) { |
102 | OS::get_singleton()->delay_usec(1000); |
103 | } |
104 | } else { |
105 | physics_server_2d->init(); |
106 | } |
107 | } |
108 | |
109 | void PhysicsServer2DWrapMT::finish() { |
110 | if (thread.is_started()) { |
111 | command_queue.push(this, &PhysicsServer2DWrapMT::thread_exit); |
112 | thread.wait_to_finish(); |
113 | } else { |
114 | physics_server_2d->finish(); |
115 | } |
116 | } |
117 | |
118 | PhysicsServer2DWrapMT::PhysicsServer2DWrapMT(PhysicsServer2D *p_contained, bool p_create_thread) : |
119 | command_queue(p_create_thread) { |
120 | physics_server_2d = 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 | PhysicsServer2DWrapMT::~PhysicsServer2DWrapMT() { |
135 | memdelete(physics_server_2d); |
136 | //finish(); |
137 | } |
138 | |