Skip to content

Commit 1176134

Browse files
Rollup merge of #123811 - joboet:queue_em_up, r=ChrisDenton
Use queue-based `RwLock` on more platforms This switches over Windows 7, SGX and Xous to the queue-based `RwLock` implementation added in #110211, thereby fixing #121949 for Windows 7 and partially resolving #114581 on SGX. TEEOS can't currently be switched because it doesn't have a good thread parking implementation. CC `@roblabla` `@raoulstrackx` `@xobs` Could you help me test this, please? r? `@ChrisDenton` the Windows stuff should be familiar to you
2 parents 239b372 + 10b6ca1 commit 1176134

File tree

8 files changed

+58
-387
lines changed

8 files changed

+58
-387
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! The functions in this module are needed by libunwind. These symbols are named
2+
//! in pre-link args for the target specification, so keep that in sync.
3+
4+
#![cfg(not(test))]
5+
6+
use crate::sys::sync::RwLock;
7+
8+
// Verify that the byte pattern libunwind uses to initialize an RwLock is
9+
// equivalent to the value of RwLock::new(). If the value changes,
10+
// `src/UnwindRustSgx.h` in libunwind needs to be changed too.
11+
const _: () = unsafe {
12+
let bits_rust: usize = crate::mem::transmute(RwLock::new());
13+
assert!(bits_rust == 0);
14+
};
15+
16+
const EINVAL: i32 = 22;
17+
18+
#[no_mangle]
19+
pub unsafe extern "C" fn __rust_rwlock_rdlock(p: *mut RwLock) -> i32 {
20+
if p.is_null() {
21+
return EINVAL;
22+
}
23+
24+
// We cannot differentiate between reads an writes in unlock and therefore
25+
// always use a write-lock. Unwinding isn't really in the hot path anyway.
26+
unsafe { (*p).write() };
27+
return 0;
28+
}
29+
30+
#[no_mangle]
31+
pub unsafe extern "C" fn __rust_rwlock_wrlock(p: *mut RwLock) -> i32 {
32+
if p.is_null() {
33+
return EINVAL;
34+
}
35+
unsafe { (*p).write() };
36+
return 0;
37+
}
38+
39+
#[no_mangle]
40+
pub unsafe extern "C" fn __rust_rwlock_unlock(p: *mut RwLock) -> i32 {
41+
if p.is_null() {
42+
return EINVAL;
43+
}
44+
unsafe { (*p).write_unlock() };
45+
return 0;
46+
}

library/std/src/sys/pal/sgx/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod fd;
1717
pub mod fs;
1818
#[path = "../unsupported/io.rs"]
1919
pub mod io;
20+
mod libunwind_integration;
2021
pub mod net;
2122
pub mod os;
2223
#[path = "../unsupported/pipe.rs"]

library/std/src/sys/pal/sgx/waitqueue/mod.rs

+5-23
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ impl<T> WaitVariable<T> {
5252
WaitVariable { queue: WaitQueue::new(), lock: var }
5353
}
5454

55-
pub fn queue_empty(&self) -> bool {
56-
self.queue.is_empty()
57-
}
58-
5955
pub fn lock_var(&self) -> &T {
6056
&self.lock
6157
}
@@ -68,7 +64,7 @@ impl<T> WaitVariable<T> {
6864
#[derive(Copy, Clone)]
6965
pub enum NotifiedTcs {
7066
Single(Tcs),
71-
All { count: NonZero<usize> },
67+
All { _count: NonZero<usize> },
7268
}
7369

7470
/// An RAII guard that will notify a set of target threads as well as unlock
@@ -98,19 +94,6 @@ impl Default for WaitQueue {
9894
}
9995
}
10096

101-
impl<'a, T> WaitGuard<'a, T> {
102-
/// Returns which TCSes will be notified when this guard drops.
103-
pub fn notified_tcs(&self) -> NotifiedTcs {
104-
self.notified_tcs
105-
}
106-
107-
/// Drop this `WaitGuard`, after dropping another `guard`.
108-
pub fn drop_after<U>(self, guard: U) {
109-
drop(guard);
110-
drop(self);
111-
}
112-
}
113-
11497
impl<'a, T> Deref for WaitGuard<'a, T> {
11598
type Target = SpinMutexGuard<'a, WaitVariable<T>>;
11699

@@ -141,10 +124,6 @@ impl WaitQueue {
141124
WaitQueue { inner: UnsafeList::new() }
142125
}
143126

144-
pub fn is_empty(&self) -> bool {
145-
self.inner.is_empty()
146-
}
147-
148127
/// Adds the calling thread to the `WaitVariable`'s wait queue, then wait
149128
/// until a wakeup event.
150129
///
@@ -253,7 +232,10 @@ impl WaitQueue {
253232
}
254233

255234
if let Some(count) = NonZero::new(count) {
256-
Ok(WaitGuard { mutex_guard: Some(guard), notified_tcs: NotifiedTcs::All { count } })
235+
Ok(WaitGuard {
236+
mutex_guard: Some(guard),
237+
notified_tcs: NotifiedTcs::All { _count: count },
238+
})
257239
} else {
258240
Err(guard)
259241
}

library/std/src/sys/sync/rwlock/mod.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,20 @@ cfg_if::cfg_if! {
1212
))] {
1313
mod futex;
1414
pub use futex::RwLock;
15-
} else if #[cfg(target_family = "unix")] {
15+
} else if #[cfg(any(
16+
target_family = "unix",
17+
all(target_os = "windows", target_vendor = "win7"),
18+
all(target_vendor = "fortanix", target_env = "sgx"),
19+
target_os = "xous",
20+
))] {
1621
mod queue;
1722
pub use queue::RwLock;
18-
} else if #[cfg(all(target_os = "windows", target_vendor = "win7"))] {
19-
mod windows7;
20-
pub use windows7::RwLock;
21-
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
22-
mod sgx;
23-
pub use sgx::RwLock;
2423
} else if #[cfg(target_os = "solid_asp3")] {
2524
mod solid;
2625
pub use solid::RwLock;
2726
} else if #[cfg(target_os = "teeos")] {
2827
mod teeos;
2928
pub use teeos::RwLock;
30-
} else if #[cfg(target_os = "xous")] {
31-
mod xous;
32-
pub use xous::RwLock;
3329
} else {
3430
mod no_threads;
3531
pub use no_threads::RwLock;

library/std/src/sys/sync/rwlock/sgx.rs

-219
This file was deleted.

library/std/src/sys/sync/rwlock/sgx/tests.rs

-21
This file was deleted.

0 commit comments

Comments
 (0)