diff --git a/src/core/emu.zig b/src/core/emu.zig index 0c8a03c..d8d2375 100644 --- a/src/core/emu.zig +++ b/src/core/emu.zig @@ -56,7 +56,7 @@ fn inner(comptime kind: RunKind, audio_sync: bool, quit: *Atomic(bool), schedule .Unlimited, .UnlimitedFPS => { log.info("Emulation w/out video sync", .{}); - while (!quit.load(.SeqCst)) { + while (!quit.load(.Monotonic)) { runFrame(scheduler, cpu); audioSync(audio_sync, cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full); @@ -68,7 +68,7 @@ fn inner(comptime kind: RunKind, audio_sync: bool, quit: *Atomic(bool), schedule var timer = Timer.start() catch @panic("failed to initalize std.timer.Timer"); var wake_time: u64 = frame_period; - while (!quit.load(.SeqCst)) { + while (!quit.load(.Monotonic)) { runFrame(scheduler, cpu); const new_wake_time = videoSync(&timer, wake_time); diff --git a/src/core/ppu.zig b/src/core/ppu.zig index 24a943f..8c05322 100644 --- a/src/core/ppu.zig +++ b/src/core/ppu.zig @@ -1464,7 +1464,7 @@ const FrameBuffer = struct { layers: [2][]u8, buf: []u8, - current: u1, + current: std.atomic.Atomic(u8), allocator: Allocator, @@ -1483,7 +1483,7 @@ const FrameBuffer = struct { // Front and Back Framebuffers .layers = [_][]u8{ buf[0..][0..framebuf_len], buf[framebuf_len..][0..framebuf_len] }, .buf = buf, - .current = 0, + .current = std.atomic.Atomic(u8).init(0), .allocator = allocator, }; @@ -1495,10 +1495,12 @@ const FrameBuffer = struct { } pub fn swap(self: *Self) void { - self.current = ~self.current; + _ = self.current.fetchXor(1, .Release); // fetchNot(.Release) } pub fn get(self: *Self, comptime dev: Device) []u8 { - return self.layers[if (dev == .Emulator) self.current else ~self.current]; + const current = @intCast(u1, self.current.load(.Acquire)); + + return self.layers[if (dev == .Emulator) current else ~current]; } }; diff --git a/src/platform.zig b/src/platform.zig index 426bd2c..fb598f9 100644 --- a/src/platform.zig +++ b/src/platform.zig @@ -246,7 +246,7 @@ pub const Gui = struct { SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr); } - quit.store(true, .SeqCst); // Terminate Emulator Thread + quit.store(true, .Monotonic); // Terminate Emulator Thread } pub fn deinit(self: *Self) void { diff --git a/src/util.zig b/src/util.zig index 0042d9d..ee53370 100644 --- a/src/util.zig +++ b/src/util.zig @@ -47,7 +47,7 @@ pub const FpsTracker = struct { pub fn value(self: *Self) u32 { if (self.timer.read() >= std.time.ns_per_s) { - self.fps = self.count.swap(0, .SeqCst); + self.fps = self.count.swap(0, .Monotonic); self.timer.reset(); }