Skip to content

Commit 79dba40

Browse files
committed
Remove thread_local! weirdness in anyref.rs
Resolves an old comment long since been fixed, and confirmed that we still don't generate extra cruft! Closes #1523
1 parent f977630 commit 79dba40

File tree

1 file changed

+7
-57
lines changed

1 file changed

+7
-57
lines changed

src/anyref.rs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::mem;
33
use std::ptr;
44
use std::slice;
55
use std::vec::Vec;
6-
6+
use std::cell::Cell;
77
use crate::JsValue;
88

99
externs! {
@@ -121,63 +121,13 @@ fn internal_error(msg: &str) -> ! {
121121
}
122122
}
123123

124-
// Whoa, there's two `tl` modules here! That's currently intention, but for sort
125-
// of a weird reason. The table here is fundamentally thread local, so we want
126-
// to use the `thread_local!` macro. The implementation of thread locals (as of
127-
// the time of this writing) generates a lot of code as it pulls in panic paths
128-
// in libstd (even when using `try_with`). There's a patch to fix that
129-
// (rust-lang/rust#55518), but in the meantime the stable/beta channels produce
130-
// a lot of code.
131-
//
132-
// Matters are made worse here because this code is almost never used (it's only
133-
// here for an unstable feature). If we were to have panics here, though, then
134-
// we couldn't effectively gc away the panic infrastructure, meaning this unused
135-
// infrastructure would show up in binaries! That's a no-no for wasm-bindgen.
136-
//
137-
// In the meantime, if the atomics feature is turned on (which it never is by
138-
// default) then we use `thread_local!`, otherwise we use a home-grown
139-
// implementation that will be replaced once #55518 lands on stable.
140-
#[cfg(target_feature = "atomics")]
141-
mod tl {
142-
use super::Slab;
143-
use std::cell::Cell;
144-
use std::*; // hack to get `thread_local!` to work
145-
146-
thread_local!(pub static HEAP_SLAB: Cell<Slab> = Cell::new(Slab::new()));
147-
}
148-
149-
#[cfg(not(target_feature = "atomics"))]
150-
mod tl {
151-
use super::Slab;
152-
use std::alloc::{self, Layout};
153-
use std::cell::Cell;
154-
use std::ptr;
155-
156-
pub struct HeapSlab;
157-
pub static HEAP_SLAB: HeapSlab = HeapSlab;
158-
static mut SLOT: *mut Cell<Slab> = 0 as *mut Cell<Slab>;
159-
160-
impl HeapSlab {
161-
pub fn try_with<R>(&self, f: impl FnOnce(&Cell<Slab>) -> R) -> Result<R, ()> {
162-
unsafe {
163-
if SLOT.is_null() {
164-
let ptr = alloc::alloc(Layout::new::<Cell<Slab>>());
165-
if ptr.is_null() {
166-
super::internal_error("allocation failure");
167-
}
168-
let ptr = ptr as *mut Cell<Slab>;
169-
ptr::write(ptr, Cell::new(Slab::new()));
170-
SLOT = ptr;
171-
}
172-
Ok(f(&*SLOT))
173-
}
174-
}
175-
}
176-
}
124+
// Management of `anyref` is always thread local since an `anyref` value can't
125+
// cross threads in wasm. Indices as a result are always thread-local.
126+
std::thread_local!(pub static HEAP_SLAB: Cell<Slab> = Cell::new(Slab::new()));
177127

178128
#[no_mangle]
179129
pub extern "C" fn __wbindgen_anyref_table_alloc() -> usize {
180-
tl::HEAP_SLAB
130+
HEAP_SLAB
181131
.try_with(|slot| {
182132
let mut slab = slot.replace(Slab::new());
183133
let ret = slab.alloc();
@@ -197,7 +147,7 @@ pub extern "C" fn __wbindgen_anyref_table_dealloc(idx: usize) {
197147
unsafe {
198148
__wbindgen_anyref_table_set_null(idx);
199149
}
200-
tl::HEAP_SLAB
150+
HEAP_SLAB
201151
.try_with(|slot| {
202152
let mut slab = slot.replace(Slab::new());
203153
slab.dealloc(idx);
@@ -217,7 +167,7 @@ pub unsafe extern "C" fn __wbindgen_drop_anyref_slice(ptr: *mut JsValue, len: us
217167
// `anyref` instead of the JS `heap`.
218168
#[no_mangle]
219169
pub unsafe extern "C" fn __wbindgen_anyref_heap_live_count_impl() -> u32 {
220-
tl::HEAP_SLAB
170+
HEAP_SLAB
221171
.try_with(|slot| {
222172
let slab = slot.replace(Slab::new());
223173
let count = slab.live_count();

0 commit comments

Comments
 (0)