From 2c4bfec91c3e5eb327f27ffee7621d2e1b99eb09 Mon Sep 17 00:00:00 2001 From: namedkitten Date: Thu, 9 Jul 2020 12:01:38 +0100 Subject: [PATCH] Added mutexes. --- src/bar/bar.zig | 4 ++-- src/types/bar.zig | 6 +++--- src/widgets/weather/weather.zig | 13 +++++++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/bar/bar.zig b/src/bar/bar.zig index 59eb57f..887b3b0 100644 --- a/src/bar/bar.zig +++ b/src/bar/bar.zig @@ -34,8 +34,8 @@ pub const Bar = struct { try out_file.writer().writeAll("],\n"); } } - pub fn add(self: Bar, i: *Info) void { - std.debug.warn("Add {}!\n", .{i.name}); + pub fn keep_running(self: Bar) bool { + return self.running; } }; diff --git a/src/types/bar.zig b/src/types/bar.zig index 1a67698..bd8c701 100644 --- a/src/types/bar.zig +++ b/src/types/bar.zig @@ -5,15 +5,15 @@ const Info = @import("info.zig").Info; pub const Bar = struct { const IFace = Interface(struct { - add: fn (*SelfType, *Info) anyerror!void, start: fn (*SelfType) anyerror!void, + keep_running: fn (*SelfType) bool, }, interface.Storage.NonOwning); iface: IFace, pub fn init(impl_ptr: var) Bar { return .{ .iface = try IFace.init(.{impl_ptr}) }; } - pub fn add(self: *Bar, i: *Info) anyerror!void { - return try self.iface.call("add", .{i}); + pub fn keep_running(self: *Bar) bool { + return self.iface.call("keep_running", .{}); } pub fn start(self: *Bar) anyerror!void { return try self.iface.call("start", .{}); diff --git a/src/widgets/weather/weather.zig b/src/widgets/weather/weather.zig index 1902a8f..e03e80c 100644 --- a/src/widgets/weather/weather.zig +++ b/src/widgets/weather/weather.zig @@ -12,6 +12,7 @@ pub const WeatherWidget = struct { bar: *Bar, weather_api_url: []const u8, info: ?Info, + mutex: std.Mutex, pub fn name(self: *WeatherWidget) []const u8 { return "weather"; @@ -25,6 +26,8 @@ pub const WeatherWidget = struct { }; } pub fn info(self: *WeatherWidget) Info { + const lock = self.mutex.acquire(); + defer lock.release(); if (self.info == null) { return self.initial_info(); } else { @@ -33,7 +36,8 @@ pub const WeatherWidget = struct { } pub fn start(self: *WeatherWidget) anyerror!void { - while (true) { + defer self.mutex.deinit(); + while (self.bar.keep_running()) { std.time.sleep(2000 * std.time.ns_per_ms); std.debug.print("Starting Weather Widget.\n", .{}); @@ -108,7 +112,7 @@ pub const WeatherWidget = struct { self.allocator.free(self.info.?.full_text); } - self.info = Info{ + var i = Info{ .name = "weather", .full_text = try std.fmt.allocPrint(self.allocator, "{} {}{}{} {}", .{ colour(arenacator, "accentlight", "weather"), @@ -120,6 +124,10 @@ pub const WeatherWidget = struct { .markup = "pango", .color = "#ffffff", }; + const lock = self.mutex.acquire(); + self.info = i; + lock.release(); + arena.deinit(); } } @@ -131,5 +139,6 @@ pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: [ .bar = bar, .weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric", .info = null, + .mutex = std.Mutex.init(), }; }