11
11
12
12
use std:: cell:: Cell ;
13
13
14
- #[ cfg( not( wasm_nothreads) ) ]
15
- use std:: thread:: ThreadId ;
16
-
17
14
use super :: GodotBinding ;
18
15
use crate :: ManualInitCell ;
19
16
20
17
pub ( super ) struct BindingStorage {
21
18
// No threading when linking against Godot with a nothreads Wasm build.
22
19
// Therefore, we just need to check if the bindings were initialized, as all accesses are from the main thread.
23
- #[ cfg( wasm_nothreads) ]
24
20
initialized : Cell < bool > ,
25
-
26
- // Is used in to check that we've been called from the right thread, so must be thread-safe to access.
27
- #[ cfg( not( wasm_nothreads) ) ]
28
- main_thread_id : Cell < Option < ThreadId > > ,
29
21
binding : ManualInitCell < GodotBinding > ,
30
22
}
31
23
@@ -38,11 +30,7 @@ impl BindingStorage {
38
30
#[ inline( always) ]
39
31
unsafe fn storage ( ) -> & ' static Self {
40
32
static BINDING : BindingStorage = BindingStorage {
41
- #[ cfg( wasm_nothreads) ]
42
33
initialized : Cell :: new ( false ) ,
43
-
44
- #[ cfg( not( wasm_nothreads) ) ]
45
- main_thread_id : Cell :: new ( None ) ,
46
34
binding : ManualInitCell :: new ( ) ,
47
35
} ;
48
36
@@ -53,11 +41,7 @@ impl BindingStorage {
53
41
///
54
42
/// It is recommended to use this function for that purpose as the field to check varies depending on the compilation target.
55
43
fn initialized ( & self ) -> bool {
56
- #[ cfg( wasm_nothreads) ]
57
- return self . initialized . get ( ) ;
58
-
59
- #[ cfg( not( wasm_nothreads) ) ]
60
- self . main_thread_id . get ( ) . is_some ( )
44
+ self . initialized . get ( )
61
45
}
62
46
63
47
/// Marks the binding storage as initialized or deinitialized.
@@ -78,17 +62,7 @@ impl BindingStorage {
78
62
}
79
63
}
80
64
81
- // 'std::thread::current()' fails when linking to a Godot web build without threads. When compiling to wasm-nothreads,
82
- // we assume it is impossible to have multi-threading, so checking if we are in the main thread is not needed.
83
- // Therefore, we don't store the thread ID, but rather just whether initialization already occurred.
84
- #[ cfg( wasm_nothreads) ]
85
65
self . initialized . set ( initialized) ;
86
-
87
- #[ cfg( not( wasm_nothreads) ) ]
88
- {
89
- let thread_id = initialized. then ( || std:: thread:: current ( ) . id ( ) ) ;
90
- self . main_thread_id . set ( thread_id) ;
91
- }
92
66
}
93
67
94
68
/// Initialize the binding storage, this must be called before any other public functions.
@@ -152,11 +126,7 @@ impl BindingStorage {
152
126
// TODO: figure out why the panic happens on Android, and how to resolve it. See https://github.com/godot-rust/gdext/pull/780.
153
127
#[ cfg( all( debug_assertions, not( wasm_nothreads) , not( target_os = "android" ) ) ) ]
154
128
{
155
- let main_thread_id = storage. main_thread_id . get ( ) . expect (
156
- "Godot engine not available; make sure you are not calling it from unit/doc tests" ,
157
- ) ;
158
-
159
- if main_thread_id != std:: thread:: current ( ) . id ( ) {
129
+ if !crate :: is_main_thread ( ) {
160
130
// If a binding is accessed the first time, this will panic and start unwinding. It can then happen that during unwinding,
161
131
// another FFI call happens (e.g. Godot destructor), which would cause immediate abort, swallowing the error message.
162
132
// Thus check if a panic is already in progress.
0 commit comments