From b5f7b2f86a828fd777e3d022e7e1066e9291c2fa Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 27 Jul 2021 11:07:15 -0500 Subject: [PATCH] Remove thread local for mach port (#3119) This was needed a long time ago in the original implementation when the function being called here was hotter than it was before, but nowadays this function isn't hot as it's protected elsewhere from being repeatedly called, so the caching thread local is no longer necessary. --- crates/runtime/src/traphandlers/macos.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/crates/runtime/src/traphandlers/macos.rs b/crates/runtime/src/traphandlers/macos.rs index 133ae80245..c5f7f2527e 100644 --- a/crates/runtime/src/traphandlers/macos.rs +++ b/crates/runtime/src/traphandlers/macos.rs @@ -392,20 +392,6 @@ unsafe extern "C" fn unwind(wasm_pc: *const u8) -> ! { wasmtime_longjmp(jmp_buf); } -thread_local! { - static MY_PORT: ClosePort = ClosePort(unsafe { mach_thread_self() }); -} - -struct ClosePort(mach_port_name_t); - -impl Drop for ClosePort { - fn drop(&mut self) { - unsafe { - mach_port_deallocate(mach_task_self(), self.0); - } - } -} - /// Exceptions on macOS can be delivered to either thread-level or task-level /// exception ports. In wasmtime we choose to send the exceptions to /// thread-level ports. This means that we need to, for each thread that can @@ -426,13 +412,15 @@ impl Drop for ClosePort { pub fn lazy_per_thread_init() -> Result<(), Trap> { unsafe { assert!(WASMTIME_PORT != MACH_PORT_NULL); + let this_thread = mach_thread_self(); let kret = thread_set_exception_ports( - MY_PORT.with(|p| p.0), + this_thread, EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION, WASMTIME_PORT, EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, mach_addons::THREAD_STATE_NONE, ); + mach_port_deallocate(mach_task_self(), this_thread); assert_eq!(kret, KERN_SUCCESS, "failed to set thread exception port"); } Ok(())