|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +//! Linux-specific configuration. |
| 9 | +
|
| 10 | +// Avoid TLS-destructors preventing the dynamic library from being closed. |
| 11 | +// |
| 12 | +// Credits to fasterthanlime for discovering the very helpful workaround. |
| 13 | +// See: https://fasterthanli.me/articles/so-you-want-to-live-reload-rust#what-can-prevent-dlclose-from-unloading-a-library |
| 14 | + |
| 15 | +use std::ffi::c_void; |
| 16 | +use std::sync::OnceLock; |
| 17 | + |
| 18 | +pub type ThreadAtexitFn = unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void); |
| 19 | + |
| 20 | +static SYSTEM_THREAD_ATEXIT: OnceLock<Option<ThreadAtexitFn>> = OnceLock::new(); |
| 21 | +static HOT_RELOADING_ENABLED: OnceLock<bool> = OnceLock::new(); |
| 22 | + |
| 23 | +fn system_thread_atexit() -> &'static Option<ThreadAtexitFn> { |
| 24 | + SYSTEM_THREAD_ATEXIT.get_or_init(|| unsafe { |
| 25 | + let name = c"__cxa_thread_atexit_impl".as_ptr(); |
| 26 | + std::mem::transmute(libc::dlsym(libc::RTLD_NEXT, name)) |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +pub fn enable_hot_reload() { |
| 31 | + // If hot reloading is enabled then we should properly unload the library, so this will only be called once. |
| 32 | + HOT_RELOADING_ENABLED |
| 33 | + .set(true) |
| 34 | + .expect("hot reloading should only be set once") |
| 35 | +} |
| 36 | + |
| 37 | +pub fn disable_hot_reload() { |
| 38 | + // If hot reloading is disabled then we may call this method multiple times. |
| 39 | + _ = HOT_RELOADING_ENABLED.set(false) |
| 40 | +} |
| 41 | + |
| 42 | +pub fn default_set_hot_reload() { |
| 43 | + // By default we enable hot reloading for debug builds, as it's likely that the user may want hot reloading in debug builds. |
| 44 | + // Release builds however should avoid leaking memory, so we disable hot reloading support by default. |
| 45 | + if cfg!(debug_assertions) { |
| 46 | + enable_hot_reload() |
| 47 | + } else { |
| 48 | + disable_hot_reload() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +fn is_hot_reload_enabled() -> bool { |
| 53 | + // Assume hot reloading is disabled unless something else has been specified already. This is the better default as thread local storage |
| 54 | + // destructors exist for good reasons. |
| 55 | + // This is needed for situations like unit-tests, where we may create TLS-destructors without explicitly calling any of the methods |
| 56 | + // that set hot reloading to be enabled or disabled. |
| 57 | + *HOT_RELOADING_ENABLED.get_or_init(|| false) |
| 58 | +} |
| 59 | + |
| 60 | +/// Turns glibc's TLS destructor register function, `__cxa_thread_atexit_impl`, |
| 61 | +/// into a no-op if hot reloading is enabled. |
| 62 | +/// |
| 63 | +/// # Safety |
| 64 | +/// This needs to be public for symbol visibility reasons, but you should |
| 65 | +/// never need to call this yourself |
| 66 | +pub unsafe fn thread_atexit(func: *mut c_void, obj: *mut c_void, dso_symbol: *mut c_void) { |
| 67 | + if is_hot_reload_enabled() { |
| 68 | + // Avoid registering TLS destructors on purpose, to avoid |
| 69 | + // double-frees and general crashiness |
| 70 | + } else if let Some(system_thread_atexit) = *system_thread_atexit() { |
| 71 | + // Hot reloading is disabled, and system provides `__cxa_thread_atexit_impl`, |
| 72 | + // so forward the call to it. |
| 73 | + system_thread_atexit(func, obj, dso_symbol); |
| 74 | + } else { |
| 75 | + // Hot reloading is disabled *and* we don't have `__cxa_thread_atexit_impl`, |
| 76 | + // throw hands up in the air and leak memory. |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[macro_export] |
| 81 | +macro_rules! register_hot_reload_workaround { |
| 82 | + () => { |
| 83 | + #[no_mangle] |
| 84 | + pub unsafe extern "C" fn __cxa_thread_atexit_impl( |
| 85 | + func: *mut ::std::ffi::c_void, |
| 86 | + obj: *mut ::std::ffi::c_void, |
| 87 | + dso_symbol: *mut ::std::ffi::c_void, |
| 88 | + ) { |
| 89 | + $crate::linux_reload_workaround::thread_atexit(func, obj, dso_symbol); |
| 90 | + } |
| 91 | + }; |
| 92 | +} |
0 commit comments