Start the game again after Restart on every processor it had (Linux).

On Linux each Restart from the menu cost the next game one more processor core on machines with 12 or more logical processors. A process started with posix_spawn begins with the mask of the thread that spawned it, and since the recording thread has a core of its own no thread has every processor: the restarted game began without that core and took the next fast one for its own recording thread. plat_restart (runtime/platform/linux.c) now widens the calling thread's mask to all of the process's processors for the time of the spawn and puts it back. A mask set from outside is kept, helper programs started with plat_spawn are unchanged, and Windows is not touched.

From pull request #435, with its test tests/platform_restart_mask_test.c, which needs Linux. Not compiled or run here: this machine has no Linux compiler.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michael Schienbein
2026-11-10 12:49:10 -05:00
committed by chrg6
parent 919aa7b2a8
commit 541c4ff84c
2 changed files with 101 additions and 0 deletions

View File

@@ -2373,6 +2373,22 @@ static void spawn_close_from(posix_spawn_file_actions_t *fa, int from) {
if (posix_spawn_file_actions_addclosefrom_np) posix_spawn_file_actions_addclosefrom_np(fa, from);
}
// The calling thread's mask with every processor this process uses, for the time of a posix_spawn: a child starts
// with the mask of the thread that spawned it, and after plat_cpu_reserve no thread has them all (the recording thread
// has its core alone, every other thread lacks that core). The game plat_restart starts would begin without the core,
// take the next fast one for its own recording thread, and so have one core fewer after every Restart.
// 1 when the mask was widened: *saved is then the mask to put back.
static int spawn_mask_whole(cpu_set_t *saved) {
cpu_set_t whole;
plat_mutex_lock(&cpu_lock);
int taken = cpu_taker != 0;
CPU_OR(&whole, &cpu_taken, &cpu_others);
plat_mutex_unlock(&cpu_lock);
if (!taken || sched_getaffinity(0, sizeof *saved, saved)) return 0;
CPU_OR(&whole, &whole, saved);
return !CPU_EQUAL(&whole, saved) && !sched_setaffinity(0, sizeof whole, &whole);
}
int plat_restart(void) {
// the arguments, NUL-separated, from /proc/self/cmdline
static char args[16384];
@@ -2399,8 +2415,11 @@ int plat_restart(void) {
posix_spawnattr_setsigdefault(&at, &all);
posix_spawnattr_setflags(&at, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF);
setenv(RESTART_WAIT, wait, 1);
cpu_set_t mask;
int whole = spawn_mask_whole(&mask);
pid_t pid;
int e = posix_spawn(&pid, exe, &fa, &at, argv, environ);
if (whole) sched_setaffinity(0, sizeof mask, &mask);
unsetenv(RESTART_WAIT);
posix_spawnattr_destroy(&at);
posix_spawn_file_actions_destroy(&fa);