From e38cb65f457acfc1cc2d0523ab9a5bfbe4446edc Mon Sep 17 00:00:00 2001 From: ivall Date: Mon, 23 Nov 2026 10:42:51 +0100 Subject: [PATCH] XAsyncSchedule: a terminated queue takes no new work either Like XTaskQueueSubmitDelayedCallback since #57 (libHttpClient schedules through it): E_ABORT, so XAsyncRun / XCL's own operations on a terminated queue fail through the block (#56) and their work never runs. Test async_work_on_a_terminated_queue_is_aborted; the task queue module comment follows #54 / #57. --- crates/xcl/src/hle/xgameruntime/taskqueue.rs | 8 ++++++- crates/xcl/src/hle/xgameruntime/xasync.rs | 25 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/xgameruntime/taskqueue.rs b/crates/xcl/src/hle/xgameruntime/taskqueue.rs index 615189f..f955ea4 100644 --- a/crates/xcl/src/hle/xgameruntime/taskqueue.rs +++ b/crates/xcl/src/hle/xgameruntime/taskqueue.rs @@ -2,7 +2,9 @@ //! //! Dispatch modes: Manual (caller pumps `XTaskQueueDispatch`), ThreadPool, //! SerializedThreadPool (pool, one at a time) and Immediate (run on the submitting -//! thread). Callbacks submitted after `XTaskQueueTerminate` run with `canceled = true`. +//! thread). `XTaskQueueTerminate` runs the queue's pending callbacks (delayed ones included) +//! with `canceled = true` before its own callback; after it the queue takes no new work +//! (`E_ABORT`, like libHttpClient's `VerifyNotTerminated`). //! //! Ports are reference-counted (`Arc`) so composite queues created via //! `XTaskQueueGetPort` + `XTaskQueueCreateComposite` can share underlying work/completion @@ -213,6 +215,10 @@ impl Queue { }) } + pub fn is_terminated(&self) -> bool { + self.terminated.load(Ordering::Acquire) + } + fn done_one(&self) { if self.inflight.fetch_sub(1, Ordering::AcqRel) == 1 { let _g = self.idle.0.lock().unwrap(); diff --git a/crates/xcl/src/hle/xgameruntime/xasync.rs b/crates/xcl/src/hle/xgameruntime/xasync.rs index a7bb281..c67d5e0 100644 --- a/crates/xcl/src/hle/xgameruntime/xasync.rs +++ b/crates/xcl/src/hle/xgameruntime/xasync.rs @@ -250,6 +250,10 @@ pub unsafe extern "system" fn XAsyncSchedule(b: *mut XAsyncBlock, delay_ms: u32) let Some(q) = taskqueue::queue_from((*b).queue) else { return if (*b).queue.is_null() { taskqueue::E_NO_TASK_QUEUE } else { E_INVALIDARG }; }; + // a terminated queue takes no new work (libHttpClient: XTaskQueueSubmitDelayedCallback) + if q.is_terminated() { + return E_ABORT; + } taskqueue::submit_delayed(&q, PORT_WORK, delay_ms, Item { cb: do_work_cb as usize, ctx: b as usize }); S_OK } @@ -727,4 +731,25 @@ mod tests { assert_eq!(XAsyncGetStatus(&mut b, true), 3); } } + + /// A terminated queue takes no new async work: the call fails with E_ABORT through the + /// block, and the work never runs. + #[test] + fn async_work_on_a_terminated_queue_is_aborted() { + let _g = crate::hle::xgameruntime::xasync::ASYNC_TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + unsafe { + let mut q = std::ptr::null_mut(); + assert_eq!(taskqueue::XTaskQueueCreate(taskqueue::MODE_THREAD_POOL, taskqueue::MODE_THREAD_POOL, &mut q), S_OK); + assert_eq!(taskqueue::XTaskQueueTerminate(q, true, std::ptr::null_mut(), 0), S_OK); + let mut b: XAsyncBlock = std::mem::zeroed(); + b.queue = q; + assert_eq!(XAsyncRun(&mut b, run_work as usize), S_OK); + assert_eq!(XAsyncGetStatus(&mut b, true), E_ABORT); + let mut b: XAsyncBlock = std::mem::zeroed(); + b.queue = q; + assert_eq!(begin_with(&mut b, 9, || -> Result, HRESULT> { panic!("work ran on a terminated queue") }), S_OK); + assert_eq!(XAsyncGetStatus(&mut b, true), E_ABORT); + taskqueue::XTaskQueueCloseHandle(q); + } + } }