chore: be more intentional in atomic ordering use

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-11-30 00:21:02 -04:00
parent f9aefedf60
commit 20f611b7b5
4 changed files with 10 additions and 8 deletions

View File

@ -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);

View File

@ -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];
}
};

View File

@ -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 {

View File

@ -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();
}