Skip to main content

wasmtime/runtime/vm/sys/unix/
traphandlers.rs

1cfg_select! {
2    not(has_native_signals) => {
3        // If signals-based traps are disabled statically then there's no
4        // platform signal handler and no per-thread init, so stub these both
5        // out.
6        pub enum SignalHandler {}
7
8        #[inline]
9        pub fn lazy_per_thread_init() {}
10    }
11    target_vendor = "apple" => {
12        // On macOS a dynamic decision is made to use mach ports or signals at
13        // process initialization time.
14
15        /// Whether or not macOS is using mach ports.
16        static mut USE_MACH_PORTS: bool = false;
17
18        pub use super::signals::SignalHandler;
19
20        pub enum TrapHandler {
21            Signals(super::signals::TrapHandler),
22            #[allow(dead_code, reason = "used for its drop")]
23            MachPorts(super::machports::TrapHandler),
24        }
25
26        impl TrapHandler {
27            pub unsafe fn new(macos_use_mach_ports: bool) -> TrapHandler {
28                unsafe {
29                    USE_MACH_PORTS = macos_use_mach_ports;
30                    if macos_use_mach_ports {
31                        TrapHandler::MachPorts(super::machports::TrapHandler::new())
32                    } else {
33                        TrapHandler::Signals(super::signals::TrapHandler::new(false))
34                    }
35                }
36            }
37
38            pub fn validate_config(&self, macos_use_mach_ports: bool) {
39                match self {
40                    TrapHandler::Signals(t) => t.validate_config(macos_use_mach_ports),
41                    TrapHandler::MachPorts(_) => assert!(macos_use_mach_ports),
42                }
43            }
44        }
45
46        pub fn lazy_per_thread_init() {
47            unsafe {
48                if USE_MACH_PORTS {
49                    super::machports::lazy_per_thread_init();
50                } else {
51                    super::signals::lazy_per_thread_init();
52                }
53            }
54        }
55    }
56    _ => {
57        // Otherwise unix platforms use the signals-based implementation of
58        // these functions.
59        pub use super::signals::{TrapHandler, SignalHandler, lazy_per_thread_init};
60    }
61}