Switch to const-initialized thread_local variables where appropriate

This commit is contained in:
Noa
2022-08-21 15:10:50 -05:00
committed by Jeong, YunWon
parent 2230d6c751
commit 98137eb79c
4 changed files with 13 additions and 4 deletions

View File

@@ -56,7 +56,9 @@ mod non_threading {
$($(#[$attr])*
$vis static $name: $crate::static_cell::StaticCell<$t> = {
::std::thread_local! {
$vis static $name: $crate::lock::OnceCell<&'static $t> = $crate::lock::OnceCell::new();
$vis static $name: $crate::lock::OnceCell<&'static $t> = const {
$crate::lock::OnceCell::new()
};
}
$crate::static_cell::StaticCell::_from_local_key(&$name)
};)+

View File

@@ -486,7 +486,10 @@ pub mod levenshtein {
pub fn levenshtein_distance(a: &str, b: &str, max_cost: usize) -> usize {
thread_local! {
static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = const { RefCell::new([0usize; MAX_STRING_SIZE]) };
#[allow(clippy::declare_interior_mutable_const)]
static BUFFER: RefCell<[usize; MAX_STRING_SIZE]> = const {
RefCell::new([0usize; MAX_STRING_SIZE])
};
}
if a == b {

View File

@@ -355,7 +355,9 @@ pub(crate) mod _thread {
Err(vm.new_exception_empty(vm.ctx.exceptions.system_exit.to_owned()))
}
thread_local!(static SENTINELS: RefCell<Vec<PyRef<Lock>>> = RefCell::default());
thread_local! {
static SENTINELS: RefCell<Vec<PyRef<Lock>>> = const { RefCell::new(Vec::new()) };
}
#[pyfunction]
fn _set_sentinel(vm: &VirtualMachine) -> PyRef<Lock> {

View File

@@ -86,7 +86,9 @@ pub fn add_init_func(f: fn(&mut VirtualMachine)) {
// https://rustwasm.github.io/2018/10/24/multithreading-rust-and-wasm.html#atomic-instructions
thread_local! {
static STORED_VMS: RefCell<HashMap<String, Rc<StoredVirtualMachine>>> = RefCell::default();
static VM_INIT_FUNCS: RefCell<Vec<fn(&mut VirtualMachine)>> = RefCell::default();
static VM_INIT_FUNCS: RefCell<Vec<fn(&mut VirtualMachine)>> = const {
RefCell::new(Vec::new())
};
}
pub fn get_vm_id(vm: &VirtualMachine) -> &str {