From 2b2e0a304b3a2e8c63e42714b7a69b8e97feac68 Mon Sep 17 00:00:00 2001 From: Longno242 Date: Fri, 27 Nov 2026 14:27:16 +0000 Subject: [PATCH] XGameRuntime: dispatch serialized task-queue work on the caller XTaskQueueDispatch returned false for a SerializedThreadPool port even when a callback was queued. The Windows runtime runs that callback on the caller, so claim one queued item the same way a manual port does and leave the pool worker to drain the rest. Co-authored-by: Cursor --- crates/xcl/src/hle/xgameruntime/taskqueue.rs | 121 ++++++++++++++++--- 1 file changed, 101 insertions(+), 20 deletions(-) diff --git a/crates/xcl/src/hle/xgameruntime/taskqueue.rs b/crates/xcl/src/hle/xgameruntime/taskqueue.rs index e5565ad699..1af096b12d 100644 --- a/crates/xcl/src/hle/xgameruntime/taskqueue.rs +++ b/crates/xcl/src/hle/xgameruntime/taskqueue.rs @@ -1,7 +1,8 @@ //! XTaskQueue – GDK's async dispatch primitive (contract: libHttpClient XTaskQueue.h, MIT). //! //! Dispatch modes: Manual (caller pumps `XTaskQueueDispatch`), ThreadPool, -//! SerializedThreadPool (pool, one at a time) and Immediate (run on the submitting +//! SerializedThreadPool (pool, one at a time; an explicit `XTaskQueueDispatch` can +//! also claim one queued callback on its caller) and Immediate (run on the submitting //! 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`). @@ -296,7 +297,7 @@ pub fn submit_now(q: &Arc, port: u32, item: Item) -> bool { let q2 = q.clone(); spawn(Box::new(move || unsafe { q2.execute(port, item) })); } - MODE_SERIALIZED_THREAD_POOL => { + _ => { { let mut items = p.items.lock().unwrap(); { @@ -308,27 +309,15 @@ pub fn submit_now(q: &Arc, port: u32, item: Item) -> bool { } items.push_back(QueuedItem { item, queue: q.clone(), port }); } + p.cv.notify_one(); q.notify_monitors(port); - if !p.running.swap(true, Ordering::AcqRel) { + // Serialization applies to the automatic worker. Explicit dispatch can claim + // the same queue, so a waiter must be woken here too. + if p.mode == MODE_SERIALIZED_THREAD_POOL && !p.running.swap(true, Ordering::AcqRel) { let p2 = p.clone(); spawn(Box::new(move || drain_serialized(p2))); } } - _ => { - { - let mut items = p.items.lock().unwrap(); - { - let mut state = q.state.lock().unwrap(); - if state.terminated { - return false; - } - state.inflight[port as usize] += 1; - } - items.push_back(QueuedItem { item, queue: q.clone(), port }); - } - p.cv.notify_one(); - q.notify_monitors(port); - } } true } @@ -355,10 +344,11 @@ pub fn submit_delayed(q: &Arc, port: u32, delay_ms: u32, item: Item) -> b true } -/// Dispatch one item from a Manual port. Returns true when something ran. +/// Dispatch one queued item on the caller's thread. Returns true when something ran. pub fn dispatch(q: &Arc, port: u32, timeout_ms: u32) -> bool { let p = &q.ports[port as usize]; - if p.mode != MODE_MANUAL { + // Serialization applies to the automatic worker. Explicit callers can also claim queued work. + if !matches!(p.mode, MODE_MANUAL | MODE_SERIALIZED_THREAD_POOL) { return false; } let mut g = p.items.lock().unwrap(); @@ -757,6 +747,97 @@ mod tests { } } + #[test] + fn serialized_work_port_can_dispatch_on_the_caller() { + const TIMEOUT: Duration = Duration::from_secs(5); + + struct First { + started: mpsc::Sender, + release: mpsc::Receiver<()>, + done: mpsc::Sender, + } + + unsafe extern "system" fn first(ctx: *mut c_void, canceled: bool) { + let first = Box::from_raw(ctx.cast::()); + let _ = first.started.send(std::thread::current().id()); + let released = first.release.recv_timeout(TIMEOUT).is_ok(); + let _ = first.done.send(released && !canceled); + } + + type ResultSender = mpsc::Sender<(std::thread::ThreadId, bool)>; + + unsafe extern "system" fn report_thread(ctx: *mut c_void, canceled: bool) { + let done = Box::from_raw(ctx.cast::()); + let _ = done.send((std::thread::current().id(), canceled)); + } + + unsafe fn submit_record(queue: *mut c_void, done: ResultSender) -> HRESULT { + let ctx = Box::into_raw(Box::new(done)); + let hr = XTaskQueueSubmitCallback(queue, PORT_WORK, ctx.cast(), report_thread as usize); + if hr != S_OK { + drop(Box::from_raw(ctx)); + } + hr + } + + let (started_tx, started_rx) = mpsc::channel(); + let (release_tx, release_rx) = mpsc::channel(); + let (first_done_tx, first_done_rx) = mpsc::channel(); + let (second_done_tx, second_done_rx) = mpsc::channel(); + let (third_done_tx, third_done_rx) = mpsc::channel(); + let caller = std::thread::current().id(); + + unsafe { + let mut queue = std::ptr::null_mut(); + assert_eq!(XTaskQueueCreate(MODE_SERIALIZED_THREAD_POOL, MODE_MANUAL, &mut queue), S_OK); + + let first_context = Box::into_raw(Box::new(First { started: started_tx, release: release_rx, done: first_done_tx })); + let first_hr = XTaskQueueSubmitCallback(queue, PORT_WORK, first_context.cast(), first as usize); + if first_hr != S_OK { + drop(Box::from_raw(first_context)); + } + let first_thread = if first_hr == S_OK { started_rx.recv_timeout(TIMEOUT).ok() } else { None }; + let empty_dispatch = first_thread.is_some() && XTaskQueueDispatch(queue, PORT_WORK, 0); + + let mut second_hr = crate::util::E_ABORT; + let mut third_hr = crate::util::E_ABORT; + if first_thread.is_some() { + second_hr = submit_record(queue, second_done_tx); + third_hr = submit_record(queue, third_done_tx); + } + let dispatched = second_hr == S_OK && XTaskQueueDispatch(queue, PORT_WORK, 0); + let early = second_done_rx.try_recv().ok(); + let before_release = early.is_some(); + let third_early = third_done_rx.try_recv().ok(); + + let _ = release_tx.send(()); + let first_released = first_thread.is_some() && first_done_rx.recv_timeout(TIMEOUT) == Ok(true); + let second_done = if second_hr == S_OK { + early.or_else(|| second_done_rx.recv_timeout(TIMEOUT).ok()) + } else { + None + }; + let third_done = if third_hr == S_OK { + third_early.or_else(|| third_done_rx.recv_timeout(TIMEOUT).ok()) + } else { + None + }; + XTaskQueueCloseHandle(queue); + + let on_caller = second_done.as_ref().is_some_and(|(thread, _)| thread == &caller); + assert_eq!(first_hr, S_OK); + assert!(first_released, "first callback must finish after the explicit release"); + assert_ne!(first_thread, Some(caller), "automatic dispatch must use a pool worker"); + assert!(!empty_dispatch, "an in-flight callback is not queued work"); + assert_eq!(second_hr, S_OK); + assert_eq!(third_hr, S_OK); + assert_eq!(second_done.as_ref().map(|(_, canceled)| *canceled), Some(false)); + assert!(dispatched && before_release && on_caller, "explicit dispatch must run the queued callback on its caller"); + assert!(third_early.is_none(), "one explicit dispatch must consume only one callback"); + assert_eq!(third_done, first_thread.map(|thread| (thread, false)), "the original worker must keep draining the port"); + } + } + #[test] fn closed_and_stray_handles_are_not_read() { unsafe {