Browse Source

Add mutex to fix crashing

master
Kenneth Bruen 9 months ago
parent
commit
ff2128ac06
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 16
      src/departure.zig
  2. 1
      src/state.zig

16
src/departure.zig

@ -75,6 +75,8 @@ fn fetchThread(state: *AppState) !void {
std.debug.print("[departure/fetchThread] JSON parse error: {}\n", .{err}); std.debug.print("[departure/fetchThread] JSON parse error: {}\n", .{err});
continue; continue;
}; };
state.departure_screen_state.mutex.lock();
defer state.departure_screen_state.mutex.unlock();
if (state.departure_screen_state.fetch_result) |old_result| { if (state.departure_screen_state.fetch_result) |old_result| {
old_result.deinit(); old_result.deinit();
} }
@ -532,7 +534,9 @@ pub fn render(state: *AppState) !void {
rl.KEY_MINUS, rl.KEY_KP_SUBTRACT => { rl.KEY_MINUS, rl.KEY_KP_SUBTRACT => {
ds.max_next_trains = @max(1, ds.max_next_trains - 1); ds.max_next_trains = @max(1, ds.max_next_trains - 1);
}, },
rl.KEY_EQUAL, rl.KEY_KP_EQUAL => { rl.KEY_EQUAL, rl.KEY_KP_ADD => {
ds.mutex.lock();
defer ds.mutex.unlock();
ds.max_next_trains = @min(ds.max_next_trains + 1, if (ds.fetch_result) |fr| @as(c_int, @intCast(fr.value.object.get("departures").?.array.items.len)) else 5); ds.max_next_trains = @min(ds.max_next_trains + 1, if (ds.fetch_result) |fr| @as(c_int, @intCast(fr.value.object.get("departures").?.array.items.len)) else 5);
}, },
rl.KEY_T => { rl.KEY_T => {
@ -551,9 +555,13 @@ pub fn render(state: *AppState) !void {
rl.BeginDrawing(); rl.BeginDrawing();
defer rl.EndDrawing(); defer rl.EndDrawing();
switch (ds.render_style) { {
.db1 => try draw_db1(state), ds.mutex.lock();
.ns => try draw_ns(state), defer ds.mutex.unlock();
switch (ds.render_style) {
.db1 => try draw_db1(state),
.ns => try draw_ns(state),
}
} }
state.close_app = rl.WindowShouldClose(); state.close_app = rl.WindowShouldClose();

1
src/state.zig

@ -25,6 +25,7 @@ pub const RenderStyle = enum(u8) {
}; };
pub const DepartureScreenState = struct { pub const DepartureScreenState = struct {
mutex: std.Thread.Mutex = .{},
station_id: std.ArrayListUnmanaged(u8), station_id: std.ArrayListUnmanaged(u8),
platform: std.ArrayListUnmanaged(u8), platform: std.ArrayListUnmanaged(u8),
departure_date: std.time.Instant, departure_date: std.time.Instant,

Loading…
Cancel
Save