Skip to content

Commit 7450cf0

Browse files
authored
Rollup merge of rust-lang#125800 - fortanix:raoul/rte-99-fix_mut_static_task_queue, r=jethrogb
Fix `mut` static task queue in SGX target [PR 125046](rust-lang#125046) prevents mutable references to statics with `#[linkage]`. Such a construct was used with the tests for the `x86_64-fortanix-unknown-sgx` target. This PR fixes this and cleans up code a bit in 5 steps. Each step passes CI: - The `mut` static is removed, and `Task` explicitly implements `Send` - Renaming of the `task_queue::lock` function - Pass function for `Thread` as `Send` to `Thread::imp` and update when `Packet<'scope, T>` implements `Sync` - Storing `Task::p` as a type that implements `Send` - Letting the compiler auto implement `Send` for `Task` cc: ``@jethrogb``
2 parents 1632165 + ea7e91c commit 7450cf0

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

std/src/sys/pal/sgx/thread.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use self::task_queue::JoinNotifier;
1515

1616
mod task_queue {
1717
use super::wait_notify;
18-
use crate::sync::{Mutex, MutexGuard, Once};
18+
use crate::sync::{Mutex, MutexGuard};
1919

2020
pub type JoinHandle = wait_notify::Waiter;
2121

@@ -28,12 +28,12 @@ mod task_queue {
2828
}
2929

3030
pub(super) struct Task {
31-
p: Box<dyn FnOnce()>,
31+
p: Box<dyn FnOnce() + Send>,
3232
done: JoinNotifier,
3333
}
3434

3535
impl Task {
36-
pub(super) fn new(p: Box<dyn FnOnce()>) -> (Task, JoinHandle) {
36+
pub(super) fn new(p: Box<dyn FnOnce() + Send>) -> (Task, JoinHandle) {
3737
let (done, recv) = wait_notify::new();
3838
let done = JoinNotifier(Some(done));
3939
(Task { p, done }, recv)
@@ -45,18 +45,12 @@ mod task_queue {
4545
}
4646
}
4747

48-
#[cfg_attr(test, linkage = "available_externally")]
49-
#[export_name = "_ZN16__rust_internals3std3sys3sgx6thread15TASK_QUEUE_INITE"]
50-
static TASK_QUEUE_INIT: Once = Once::new();
5148
#[cfg_attr(test, linkage = "available_externally")]
5249
#[export_name = "_ZN16__rust_internals3std3sys3sgx6thread10TASK_QUEUEE"]
53-
static mut TASK_QUEUE: Option<Mutex<Vec<Task>>> = None;
50+
static TASK_QUEUE: Mutex<Vec<Task>> = Mutex::new(Vec::new());
5451

5552
pub(super) fn lock() -> MutexGuard<'static, Vec<Task>> {
56-
unsafe {
57-
TASK_QUEUE_INIT.call_once(|| TASK_QUEUE = Some(Default::default()));
58-
TASK_QUEUE.as_ref().unwrap().lock().unwrap()
59-
}
53+
TASK_QUEUE.lock().unwrap()
6054
}
6155
}
6256

@@ -101,7 +95,7 @@ pub mod wait_notify {
10195

10296
impl Thread {
10397
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
104-
pub unsafe fn new(_stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
98+
pub unsafe fn new(_stack: usize, p: Box<dyn FnOnce() + Send>) -> io::Result<Thread> {
10599
let mut queue_lock = task_queue::lock();
106100
unsafe { usercalls::launch_thread()? };
107101
let (task, handle) = task_queue::Task::new(p);

std/src/thread/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ impl Builder {
561561
let main = Box::new(main);
562562
// SAFETY: dynamic size and alignment of the Box remain the same. See below for why the
563563
// lifetime change is justified.
564-
let main = unsafe { Box::from_raw(Box::into_raw(main) as *mut (dyn FnOnce() + 'static)) };
564+
let main =
565+
unsafe { Box::from_raw(Box::into_raw(main) as *mut (dyn FnOnce() + Send + 'static)) };
565566

566567
Ok(JoinInner {
567568
// SAFETY:
@@ -1544,7 +1545,7 @@ struct Packet<'scope, T> {
15441545
// The type `T` should already always be Send (otherwise the thread could not
15451546
// have been created) and the Packet is Sync because all access to the
15461547
// `UnsafeCell` synchronized (by the `join()` boundary), and `ScopeData` is Sync.
1547-
unsafe impl<'scope, T: Sync> Sync for Packet<'scope, T> {}
1548+
unsafe impl<'scope, T: Send> Sync for Packet<'scope, T> {}
15481549

15491550
impl<'scope, T> Drop for Packet<'scope, T> {
15501551
fn drop(&mut self) {

0 commit comments

Comments
 (0)