diff --git a/src/core/kernel/support/EmuFS.cpp b/src/core/kernel/support/EmuFS.cpp index c193edeaa..ad6e54b6e 100644 --- a/src/core/kernel/support/EmuFS.cpp +++ b/src/core/kernel/support/EmuFS.cpp @@ -125,15 +125,23 @@ uint32_t fs_lock = 0; __declspec(naked) void LockFS() { __asm { - pushfd - pushad - spinlock : - mov eax, 1 - xchg eax, fs_lock - test eax, eax - jnz spinlock - popad - popfd + // Backup Registers + pushfd + pushad + + // Spin until we can aquire the lock + spinlock : + call SwitchToThread // Give other threads chance to run, prevents hogging entire timeslice waiting for spinlock + // We do this here loop because SwitchToThread will overwrite eax, so it cannot go below + // It's not worth wasting the extra cycles of pushing/popping eax to the stack around this call + mov eax, 1 + xchg eax, fs_lock + test eax, eax + jnz spinlock + + // Restore registers and return + popad + popfd ret } }