Keep RivaTuner Statistics Server's profile as the exit put it back, so the game's limit no longer returns to it.

rtss_limit_restore put the profile back and set the want to unset, but the game presents on until the process ends
(the rest of the exit runs after it). The next frame_step_cap_ns called rtss_limit_want with the limit, which replaced
the unset want, and the worker's next poll applied it again: the profile kept the game's limit after the exit, and the
next run took it for the player's own. An exit before the first want, or while RTSS was kept out, found the worker not
started, and a want after it started the worker, whose first poll applied at once. A want the worker read before the
restore was applied after it too, since the worker reads the want before it takes its lock.

rtss_limit_restore now marks the run closed before anything else: rtss_limit_want returns at once, the worker skips
its polls once it holds the lock, and rtss_limit_active and rtss_limit_state read 0, so the game's own limiter paces
the frames left.

tests/rtss_limit_check.py builds tests/rtss_worker_check.c, which compiles the real rtss_limit.c against a fake RTSS
(its registry key, hooks library, profiles and shared memory) and runs each scenario in a process of its own: a want
after the restore, the player's own limit, a want read before the restore, and an exit before the first want or while
RTSS was kept out. All five failed before the change and pass after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:07:41 -05:00
parent 2f640fb8ed
commit a1153aa079
3 changed files with 387 additions and 2 deletions

View File

@@ -53,6 +53,7 @@ static PlatMutex lock = PLAT_MUTEX_INIT; // the hooks calls and the state belo
static _Atomic int64_t want = -2; // millihertz from the pacer: >0 a limit, 0 none, -1 RTSS kept out; -2 unset
static _Atomic int active; // RTSS limits this process now
static _Atomic int failed; // FAIL_*: RTSS is out of it for the rest of the run
static _Atomic int closed; // rtss_limit_restore ran: nothing reaches the profile any more
static int started;
static char dll_path[MAX_PATH], profile[MAX_PATH], rtss_dir[MAX_PATH];
static HMODULE dll;
@@ -249,6 +250,10 @@ static void worker(void *arg) {
int64_t w = atomic_load(&want);
if (w == -2) continue;
plat_mutex_lock(&lock);
if (atomic_load(&closed)) { // a want read before rtss_limit_restore must not reach the profile after it
plat_mutex_unlock(&lock);
continue;
}
int hooked;
double fps;
int run = rtss_running(&hooked, &fps), f = atomic_load(&failed);
@@ -320,6 +325,7 @@ static void start(void) {
}
void rtss_limit_want(int64_t mhz) {
if (atomic_load_explicit(&closed, memory_order_relaxed)) return; // the exit put the profile back
if (!started && mhz < 0) return; // kept out from the start, for the whole run (both reasons need a restart)
if (!started) start();
// a new limit: the game's own limiter holds it until the worker had RTSS apply it, up to POLL_MS later (a movie's
@@ -327,21 +333,28 @@ void rtss_limit_want(int64_t mhz) {
if (atomic_exchange(&want, mhz) != mhz) atomic_store(&active, 0);
}
int rtss_limit_active(void) { return atomic_load_explicit(&active, memory_order_relaxed); }
int rtss_limit_active(void) {
return !atomic_load_explicit(&closed, memory_order_relaxed) && atomic_load_explicit(&active, memory_order_relaxed);
}
int rtss_limit_state(void) {
if (atomic_load_explicit(&closed, memory_order_relaxed)) return 0;
int f = atomic_load_explicit(&failed, memory_order_relaxed);
if (atomic_load(&want) >= 0 && (f == FAIL_READONLY || f == FAIL_NOT_LIMITING)) return f == FAIL_READONLY ? 2 : 3;
return rtss_limit_active() && atomic_load(&want) > 0;
}
void rtss_limit_restore(void) {
// The game presents on after this call (the rest of the exit runs first), so from here the game's own limiter holds
// the frames left and no want reaches the profile again: the worker would otherwise write the game's limit back into
// the profile just put back, and the next run would take it for the player's own. Set before the check below, so
// that an exit before the first limit, or while RTSS was kept out, also stops a later want from starting the worker.
atomic_store(&closed, 1);
if (!started || !*profile) return;
// the exit may come from a crash on any thread: give up after a second rather than wait on a lock held there
for (int i = 0; i < 100; i++) {
if (plat_mutex_trylock(&lock)) {
if (dll) put_back();
atomic_store(&want, -2); // the worker leaves the profile alone from now on
plat_mutex_unlock(&lock);
return;
}