Add mutex to fix crashing

This commit is contained in:
Kenneth Bruen 2024-02-17 13:36:10 +01:00
parent 1d8cd97a9e
commit ff2128ac06
Signed by: kbruen
GPG key ID: C1980A470C3EE5B1
2 changed files with 13 additions and 4 deletions

View file

@ -75,6 +75,8 @@ fn fetchThread(state: *AppState) !void {
std.debug.print("[departure/fetchThread] JSON parse error: {}\n", .{err});
continue;
};
state.departure_screen_state.mutex.lock();
defer state.departure_screen_state.mutex.unlock();
if (state.departure_screen_state.fetch_result) |old_result| {
old_result.deinit();
}
@ -532,7 +534,9 @@ pub fn render(state: *AppState) !void {
rl.KEY_MINUS, rl.KEY_KP_SUBTRACT => {
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);
},
rl.KEY_T => {
@ -551,10 +555,14 @@ pub fn render(state: *AppState) !void {
rl.BeginDrawing();
defer rl.EndDrawing();
{
ds.mutex.lock();
defer ds.mutex.unlock();
switch (ds.render_style) {
.db1 => try draw_db1(state),
.ns => try draw_ns(state),
}
}
state.close_app = rl.WindowShouldClose();
}

View file

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