Skip to content

Commit

Permalink
Rollup merge of rust-lang#125800 - fortanix:raoul/rte-99-fix_mut_stat…
Browse files Browse the repository at this point in the history
…ic_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``
  • Loading branch information
matthiaskrgr authored Jun 5, 2024
2 parents 1632165 + ea7e91c commit 7450cf0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
18 changes: 6 additions & 12 deletions std/src/sys/pal/sgx/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use self::task_queue::JoinNotifier;

mod task_queue {
use super::wait_notify;
use crate::sync::{Mutex, MutexGuard, Once};
use crate::sync::{Mutex, MutexGuard};

pub type JoinHandle = wait_notify::Waiter;

Expand All @@ -28,12 +28,12 @@ mod task_queue {
}

pub(super) struct Task {
p: Box<dyn FnOnce()>,
p: Box<dyn FnOnce() + Send>,
done: JoinNotifier,
}

impl Task {
pub(super) fn new(p: Box<dyn FnOnce()>) -> (Task, JoinHandle) {
pub(super) fn new(p: Box<dyn FnOnce() + Send>) -> (Task, JoinHandle) {
let (done, recv) = wait_notify::new();
let done = JoinNotifier(Some(done));
(Task { p, done }, recv)
Expand All @@ -45,18 +45,12 @@ mod task_queue {
}
}

#[cfg_attr(test, linkage = "available_externally")]
#[export_name = "_ZN16__rust_internals3std3sys3sgx6thread15TASK_QUEUE_INITE"]
static TASK_QUEUE_INIT: Once = Once::new();
#[cfg_attr(test, linkage = "available_externally")]
#[export_name = "_ZN16__rust_internals3std3sys3sgx6thread10TASK_QUEUEE"]
static mut TASK_QUEUE: Option<Mutex<Vec<Task>>> = None;
static TASK_QUEUE: Mutex<Vec<Task>> = Mutex::new(Vec::new());

pub(super) fn lock() -> MutexGuard<'static, Vec<Task>> {
unsafe {
TASK_QUEUE_INIT.call_once(|| TASK_QUEUE = Some(Default::default()));
TASK_QUEUE.as_ref().unwrap().lock().unwrap()
}
TASK_QUEUE.lock().unwrap()
}
}

Expand Down Expand Up @@ -101,7 +95,7 @@ pub mod wait_notify {

impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(_stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
pub unsafe fn new(_stack: usize, p: Box<dyn FnOnce() + Send>) -> io::Result<Thread> {
let mut queue_lock = task_queue::lock();
unsafe { usercalls::launch_thread()? };
let (task, handle) = task_queue::Task::new(p);
Expand Down
5 changes: 3 additions & 2 deletions std/src/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ impl Builder {
let main = Box::new(main);
// SAFETY: dynamic size and alignment of the Box remain the same. See below for why the
// lifetime change is justified.
let main = unsafe { Box::from_raw(Box::into_raw(main) as *mut (dyn FnOnce() + 'static)) };
let main =
unsafe { Box::from_raw(Box::into_raw(main) as *mut (dyn FnOnce() + Send + 'static)) };

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

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

0 comments on commit 7450cf0

Please sign in to comment.