1
0
Fork 0

Added mutexes.

This commit is contained in:
namedkitten 2020-07-09 12:01:38 +01:00
parent b4353adc0c
commit 2c4bfec91c
3 changed files with 16 additions and 7 deletions

View file

@ -34,8 +34,8 @@ pub const Bar = struct {
try out_file.writer().writeAll("],\n"); try out_file.writer().writeAll("],\n");
} }
} }
pub fn add(self: Bar, i: *Info) void { pub fn keep_running(self: Bar) bool {
std.debug.warn("Add {}!\n", .{i.name}); return self.running;
} }
}; };

View file

@ -5,15 +5,15 @@ const Info = @import("info.zig").Info;
pub const Bar = struct { pub const Bar = struct {
const IFace = Interface(struct { const IFace = Interface(struct {
add: fn (*SelfType, *Info) anyerror!void,
start: fn (*SelfType) anyerror!void, start: fn (*SelfType) anyerror!void,
keep_running: fn (*SelfType) bool,
}, interface.Storage.NonOwning); }, interface.Storage.NonOwning);
iface: IFace, iface: IFace,
pub fn init(impl_ptr: var) Bar { pub fn init(impl_ptr: var) Bar {
return .{ .iface = try IFace.init(.{impl_ptr}) }; return .{ .iface = try IFace.init(.{impl_ptr}) };
} }
pub fn add(self: *Bar, i: *Info) anyerror!void { pub fn keep_running(self: *Bar) bool {
return try self.iface.call("add", .{i}); return self.iface.call("keep_running", .{});
} }
pub fn start(self: *Bar) anyerror!void { pub fn start(self: *Bar) anyerror!void {
return try self.iface.call("start", .{}); return try self.iface.call("start", .{});

View file

@ -12,6 +12,7 @@ pub const WeatherWidget = struct {
bar: *Bar, bar: *Bar,
weather_api_url: []const u8, weather_api_url: []const u8,
info: ?Info, info: ?Info,
mutex: std.Mutex,
pub fn name(self: *WeatherWidget) []const u8 { pub fn name(self: *WeatherWidget) []const u8 {
return "weather"; return "weather";
@ -25,6 +26,8 @@ pub const WeatherWidget = struct {
}; };
} }
pub fn info(self: *WeatherWidget) Info { pub fn info(self: *WeatherWidget) Info {
const lock = self.mutex.acquire();
defer lock.release();
if (self.info == null) { if (self.info == null) {
return self.initial_info(); return self.initial_info();
} else { } else {
@ -33,7 +36,8 @@ pub const WeatherWidget = struct {
} }
pub fn start(self: *WeatherWidget) anyerror!void { 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.time.sleep(2000 * std.time.ns_per_ms);
std.debug.print("Starting Weather Widget.\n", .{}); std.debug.print("Starting Weather Widget.\n", .{});
@ -108,7 +112,7 @@ pub const WeatherWidget = struct {
self.allocator.free(self.info.?.full_text); self.allocator.free(self.info.?.full_text);
} }
self.info = Info{ var i = Info{
.name = "weather", .name = "weather",
.full_text = try std.fmt.allocPrint(self.allocator, "{} {}{}{} {}", .{ .full_text = try std.fmt.allocPrint(self.allocator, "{} {}{}{} {}", .{
colour(arenacator, "accentlight", "weather"), colour(arenacator, "accentlight", "weather"),
@ -120,6 +124,10 @@ pub const WeatherWidget = struct {
.markup = "pango", .markup = "pango",
.color = "#ffffff", .color = "#ffffff",
}; };
const lock = self.mutex.acquire();
self.info = i;
lock.release();
arena.deinit(); arena.deinit();
} }
} }
@ -131,5 +139,6 @@ pub inline fn New(allocator: *std.mem.Allocator, bar: *Bar, comptime location: [
.bar = bar, .bar = bar,
.weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric", .weather_api_url = "/data/2.5/weather?q=" ++ location ++ "&appid=dcea3595afe693d1c17846141f58ea10&units=metric",
.info = null, .info = null,
.mutex = std.Mutex.init(),
}; };
} }