Browse Source

Improve thread safety

master
Kenneth Bruen 8 months ago
parent
commit
9585ce7f9e
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 17
      src/departure.zig
  2. 5
      src/home.zig

17
src/departure.zig

@ -27,7 +27,14 @@ fn fetchThread(state: *AppState) !void {
var curl = Curl.init() orelse return;
defer curl.deinit();
while (state.departure_screen_state.fetch_thread != null) {
while (true) {
{
state.departure_screen_state.mutex.lock();
defer state.departure_screen_state.mutex.unlock();
if (state.departure_screen_state.fetch_thread == null) {
break;
}
}
var fetch_anyway = state.departure_screen_state.should_refresh;
if (state.departure_screen_state.last_refresh_time + 30000 < std.time.milliTimestamp()) {
fetch_anyway = true;
@ -635,6 +642,9 @@ pub fn render(state: *AppState) !void {
switch (key) {
rl.KEY_LEFT => {
state.screen = .home;
ds.mutex.lock();
defer ds.mutex.unlock();
ds.fetch_thread = null;
},
rl.KEY_R => {
ds.should_refresh = true;
@ -666,7 +676,10 @@ pub fn render(state: *AppState) !void {
{
ds.mutex.lock();
defer ds.mutex.unlock();
defer {
ds.mutex.unlock();
_ = std.Thread.yield() catch null;
}
switch (ds.render_style) {
.db1 => try draw_db1(state),
.ns => try draw_ns(state),

5
src/home.zig

@ -102,7 +102,10 @@ pub fn render(state: *AppState) !void {
}
state.home_screen_state.mutex.lock();
defer state.home_screen_state.mutex.unlock();
defer {
state.home_screen_state.mutex.unlock();
_ = std.Thread.yield() catch null;
}
while (raylib.GetKeyPressed()) |key| {
switch (key) {

Loading…
Cancel
Save