Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove with_runtime()
  • Loading branch information
djc committed Jun 16, 2024
commit 710ef22adb83940a8f57921221f181278cb8c00d
14 changes: 7 additions & 7 deletions src/bin/rustup-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rustup::cli::rustup_mode;
#[cfg(windows)]
use rustup::cli::self_update;
use rustup::cli::setup_mode;
use rustup::currentprocess::{with_runtime, Process};
use rustup::currentprocess::Process;
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
use rustup::errors::RustupError;
use rustup::is_proxyable_tools;
Expand All @@ -38,19 +38,19 @@ fn main() {
pre_rustup_main_init();

let process = Process::os();
let mut builder = Builder::new_multi_thread();
builder.enable_all();
with_runtime(process.clone(), builder, {
async move {
Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async move {
match maybe_trace_rustup(&process).await {
Err(e) => {
common::report_error(&e, &process);
std::process::exit(1);
}
Ok(utils::ExitCode(c)) => std::process::exit(c),
}
}
});
});
}

async fn maybe_trace_rustup(process: &Process) -> Result<utils::ExitCode> {
Expand Down
91 changes: 1 addition & 90 deletions src/currentprocess.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::env;
use std::ffi::OsString;
use std::fmt::Debug;
use std::future::Future;
use std::io;
use std::panic;
use std::io::IsTerminal;
use std::path::PathBuf;
use std::sync::Once;
use std::{cell::RefCell, io::IsTerminal};
#[cfg(feature = "test")]
use std::{
collections::HashMap,
Expand Down Expand Up @@ -142,92 +139,6 @@ impl home::env::Env for Process {
}
}

static HOOK_INSTALLED: Once = Once::new();

fn ensure_hook() {
HOOK_INSTALLED.call_once(|| {
let orig_hook = panic::take_hook();
panic::set_hook(Box::new(move |info| {
clear_process();
orig_hook(info);
}));
});
}

/// Run a function in the context of a process definition and a tokio runtime.
///
/// The process state is injected into a thread-local in every work thread of
/// the runtime, but this requires access to the runtime builder, so this
/// function must be the one to create the runtime.
pub fn with_runtime<'a, R>(
process: Process,
mut runtime_builder: tokio::runtime::Builder,
fut: impl Future<Output = R> + 'a,
) -> R {
ensure_hook();

let start_process = process.clone();
let unpark_process = process.clone();
let runtime = runtime_builder
// propagate to blocking threads
.on_thread_start(move || {
// assign the process persistently to the thread local.
PROCESS.with(|p| {
if let Some(old_p) = &*p.borrow() {
panic!("current process already set {old_p:?}");
}
*p.borrow_mut() = Some(start_process.clone());
// Thread exits will clear the process.
});
})
.on_thread_stop(move || {
PROCESS.with(|p| {
*p.borrow_mut() = None;
});
})
// propagate to async worker threads
.on_thread_unpark(move || {
// assign the process persistently to the thread local.
PROCESS.with(|p| {
if let Some(old_p) = &*p.borrow() {
panic!("current process already set {old_p:?}");
}
*p.borrow_mut() = Some(unpark_process.clone());
// Thread exits will clear the process.
});
})
.on_thread_park(move || {
PROCESS.with(|p| {
*p.borrow_mut() = None;
});
})
.build()
.unwrap();

// The current thread doesn't get hooks run on it.
PROCESS.with(move |p| {
if let Some(old_p) = &*p.borrow() {
panic!("current process already set {old_p:?}");
}
*p.borrow_mut() = Some(process.clone());
let result = runtime.block_on(async {
let _guard = crate::cli::log::tracing_subscriber(&process).set_default();
fut.await
});
*p.borrow_mut() = None;
result
})
}

/// Internal - for the panic hook only
fn clear_process() {
PROCESS.with(|p| p.replace(None));
}

thread_local! {
pub(crate) static PROCESS: RefCell<Option<Process>> = const { RefCell::new(None) };
}

// ----------- real process -----------------

#[derive(Clone, Debug)]
Expand Down