Take the file lock at the end of a save file call only when a CloseFile is pending, not at the end of every call.

The review of the save file use count found that fs_host.c file_release took `lock` exclusive whenever users dropped
to 0, which is the end of every single read_file, write_file, file_size, set_file_size and flush_file, not only when
a CloseFile had marked the slot. The exclusive acquisition waits for every shared holder, such as the fsync loop of a
non-save Commit (commit, FlushFileBuffers under the shared lock), and while it waits it holds up new file_of callers:
a new wait on the guest threads that the change had said it did not add.

file_release now takes the lock only when it finds closing set after its decrement. The decrement and the load of
closing, and close_file's store of closing and load of users, are sequentially consistent pairs, so either the last
call out sees the mark or close_file sees users at 0 and closes the file itself; the check under the lock still keeps
the close to one. closing is an int32_t read and stored with atomics; a slot is emptied by the new file_clear, which
stores users and closing as atomics because the call that has just left the slot may read closing without the lock,
and open_file fills only h, mode and save.

Not reproduced in the game before the change. tests/fs_file_use_check.py gains a case: 1000 reads with no close take
the exclusive lock 0 times, and a read ends while another thread holds the lock shared. It failed on the code before
this change (1000 exclusive takes, the read waited) and passes after it, as do its close-during-a-read case and its
20000 closes and opens beside a reader (six runs, no foreign bytes, no double close). The compile check of fs_host.c
passes with no -Wall warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 18:23:21 -05:00
parent a135c19450
commit b8b57cfd53
2 changed files with 55 additions and 18 deletions

View File

@@ -1,6 +1,8 @@
"""A save file's handle and a CloseFile on another thread (runtime/fs_host.c file_of, file_release, close_file): a
call under way keeps its host file open, the close happens once when the last call is out, the guest's handle value
is not handed out again before that, and a call after the close gets FS_R_INVALID_ARGUMENT. The host files here are
is not handed out again before that, and a call after the close gets FS_R_INVALID_ARGUMENT. A call with no close
pending takes no exclusive lock, so it ends while another thread holds the lock shared (a Commit's fsync loop). The
host files here are
stand-ins that hand a closed file's value to the next one opened, as Windows does with a HANDLE: a read that went on
through a closed handle would read the other file. Builds fs_host.c's file functions into a test program. No game
files.
@@ -46,7 +48,8 @@ HARNESS = r'''
#include <string.h>
typedef SRWLOCK PlatMutex;
#define PLAT_MUTEX_INIT SRWLOCK_INIT
static void plat_mutex_lock(PlatMutex *m) { AcquireSRWLockExclusive(m); }
static volatile LONG exclusive_takes; // exclusive acquisitions of fs_host.c's lock
static void plat_mutex_lock(PlatMutex *m) { InterlockedIncrement(&exclusive_takes); AcquireSRWLockExclusive(m); }
static void plat_mutex_unlock(PlatMutex *m) { ReleaseSRWLockExclusive(m); }
static void plat_mutex_lock_shared(PlatMutex *m) { AcquireSRWLockShared(m); }
static void plat_mutex_unlock_shared(PlatMutex *m) { ReleaseSRWLockShared(m); }
@@ -171,7 +174,25 @@ int main(void) {
if (hc != ha) printf("FAIL: the closed slot was not free again (%#llx)\n", (unsigned long long)hc), fails++;
close_file(hb), close_file(hc);
// 2. A reader loops over a file "a" while another thread closes it, opens "b" (which may take a closed handle's
// 2. Calls with no close pending take no exclusive lock: 1000 reads add none, and a read ends while another thread
// holds the lock shared (fs_host.c commit's fsync loop runs so).
open_file(&ha, "a", FS_OPEN_READ);
LONG before = exclusive_takes;
int bad_reads = 0;
for (int i = 0; i < 1000; i++) bad_reads += read_file(&n, ha, 0, b, 4) != 0 || b[0] != 'a';
LONG taken = exclusive_takes - before;
AcquireSRWLockShared(&lock);
h_read = ha, got = 0;
t = CreateThread(NULL, 0, reader, NULL, 0, NULL);
int ended = WaitForSingleObject(t, 2000) == WAIT_OBJECT_0;
ReleaseSRWLockShared(&lock);
WaitForSingleObject(t, INFINITE);
printf("reads with no close pending: 1000 reads took the exclusive lock %ld time(s)%s, a read beside a shared holder "
"%s\n", (long)taken, taken ? " (FAIL)" : "", ended ? "ended" : "WAITED FOR IT (FAIL)");
if (taken || bad_reads || !ended || got != 'a') fails++;
close_file(ha);
// 3. A reader loops over a file "a" while another thread closes it, opens "b" (which may take a closed handle's
// value) and opens "a" again: no read returns b's bytes.
open_file(&ha, "a", FS_OPEN_READ);
current = ha;
@@ -205,7 +226,7 @@ def main():
return 0
file_t = re.search(r'typedef struct \{ PlatFile \*h;[^\n]*\} File;', source).group(0)
parts = [HARNESS, file_t, 'static File files[NFILES];', 'static PlatMutex lock = PLAT_MUTEX_INIT;']
for sig in ('static File *file_of(', 'static void file_closed(', 'static void file_release(',
for sig in ('static File *file_of(', 'static void file_closed(', 'static void file_clear(', 'static void file_release(',
'static int64_t host_read_at(', 'static uint32_t open_file(', 'static uint32_t read_file(',
'static void close_file('):
parts.append(function(sig))