Use futex locks on wasm+atomics. · rust-lang/rust@8f2913c (original) (raw)

5 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -53,37 +53,3 @@ pub mod guard {
53 53 None
54 54 }
55 55 }
56 -
57 -// We currently just use our own thread-local to store our
58 -// current thread's ID, and then we lazily initialize it to something allocated
59 -// from a global counter.
60 -pub fn my_id() -> u32 {
61 -use crate::sync::atomic::{AtomicU32, Ordering::SeqCst};
62 -
63 -static NEXT_ID: AtomicU32 = AtomicU32::new(0);
64 -
65 -#[thread_local]
66 -static mut MY_ID: u32 = 0;
67 -
68 -unsafe {
69 -// If our thread ID isn't set yet then we need to allocate one. Do so
70 -// with with a simple "atomically add to a global counter" strategy.
71 -// This strategy doesn't handled what happens when the counter
72 -// overflows, however, so just abort everything once the counter
73 -// overflows and eventually we could have some sort of recycling scheme
74 -// (or maybe this is all totally irrelevant by that point!). In any case
75 -// though we're using a CAS loop instead of a `fetch_add` to ensure that
76 -// the global counter never overflows.
77 -if MY_ID == 0 {
78 -let mut cur = NEXT_ID.load(SeqCst);
79 -MY_ID = loop {
80 -let next = cur.checked_add(1).unwrap_or_else(|
81 -match NEXT_ID.compare_exchange(cur, next, SeqCst, SeqCst) {
82 -Ok(_) => break next,
83 -Err(i) => cur = i,
84 -}
85 -};
86 -}
87 -MY_ID
88 -}
89 -}
Original file line number Diff line number Diff line change
@@ -49,16 +49,13 @@ pub mod time;
49 49
50 50 cfg_if::cfg_if! {
51 51 if #[cfg(target_feature = "atomics")] {
52 - #[path = "atomics/condvar.rs"]
53 -mod condvar;
54 - #[path = "atomics/mutex.rs"]
55 -mod mutex;
56 - #[path = "atomics/rwlock.rs"]
57 -mod rwlock;
52 + #[path = "../unix/locks"]
58 53 pub mod locks {
59 -pub use super::condvar::*;
60 -pub use super::mutex::*;
61 -pub use super::rwlock::*;
54 + #![allow(unsafe_op_in_unsafe_fn)]
55 +mod futex;
56 +mod futex_rwlock;
57 +pub use futex::{Mutex, MovableMutex, Condvar, MovableCondvar};
58 +pub use futex_rwlock::{RwLock, MovableRwLock};
62 59 }
63 60 #[path = "atomics/futex.rs"]
64 61 pub mod futex;