Review follow-up for #1564:
- Registration and the cleanup of exit, quick_exit and _cexit hold one recursive lock of the run's own, from before the thread-local callback through the table's drain (and _cexit's stream flush), as the UCRT holds its exit lock: two threads never clean up at once, and a registration from another thread waits for the cleanup. The exiting thread may still register from a handler, _cexit still returns, and the lock is released before the run ends (end_run), also when a handler ends the run from inside a drain.
- Each guest cleanup callback (atexit, at_quick_exit, the thread-local destructor callback) runs inside a C++ exception boundary: one that throws ends the run through game_abort, as the UCRT's terminate() would (exit code 3), instead of leaving the process with 0xE06D7363.
tiny_atexit gains four modes, held to the native sequence: concurrent_cexit (a second thread's _cexit, with a thread_local of its own, waits for the cleanup), quick_exit_register (an at_quick_exit from a second thread during a quick_exit drain waits, so its handler never runs), quick_exit_throw and exit_throw (exit code 3). In the concurrent modes the slow handler waits until the second thread is about to call, so the sequence is fixed under any scheduling with the lock. All four fail on c5b51e9 through --run.
The game's at_quick_exit() (_crt_at_quick_exit) and _register_thread_local_exe_atexit_callback still
reached the host CRT, which the game shares with gta6rt. Under `gta6rt --run`:
- quick_exit() ran none of the game's at_quick_exit handlers;
- exit() and _cexit() never ran the exiting thread's thread_local destructors, and a second
registration in the host's one-slot callback would terminate gta6rt.
Now _crt_at_quick_exit goes to a second table of the run's own, which quick_exit() runs newest first
under the exit lock; _exit() and _Exit() run none (they no longer share game_quick_exit). The thread-local
callback is the run's own: exit() and _cexit() call it with DLL_PROCESS_DETACH before the atexit handlers,
as the UCRT does, and a second registration aborts, as the UCRT's terminate() does. Both reset per run.
tiny_atexit gains five modes (at_quick_exit, at_quick_exit_exit, at_quick_exit__exit, thread_local_exit,
thread_local_cexit). Their expected sequences in check_atexit.cmake were measured by running the fixture
natively on Windows 11; before this change --run differed on at_quick_exit, thread_local_exit and
thread_local_cexit. run_tiny_atexit now holds 13 paths to the Windows sequence.
#2315 kept `_register_thread_local_exe_atexit_callback` away from the host UCRT, which fast-fails a second
registration in the process (gta6rt's own startup makes the first). Review points fixed on top:
- The callback is a `_tls_callback_type` (UCRT process.h: `void (__stdcall*)(void*, unsigned long, void*)`), not
`void (*)()`: vcruntime's TLS destructor callback reads its reason argument, which was left as whatever rdx held.
It is now called as `(nullptr, DLL_PROCESS_DETACH, nullptr)`, as the UCRT does.
- `exit` and `_cexit` call it before the atexit table, as the UCRT does: the leaving thread's thread_local objects end
before the static ones ([basic.start.term]). #2315 ran it after the static destructors, and not in `_cexit`.
- The extra call on every thread detach is dropped, with the per-thread flag and the loader's include of the process
shim: the loader already calls the same function from the executable's TLS directory with DLL_THREAD_DETACH, and
the UCRT never calls the registered one there. The call after a returning entry point is dropped too (MSVC's
mainCRTStartup calls exit itself).
- run_tiny_atexit gains thread_local_exit and thread_local_cexit: a thread_local object of the main thread must end
before the handlers and destructors. On main the --run path never ends it (Wine) or stops at the second
registration (Windows).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
run_tiny_atexit compared `gta6rt --run` with the native run. Under Wine the
native leg uses Wine's own CRT, which differs from Windows in two places
(it drops a handler registered while handlers run, and its _exit from a
second thread does not wait for the exit lock), so the test failed there
although --run gave the Windows sequence (reported on #2152).
--run is now held to the expected sequence directly. On Windows this is the
same check, since the native run must match that sequence too. With RUNNER
set (Wine), a native difference is reported without failing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The mapped EXE registered atexit handlers and static destructors with the
host CRT's process-wide table, which gta6rt never runs, so they were lost.
They now go to a table of the run's own: exit() and a return from main run
it newest first (including handlers registered during exit), _cexit() runs
it and returns, and _exit/_Exit/quick_exit/ExitProcess/abort skip it, as
natively. exit/_exit/_Exit/quick_exit share an exit lock like the CRT's,
each run starts with an empty table, and a failed registration returns an
error instead of throwing into the game.
New run_tiny_atexit compares a stand-in program run natively and through
--run for 8 termination paths. Not covered yet: at_quick_exit and the
thread_local destructor callback.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>