File tree Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Expand file tree Collapse file tree 3 files changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -158,6 +158,10 @@ unsafe fn gdext_on_level_init(level: InitLevel) {
158
158
unsafe { sys:: load_class_method_table ( level) } ;
159
159
160
160
match level {
161
+ InitLevel :: Servers => {
162
+ // SAFETY: called from the main thread, sys::initialized has already been called.
163
+ unsafe { sys:: discover_main_thread ( ) } ;
164
+ }
161
165
InitLevel :: Scene => {
162
166
// SAFETY: On the main thread, api initialized, `Scene` was initialized above.
163
167
unsafe { ensure_godot_features_compatible ( ) } ;
Original file line number Diff line number Diff line change @@ -123,8 +123,7 @@ impl BindingStorage {
123
123
124
124
// We only check if we are in the main thread in debug builds if we aren't building for a non-threaded Godot build,
125
125
// since we could otherwise assume there won't be multi-threading.
126
- // TODO: figure out why the panic happens on Android, and how to resolve it. See https://github.com/godot-rust/gdext/pull/780.
127
- #[ cfg( all( debug_assertions, not( wasm_nothreads) , not( target_os = "android" ) ) ) ]
126
+ #[ cfg( all( debug_assertions, not( wasm_nothreads) ) ) ]
128
127
{
129
128
if !crate :: is_main_thread ( ) {
130
129
// If a binding is accessed the first time, this will panic and start unwinding. It can then happen that during unwinding,
Original file line number Diff line number Diff line change @@ -447,6 +447,32 @@ pub fn is_main_thread() -> bool {
447
447
}
448
448
}
449
449
450
+ /// Assign the current thread id to be the main thread.
451
+ ///
452
+ /// This is required for platforms on which Godot runs the main loop on a different thread than the thread the library was loaded on.
453
+ /// Android is one such platform.
454
+ ///
455
+ /// # Safety
456
+ ///
457
+ /// - must only be called after [`initialize`] has been called.
458
+ pub unsafe fn discover_main_thread ( ) {
459
+ #[ cfg( not( wasm_nothreads) ) ]
460
+ {
461
+ if is_main_thread ( ) {
462
+ // we don't have to do anything if the current thread is already the main thread.
463
+ return ;
464
+ }
465
+
466
+ let thread_id = std:: thread:: current ( ) . id ( ) ;
467
+
468
+ // SAFETY: initialize must have already been called before this function is called. By clearing and setting the cell again we can reinitialize it.
469
+ unsafe {
470
+ MAIN_THREAD_ID . clear ( ) ;
471
+ MAIN_THREAD_ID . set ( thread_id) ;
472
+ }
473
+ }
474
+ }
475
+
450
476
// ----------------------------------------------------------------------------------------------------------------------------------------------
451
477
// Macros to access low-level function bindings
452
478
You can’t perform that action at this time.
0 commit comments