improve tokio setup (#1024)

* improve tokio setup

* more

* more

* Revert "more"

This reverts commit e0e1db4482.

* more

* more
This commit is contained in:
Logan McNaughton
2026-06-06 22:44:30 +02:00
committed by GitHub
parent 21238c8e41
commit 8b3a1a40f2
6 changed files with 38 additions and 29 deletions
+2 -3
View File
@@ -47,7 +47,7 @@ pub mod tlb;
pub mod unmapped;
pub mod vi;
pub async fn run_game(
pub fn run_game(
device: &mut Device,
rom_contents: &[u8],
game_settings: ui::GameSettings,
@@ -76,7 +76,6 @@ pub async fn run_game(
// must be after video init
let (discord_watch_tx, discord_handle) = if ra_config.enabled && device.netplay.is_none() {
retroachievements::load_game(rom_contents, rom_contents.len(), ra_config.rich_presence)
.await
} else {
(None, None)
};
@@ -103,7 +102,7 @@ pub async fn run_game(
cpu::run(device);
retroachievements::unload_game(discord_watch_tx, discord_handle).await;
retroachievements::unload_game(discord_watch_tx, discord_handle);
ui::storage::write_saves(device);
ui::input::close(&mut device.ui);
+9 -6
View File
@@ -104,7 +104,7 @@ fn set_app_id() {
}
}
pub async fn run(args: Args, arg_count: usize) -> std::io::Result<()> {
pub fn run(args: Args, arg_count: usize) -> std::io::Result<()> {
let dirs = ui::get_dirs();
std::fs::create_dir_all(&dirs.config_dir)?;
@@ -228,7 +228,11 @@ pub async fn run(args: Args, arg_count: usize) -> std::io::Result<()> {
tx.send(false).unwrap();
}
rx.await.unwrap();
let join_handle = std::thread::spawn(move || {
rx.blocking_recv().unwrap();
});
join_handle.join().unwrap();
ra_config
} else {
retroachievements::RAConfig::default()
@@ -244,8 +248,7 @@ pub async fn run(args: Args, arg_count: usize) -> std::io::Result<()> {
load_savestate_slot: args.load_state,
},
ra_config,
)
.await;
);
// on Android, the client is shut down in the app_window function
#[cfg(not(target_os = "android"))]
@@ -266,7 +269,7 @@ pub async fn run(args: Args, arg_count: usize) -> std::io::Result<()> {
}
}
ui::usb::close(shutdown_tx, usb_handle).await;
ui::usb::close(shutdown_tx, usb_handle);
} else if arg_count > 1 {
let mut config = ui::config::Config::new();
@@ -330,7 +333,7 @@ pub async fn run(args: Args, arg_count: usize) -> std::io::Result<()> {
#[cfg(target_os = "android")]
#[unsafe(no_mangle)]
#[tokio::main(worker_threads = 4)]
#[tokio::main]
async fn android_main(app: slint::android::AndroidApp) {
slint::android::init_with_event_listener(app.clone(), move |event| match event {
slint::android::android_activity::PollEvent::Main(main_event) => match main_event {
+2 -2
View File
@@ -5,8 +5,8 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
use clap::Parser;
#[tokio::main(worker_threads = 4)]
#[tokio::main]
async fn main() -> std::io::Result<()> {
let args = gopher64::Args::parse();
gopher64::run(args, std::env::args().count()).await
gopher64::run(args, std::env::args().count())
}
+14 -9
View File
@@ -137,7 +137,7 @@ pub extern "C" fn rust_server_call(
});
}
pub async fn load_game(
pub fn load_game(
rom: &[u8],
rom_size: usize,
discord_rich_presence: bool,
@@ -150,7 +150,10 @@ pub async fn load_game(
let tx_ptr = Box::into_raw(Box::new(tx)) as *mut std::ffi::c_void;
ra_load_game(rom.as_ptr(), rom_size, tx_ptr);
};
rx.await.unwrap();
let join_handle = std::thread::spawn(move || {
rx.blocking_recv().unwrap();
});
join_handle.join().unwrap();
let mut c_title = std::ptr::null();
let mut c_image_url = std::ptr::null();
@@ -177,7 +180,7 @@ pub async fn load_game(
}
}
pub async fn unload_game(
pub fn unload_game(
discord_watch_tx: Option<tokio::sync::watch::Sender<()>>,
discord_handle: Option<tokio::task::JoinHandle<()>>,
) {
@@ -185,12 +188,14 @@ pub async fn unload_game(
&& let Some(discord_watch_tx) = discord_watch_tx
{
let _ = discord_watch_tx.send(());
if tokio::time::timeout(std::time::Duration::from_secs(1), &mut discord_handle)
.await
.is_err()
{
discord_handle.abort();
}
tokio::task::spawn(async move {
if tokio::time::timeout(std::time::Duration::from_secs(1), &mut discord_handle)
.await
.is_err()
{
discord_handle.abort();
}
});
}
unsafe { ra_unload_game() };
}
+2 -2
View File
@@ -158,14 +158,14 @@ fn argv_to_strings(argc: std::ffi::c_int, argv: *mut *mut std::ffi::c_char) -> V
}
#[unsafe(no_mangle)]
#[tokio::main(worker_threads = 4)]
#[tokio::main]
pub async extern "C" fn gopher64_sdl_main(
argc: std::ffi::c_int,
argv: *mut *mut std::ffi::c_char,
) -> std::ffi::c_int {
let raw = argv_to_strings(argc, argv);
let args = Args::try_parse_from(raw).unwrap();
if let Err(err) = run(args, argc as usize).await {
if let Err(err) = run(args, argc as usize) {
eprintln!("Error running game: {err:?}");
return 1;
}
+9 -7
View File
@@ -174,7 +174,7 @@ pub fn init() -> (
)
}
pub async fn close(
pub fn close(
shutdown_tx: Option<tokio::sync::watch::Sender<()>>,
usb_handle: Option<tokio::task::JoinHandle<()>>,
) {
@@ -182,12 +182,14 @@ pub async fn close(
&& let Some(shutdown_tx) = shutdown_tx
{
let _ = shutdown_tx.send(());
if tokio::time::timeout(std::time::Duration::from_secs(1), &mut handle)
.await
.is_err()
{
handle.abort();
}
tokio::task::spawn(async move {
if tokio::time::timeout(std::time::Duration::from_secs(1), &mut handle)
.await
.is_err()
{
handle.abort();
}
});
}
}