Add mutex to fix crashing
This commit is contained in:
parent
1d8cd97a9e
commit
ff2128ac06
2 changed files with 13 additions and 4 deletions
|
@ -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();
|
||||||
|
|
|
@ -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…
Add table
Reference in a new issue