Skip to content

D3D12.X: a Release on a thread XCL did not create is made on a Wine thread - #2498

Draft
vakaobr wants to merge 4 commits into
xcl-dev:mainfrom
vakaobr:d3d12x/defer-foreign-thread-release-on-2445
Draft

vakaobr wants to merge 4 commits into
xcl-dev:mainfrom
vakaobr:d3d12x/defer-foreign-thread-release-on-2445

Conversation

@vakaobr

@vakaobr vakaobr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Stacked on #2445; only the top commit belongs here. Part of #258. It replaces #2474, which added a second ring next to #2445's.

A Release that D3DMetal makes on a thread of its own (no Wine TEB) runs none of XCL's release path there: the caller's reference goes into #2445's late-release ring, and a drain gives it back through res_release on a Wine thread. With #2445 a kept last Release is already queued; the rest of res_release still ran on the host's thread, so a last Release that is not kept (XCL_LATE_RELEASE=0, or two last-but-one releases that miss each other) took XCL's locks there, and a contended one waits in Wine's ntdll forever.

What is in it

  • crates/xcl/src/hle/d3d12x/late_release.rs: is_foreign_thread (on a native host, D3D12.X: a native host's last Release only queues the resource #2445's real::native_host, a stack at or above 0x7000_0000_0000; measured on an M5 Pro: Wine threads 0x0180ea9f..0x0220fb37, D3DMetal 0x7ff2063df667..0x7ff2066c3677); defer_release puts the reference into D3D12.X: a native host's last Release only queues the resource #2445's ring with bit 0 set (OWED; object addresses are aligned), because an entry there otherwise means "released for good, one reference kept" and a foreign Release may not be the last; drain_known runs owed entries through real::res_release and the others through D3D12.X: a native host's last Release only queues the resource #2445's path unchanged. Tests wine_thread_is_not_foreign, spawned_threads_are_not_foreign, force_foreign_flag_overrides_detection.
  • crates/xcl/src/hle/d3d12x/real.rs: on a foreign thread res_release reads the count with the host's AddRef / Release, defers the caller's reference and returns the count it will have. Test a_release_on_a_foreign_thread_is_given_back_by_a_drain.

Tested

Regression run

  • None: I have no title of my own to run scripts/smoke.ps1 on.

Not tested

  • Windows, Wine on Linux, the whole suite, any title.
  • A real D3DMetal thread taking the new path in a test: the foreign thread is forced on a Wine thread (FORCE_FOREIGN).
  • The detection tests run with NATIVE_HOST unset (no device in their process); the stack check on a Wine thread with it set is covered only by the new GPU test.

AI use

vakaobr and others added 4 commits November 25, 2026 21:42
D3DMetal calls Release on adopted resources from a native thread of its
own at submit time.  That thread has no Wine TEB, so the first-time
std::env::var read inside the OnceLock in late_release::enabled() enters
Wine's ntdll and never returns.

Reading the setting once in adopt(), on the Wine thread that creates the
object, makes sure the OnceLock is already set before the host's thread
can reach it.

Part of xcl-dev#258.

Assisted-by: Claude Code (Claude Opus 4.6 (1M context))
The steps that drop what XCL keeps by a released resource's address (the maps, sparse pins,
title textures, tiled textures, its unified-memory block) move from res_release into
real::forget_released, unchanged and in the same order. The next commit calls it from the
late-release thread as well. No change in behaviour.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Apple's D3DMetal (CrossOver, the Game Porting Toolkit) calls Release on threads of its own (its
queue thread at submit, for a barrier's resource) and from inside calls XCL makes while XCL
holds a lock of its own. When that Release is the title's last one and late release keeps the
object, res_release took the maps' locks, the sparse state's and the texture table's, and
late_release::hold signalled the queues and waited. Whatever the host's thread waits for there,
its queue waits for too. (The one such wait seen in a retail run on the Mac port, sparse's
forget_buffer, no longer waits since xcl-dev#1981; the others are the same shape and are not waited
for here either.)

On a host whose D3D12 is in no PE module (class_for: module_of of the table's first method is
0, noted once when the first such table is adopted) a kept last Release now writes the resource
into a ring of atomics (late_release::hand_over) and returns: no lock, no fence, no allocation,
no thread start, no environment read (xcl-dev#1789 reads XCL_LATE_RELEASE at adoption). The next
drain_known, on the late-release thread (started at adoption on such a host) or a submit,
forgets what XCL keeps by the address (real::forget_released, the old body of res_release) and
holds the object like any other; the address cannot come again before that, the object is still
there. With the ring full (16,384 slots) the reference is kept for good and counted.

D3D12 on Windows and vkd3d-proton are PE modules: there the flag is never set, the ring stays
empty (drain_known reads two atomics) and a Release does what it did.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hread

D3DMetal calls Release on adopted resources from native threads of its
own (its queue thread, at submit). Such a thread has no Wine TEB, and a
std Mutex that is contended there waits in Wine's ntdll and never
returns. With the previous commit a kept last Release on a native host
only goes into the late-release ring. The rest of res_release still ran
on the host's thread: a last Release that is not kept (XCL_LATE_RELEASE=0,
or two last-but-one releases that miss each other, whose count reaches 0
in the second Release) forgets what XCL keeps by the address there, with
the maps' locks, the sparse state's and the texture table's.

Now, on a native host (real::native_host), a Release on such a thread
runs none of res_release there. The host's own AddRef and Release read
the count to return, and the caller's reference goes into the same ring
as the kept ones, with bit 0 of the address set (OWED: a Release still
owed, not a resource already released for good). drain_known, on the
late-release thread or at a submit, runs each owed one through
res_release on its own thread. Whether it is the last one is not decided
on the host's thread: a Release on another thread between that check and
ours could still take the count to 0 there. With the ring full the
reference is kept for good, as for the kept ones.

A thread is foreign when its stack is at or above 0x7000_0000_0000.
Measured on an M5 Pro (CrossOver 26.3, Rosetta 2): Wine threads
0x0180ea9f..0x0220fb37, D3DMetal threads 0x7ff2063df667..0x7ff2066c3677.
The TEB pointer (gs:[0x30]) is not read: that read hangs on a D3DMetal
thread. The check is made only on a native host: on Wine on Linux a Wine
thread's stack may sit in that range.

Measured on a MacBook Pro M5 Pro, CrossOver 26.3.0 with D3DMetal 3.0
(the test device's tables are in no PE module, so NATIVE_HOST is set),
each test in its own process: the late_release tests, the
native_host_tests and the new
a_release_on_a_foreign_thread_is_given_back_by_a_drain pass; that test
fails without the new branch in res_release ("the foreign thread's
Release was made on that thread": the count was 1, not 2, when it
returned).

Part of xcl-dev#258.
@SlopTart

SlopTart commented Nov 25, 2026 •

Copy link
Copy Markdown

gpu_ suite (cold cache): 164 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
Targeted run of all nine new tests (wine_thread_is_not_foreign, spawned_threads_are_not_foreign, a_release_on_a_foreign_thread_is_given_back_by_a_drain, the_ring_gives_back_what_was_handed_over_in_order, writers_and_readers_at_once_lose_nothing_and_take_nothing_twice, a_full_ring_keeps_the_resource_and_does_not_wait, only_a_table_in_no_pe_module_makes_a_native_host, a_kept_last_release_on_a_native_host_waits_for_no_lock, force_foreign_flag_overrides_detection), plus existing tests matching the same substrings: 22 passed, 1 ignored. This is squarely Wine-lane territory: under wine-11.0 the PE-table native-host detection and the wine-thread non-foreign classification both behave as the PR intends.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

vakaobr added a commit to vakaobr/xcl that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants