fix: ensure gdb thread respects + communicates w/ should_quit atomic bool

This commit is contained in:
Rekai Nyangadzayi Musuka 2024-03-05 20:56:57 -06:00
parent 8d2c76e410
commit 7ae72ed5a8
1 changed files with 9 additions and 10 deletions

View File

@ -28,7 +28,7 @@ const Xml = struct { target: []const u8, memory_map: []const u8 };
pub fn init(emulator: Emulator, xml: Xml) !Self { pub fn init(emulator: Emulator, xml: Xml) !Self {
var server = std.net.StreamServer.init(.{}); var server = std.net.StreamServer.init(.{});
try server.listen(std.net.Address.initIp4([_]u8{0} ** 4, port)); try server.listen(std.net.Address.initIp4(.{ 127, 0, 0, 1 }, port));
return .{ return .{
.emu = emulator, .emu = emulator,
@ -52,27 +52,26 @@ const Action = union(enum) {
nack, nack,
}; };
pub fn run(self: *Self, allocator: Allocator, quit: *std.atomic.Value(bool)) !void { pub fn run(self: *Self, allocator: Allocator, should_quit: *std.atomic.Value(bool)) !void {
var buf: [Packet.max_len]u8 = undefined; var buf: [Packet.max_len]u8 = undefined;
var client = try self.socket.accept(); var client = try self.socket.accept();
log.info("client connected from {}", .{client.address}); log.info("client connected from {}", .{client.address});
while (true) { while (!should_quit.load(.Monotonic)) {
if (self.state.should_quit) break; if (self.state.should_quit) {
// Just in case its the gdbstub that exited first,
// attempt to signal to the GUI that it should also exit
should_quit.store(true, .Monotonic);
break;
}
const len = try client.stream.read(&buf); const len = try client.stream.read(&buf);
if (len == 0) break; if (len == 0) break;
if (quit.load(.Monotonic)) break;
const action = try self.parse(allocator, buf[0..len]); const action = try self.parse(allocator, buf[0..len]);
try self.send(allocator, client, action); try self.send(allocator, client, action);
} }
// Just in case its the gdbstub that exited first,
// attempt to signal to the GUI that it should also exit
quit.store(true, .Monotonic);
} }
fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action { fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {