Compare commits

..
Author SHA1 Message Date
ates d01a7c004f xgameruntime: a delayed task-queue callback no longer holds back later callbacks (#775)
Delayed callbacks wait outside the port's FIFO and a thread-pool timer
moves them in when due, as the GDK submits a delayed callback only
after its delay. Closing a queue moves pending delayed tasks in first,
so they are drained or cancelled as before.

Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-12-02 04:57:33 +03:00
3 changed files with 119 additions and 13 deletions

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game
remains unsupported.
### Fixed
- **`xgameruntime`: a delayed task-queue callback no longer holds back the callbacks submitted after it** (#775, part 3). `XTaskQueueSubmitDelayedCallback` put the task in the port's FIFO, and the port waited for it to be due. So one multi-second retry timer blocked every later callback on that port, and on a pool lane a thread-pool thread slept meanwhile. In the GDK the delay elapses before the callback is submitted. Delayed tasks now wait outside the FIFO and a thread-pool timer moves them in when due, so a callback submitted later without a delay runs first. Delayed tasks still pending when the queue closes are drained or cancelled like the rest. `test_xgameruntime` checks both a manual and a serialized queue.
- **`d3d12_xs`: a compute pipeline the host cannot make comes back as the logged null pipeline, so the target build no longer exits with status 5 on its next dispatch** (#722). Console compute shaders are not translated yet (#746), and `CreateComputePipelineStateX` returned `E_NOTIMPL` unless `BX_NULL_RENDER=1` was set. The target build keeps the shader program it was making even then, with no pipeline in it, and the next dispatch through that program read inside the missing pipeline (call-site observation). Refused compute pipelines now return the null pipeline whatever `BX_NULL_RENDER` says, logged as before (`pipeline_refused ... null pipeline returned`); dispatches with it are skipped and logged, as every untranslated dispatch is. Serialized and derived pipelines still fail. New test `test_d3d12_compute_placeholder`.
- **`d3d12_xs.dll`: a placed texture has one host storage; host resources for title memory are released** (#782, #745).
- **One storage per texture.** A texture placed in title memory now gets its contents uploaded into its host shadow itself, not into a side copy. On the console a placed texture has one storage, and now every view and copy command of it sees the texels; before, only views resolved for translated draws did, and copy commands read an empty texture. Read-only shadows are made with simultaneous access, so the upload promotes them from COMMON without barriers that contradict the title's own. A drawn texture no longer occupies a second host texture.
+77 -3
View File
@@ -3,6 +3,7 @@
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <map>
#include <mutex>
#include <new>
#include <unordered_map>
@@ -24,10 +25,22 @@ public:
size_t workers = 0;
bool serial_running = false;
bool closed = false;
// Delayed callbacks wait here, by due time, and enter the FIFO when due (#775). In the
// GDK the delay elapses before the callback is submitted, so a callback submitted later
// without a delay runs first; holding a delayed task at the FIFO's head instead blocked
// every later callback of the port for the whole delay.
std::multimap<uint64_t, std::unique_ptr<Task>> delayed;
PTP_TIMER timer = nullptr;
QueueRef timer_owner; // keeps the queue alive while delayed tasks wait
Lane(Queue* owner, DispatchMode mode) : owner(owner), mode(mode) {}
~Lane() {
// The last reference can be dropped inside the timer callback, so do not wait for it.
if (timer) {
SetThreadpoolTimer(timer, nullptr, 0, 0);
CloseThreadpoolTimer(timer);
}
if (worker) CloseThreadpoolWork(worker);
while (first) pop();
}
@@ -37,6 +50,10 @@ public:
worker = CreateThreadpoolWork(run_worker, this, nullptr);
if (!worker) return HRESULT_FROM_WIN32(GetLastError());
}
if (mode != DispatchMode::immediate) {
timer = CreateThreadpoolTimer(on_timer, this, nullptr);
if (!timer) return HRESULT_FROM_WIN32(GetLastError());
}
return S_OK;
}
@@ -52,6 +69,21 @@ public:
task->run(owner->shared_from_this());
return true;
}
if (task->due_ms != 0 && GetTickCount64() < task->due_ms && !closed) {
const uint64_t due_ms = task->due_ms;
const bool earliest = delayed.empty() || due_ms < delayed.begin()->first;
delayed.emplace(due_ms, std::move(task));
if (!timer_owner) timer_owner = owner->shared_from_this();
if (earliest) arm_timer_locked();
return true;
}
task->due_ms = 0;
enqueue_locked(std::move(task));
return true;
}
// Appends a due task to the FIFO and starts a worker for it. The caller holds `mutex`.
void enqueue_locked(std::unique_ptr<Task> task) {
auto* tail = task.get();
(last ? last->next : first) = std::move(task);
last = tail;
@@ -59,7 +91,45 @@ public:
if (mode == DispatchMode::parallel) schedule();
schedule_serial();
return true;
}
// Moves every due delayed task (or, with `all`, every delayed task) into the FIFO, in
// due order. The caller holds `mutex`.
void release_delayed_locked(bool all) {
const uint64_t now = GetTickCount64();
while (!delayed.empty() && (all || delayed.begin()->first <= now)) {
auto task = std::move(delayed.begin()->second);
delayed.erase(delayed.begin());
task->due_ms = 0;
enqueue_locked(std::move(task));
}
}
// Points the timer at the earliest delayed task. The caller holds `mutex`.
void arm_timer_locked() {
if (!timer) return; // immediate lanes (and composite queues) hold no delayed tasks
if (delayed.empty()) {
SetThreadpoolTimer(timer, nullptr, 0, 0);
return;
}
const uint64_t now = GetTickCount64();
const uint64_t left = delayed.begin()->first > now ? delayed.begin()->first - now : 0;
// A negative FILETIME is relative, in 100 ns units.
const int64_t relative = -static_cast<int64_t>(std::max<uint64_t>(left, 1) * 10000);
FILETIME due{static_cast<DWORD>(relative & 0xFFFFFFFF), static_cast<DWORD>(relative >> 32)};
SetThreadpoolTimer(timer, &due, 0, 0);
}
static void CALLBACK on_timer(PTP_CALLBACK_INSTANCE, void* context, PTP_TIMER) {
auto& lane = *static_cast<Lane*>(context);
QueueRef keep; // released after the lock, since it may destroy the lane
{
std::lock_guard lock(lane.mutex);
keep = lane.timer_owner;
lane.release_delayed_locked(false);
lane.arm_timer_locked();
if (lane.delayed.empty()) lane.timer_owner.reset();
}
}
void schedule() {
@@ -83,8 +153,8 @@ public:
}
}
// FIFO: the head holds the lane until it is due, so a later task never
// overtakes a delayed one. Wait only until that deadline, never spin.
// Tasks enter the FIFO only when due (delayed ones through `delayed`), so the head is
// normally due already; the wait below is kept for safety and never spins.
std::chrono::steady_clock::time_point head_due() const {
const uint64_t now = GetTickCount64();
const uint64_t left = first->due_ms > now ? first->due_ms - now : 0;
@@ -106,6 +176,10 @@ public:
void close(const QueueRef& queue) {
std::unique_lock lock(mutex);
// Delayed tasks join the FIFO, so closing drains or cancels them as it does the rest.
release_delayed_locked(true);
arm_timer_locked();
timer_owner.reset(); // the caller holds its own reference
closed = true;
ready.notify_all();
if (mode != DispatchMode::manual) return;
+41 -10
View File
@@ -386,28 +386,59 @@ void check_delayed_callback(HRESULT(WINAPI* query_api)(const GUID*, const GUID*,
XTaskQueueCloseHandle(queue);
}
// FIFO: a delayed head holds the lane, so a task queued behind it does not
// overtake it. Both run, in submission order.
// A delay elapses before the callback is submitted (GDK XTaskQueueSubmitDelayedCallback), so
// a callback submitted later without a delay runs first, and the delayed one runs once due
// (#775). A delayed task used to hold the head of its port for its whole delay.
{
void* queue = nullptr;
CHECK(XTaskQueueCreate(manual, manual, &queue) == S_OK && queue);
DelayRecord later{};
DelayRecord now{};
CHECK(submit_delayed(core, queue, work, delay_ms, &later, record_delay) == S_OK);
CHECK(submit_delayed(core, queue, work, 0, &now, record_delay) == S_OK);
CHECK(XTaskQueueDispatch(queue, work, 0));
CHECK(now.runs.load() == 1 && later.runs.load() == 0);
CHECK(!XTaskQueueDispatch(queue, work, 0));
Sleep(late_margin_ms);
CHECK(XTaskQueueDispatch(queue, work, 0));
CHECK(later.runs.load() == 1 && later.cancels.load() == 0);
CHECK(!XTaskQueueDispatch(queue, work, 0));
XTaskQueueCloseHandle(queue);
}
// Due tasks keep their submission order among themselves: two delayed callbacks with the
// same delay run in the order they were submitted.
{
void* queue = nullptr;
CHECK(XTaskQueueCreate(manual, manual, &queue) == S_OK && queue);
OrderRecord fifo{};
CHECK(submit_delayed(core, queue, work, delay_ms, &fifo, record_order) == S_OK);
CHECK(submit_delayed(core, queue, work, 0, &fifo, record_order) == S_OK);
CHECK(submit_delayed(core, queue, work, delay_ms, &fifo, record_order) == S_OK);
CHECK(!XTaskQueueDispatch(queue, work, 0));
CHECK(fifo.next.load() == 0);
Sleep(early_margin_ms);
CHECK(!XTaskQueueDispatch(queue, work, 0));
CHECK(fifo.next.load() == 0);
Sleep(late_margin_ms);
CHECK(XTaskQueueDispatch(queue, work, 0));
CHECK(fifo.next.load() == 1 && fifo.first == 1);
CHECK(XTaskQueueDispatch(queue, work, 0));
CHECK(fifo.next.load() == 2 && fifo.second == 2);
CHECK(!XTaskQueueDispatch(queue, work, 0));
CHECK(fifo.next.load() == 2 && fifo.first == 1 && fifo.second == 2);
XTaskQueueCloseHandle(queue);
}
// On a pool (serialized) lane a long retry timer does not hold back later callbacks: the
// immediate callback runs well before the 2.5 s delay ends.
{
void* serial = nullptr;
CHECK(XTaskQueueCreate(serialized, manual, &serial) == S_OK && serial);
DelayRecord retry{};
DelayRecord now{};
CHECK(submit_delayed(core, serial, work, 2500, &retry, record_delay) == S_OK);
CHECK(submit_delayed(core, serial, work, 0, &now, record_delay) == S_OK);
for (int i = 0; i < 1000 && now.runs.load() == 0; ++i) Sleep(1);
CHECK(now.runs.load() == 1 && retry.runs.load() == 0);
// Closing drains the delayed task on a worker; wait for it before `retry` goes away.
XTaskQueueCloseHandle(serial);
for (int i = 0; i < 5000 && retry.runs.load() + retry.cancels.load() == 0; ++i) Sleep(1);
CHECK(retry.runs.load() + retry.cancels.load() == 1);
}
// A delayed submission cancelled before its due time runs as cancelled, not
// as a normal run: closing a manual queue drains it through Task::cancel.
{