Skip to content

Commit

Permalink
Use futex locks on wasm+atomics.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ou-se committed Apr 19, 2022
1 parent 65987ae commit 8f2913c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 354 deletions.
102 changes: 0 additions & 102 deletions library/std/src/sys/wasm/atomics/condvar.rs

This file was deleted.

64 changes: 0 additions & 64 deletions library/std/src/sys/wasm/atomics/mutex.rs

This file was deleted.

145 changes: 0 additions & 145 deletions library/std/src/sys/wasm/atomics/rwlock.rs

This file was deleted.

34 changes: 0 additions & 34 deletions library/std/src/sys/wasm/atomics/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,3 @@ pub mod guard {
None
}
}

// We currently just use our own thread-local to store our
// current thread's ID, and then we lazily initialize it to something allocated
// from a global counter.
pub fn my_id() -> u32 {
use crate::sync::atomic::{AtomicU32, Ordering::SeqCst};

static NEXT_ID: AtomicU32 = AtomicU32::new(0);

#[thread_local]
static mut MY_ID: u32 = 0;

unsafe {
// If our thread ID isn't set yet then we need to allocate one. Do so
// with with a simple "atomically add to a global counter" strategy.
// This strategy doesn't handled what happens when the counter
// overflows, however, so just abort everything once the counter
// overflows and eventually we could have some sort of recycling scheme
// (or maybe this is all totally irrelevant by that point!). In any case
// though we're using a CAS loop instead of a `fetch_add` to ensure that
// the global counter never overflows.
if MY_ID == 0 {
let mut cur = NEXT_ID.load(SeqCst);
MY_ID = loop {
let next = cur.checked_add(1).unwrap_or_else(|| crate::process::abort());
match NEXT_ID.compare_exchange(cur, next, SeqCst, SeqCst) {
Ok(_) => break next,
Err(i) => cur = i,
}
};
}
MY_ID
}
}
15 changes: 6 additions & 9 deletions library/std/src/sys/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ pub mod time;

cfg_if::cfg_if! {
if #[cfg(target_feature = "atomics")] {
#[path = "atomics/condvar.rs"]
mod condvar;
#[path = "atomics/mutex.rs"]
mod mutex;
#[path = "atomics/rwlock.rs"]
mod rwlock;
#[path = "../unix/locks"]
pub mod locks {
pub use super::condvar::*;
pub use super::mutex::*;
pub use super::rwlock::*;
#![allow(unsafe_op_in_unsafe_fn)]
mod futex;
mod futex_rwlock;
pub use futex::{Mutex, MovableMutex, Condvar, MovableCondvar};
pub use futex_rwlock::{RwLock, MovableRwLock};
}
#[path = "atomics/futex.rs"]
pub mod futex;
Expand Down

0 comments on commit 8f2913c

Please sign in to comment.