Skip to content

Commit

Permalink
Remove unnecessary critical-section implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 8, 2024
1 parent d48362c commit ba1e6a7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 64 deletions.
3 changes: 0 additions & 3 deletions tests-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ std = [
"serde-json-core?/std",
"getrandom/std",
"rand/std",
"once_cell/std",
"futures-util/std",
"futures-channel/std",
"web-thread",
Expand Down Expand Up @@ -51,10 +50,8 @@ web-sys = { version = "0.3", default-features = false, features = [
web-time = { path = "../", default-features = false }

[target.'cfg(all(target_arch = "wasm32", target_feature = "atomics"))'.dependencies]
critical-section = { version = "1", features = ["restore-state-bool"] }
futures-channel = { version = "0.3", default-features = false, features = ["alloc"] }
futures-util = { version = "0.3", default-features = false }
once_cell = { version = "1", default-features = false, features = ["critical-section"] }
web-sys = { version = "0.3", default-features = false, features = [
"DedicatedWorkerGlobalScope",
"OfflineAudioContext",
Expand Down
61 changes: 0 additions & 61 deletions tests-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#![cfg_attr(all(target_arch = "wasm32", not(feature = "std")), no_std)]
#![cfg_attr(all(test, target_arch = "wasm32"), no_main)]
#![cfg_attr(
all(target_arch = "wasm32", target_feature = "atomics"),
feature(thread_local)
)]

#[cfg(all(target_arch = "wasm32", not(feature = "std")))]
use wasm_bindgen_test as _;
Expand Down Expand Up @@ -102,60 +98,3 @@ mod allocator {
}
}
}

#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))]
#[expect(
unsafe_code,
reason = "no way to implement `critical_section` without unsafe"
)]
#[expect(
clippy::no_mangle_with_rust_abi,
reason = "from `critical_section::set_impl!` macro"
)]
mod critical_section {
//! Implementing [`critical_section`].
use core::cell::Cell;
use core::sync::atomic::{AtomicBool, Ordering};

use critical_section::Impl;

/// The lock flag.
static LOCKED: AtomicBool = AtomicBool::new(false);
/// The thread local lock flag.
#[thread_local]
static LOCAL_LOCKED: Cell<bool> = Cell::new(false);

critical_section::set_impl!(CriticalSection);

/// Implementing [`critical_section::Impl`].
struct CriticalSection;

// SAFETY: the lock is safely implemented.
unsafe impl Impl for CriticalSection {
#[inline]
unsafe fn acquire() -> bool {
if LOCAL_LOCKED.get() {
return true;
}

LOCAL_LOCKED.set(true);

while LOCKED
.compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed)
.is_err()
{}

false
}

#[inline]
unsafe fn release(restore_state: bool) {
if !restore_state {
LOCKED.store(false, Ordering::Release);

LOCAL_LOCKED.set(false);
}
}
}
}

0 comments on commit ba1e6a7

Please sign in to comment.