Skip to content

Commit

Permalink
SeqCst->{Release,Acquire} for wasm DropLock.
Browse files Browse the repository at this point in the history
SeqCst is unnecessary. Release+Acquire is the right ordering for a
mutex.
  • Loading branch information
m-ou-se committed Mar 19, 2024
1 parent e43aef0 commit 46bb073
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions library/std/src/sys/pal/wasm/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ unsafe impl GlobalAlloc for System {

#[cfg(target_feature = "atomics")]
mod lock {
use crate::sync::atomic::{AtomicI32, Ordering::SeqCst};
use crate::sync::atomic::{
AtomicI32,
Ordering::{Acquire, Release},
};

static LOCKED: AtomicI32 = AtomicI32::new(0);

pub struct DropLock;

pub fn lock() -> DropLock {
loop {
if LOCKED.swap(1, SeqCst) == 0 {
if LOCKED.swap(1, Acquire) == 0 {
return DropLock;
}
// Ok so here's where things get a little depressing. At this point
Expand Down Expand Up @@ -143,7 +146,7 @@ mod lock {

impl Drop for DropLock {
fn drop(&mut self) {
let r = LOCKED.swap(0, SeqCst);
let r = LOCKED.swap(0, Release);
debug_assert_eq!(r, 1);

// Note that due to the above logic we don't actually need to wake
Expand Down

0 comments on commit 46bb073

Please sign in to comment.