From 620205c1740904c956dd5d1c0d74c0d0f8ddb392 Mon Sep 17 00:00:00 2001 From: aldats Date: Sat, 29 Apr 2023 18:14:16 -0500 Subject: [PATCH] refactor(client): implement `Monitor` in `ChatLog` --- client/ChatLog.zig | 19 ++++++++++--------- client/main.zig | 12 ++++++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/client/ChatLog.zig b/client/ChatLog.zig index ec482a8..e66ccc0 100644 --- a/client/ChatLog.zig +++ b/client/ChatLog.zig @@ -3,7 +3,8 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const CharArray = std.ArrayList(u8); -const Mutex = std.Thread.Mutex; + +const Monitor = @import("chat").Monitor; const Self = @This(); @@ -11,17 +12,17 @@ pub const Writer = std.io.Writer(*Self, error{OutOfMemory}, appendWrite); allocator: Allocator, array: CharArray, -mutex: Mutex, +monitor: Monitor, /// Deinitialize with `deinit`. pub fn init(allocator: Allocator) Self { - return .{ .allocator = allocator, .array = CharArray.init(allocator), .mutex = Mutex{} }; + return .{ .allocator = allocator, .array = CharArray.init(allocator), .monitor = Monitor{} }; } /// Release all allocated memory. pub fn deinit(self: *Self) void { - self.mutex.lock(); - defer self.mutex.unlock(); + self.monitor.initWrite(); + defer self.monitor.deinitWrite(); self.array.deinit(); } @@ -29,8 +30,8 @@ pub fn deinit(self: *Self) void { /// Extend the list by 1 element. Allocates more memory as necessary. /// Invalidates pointers if additional memory is needed. Is thread-safe. pub fn append(self: *Self, item: u8) !void { - self.mutex.lock(); - defer self.mutex.unlock(); + self.monitor.initWrite(); + defer self.monitor.deinitWrite(); try self.array.append(item); } @@ -38,8 +39,8 @@ pub fn append(self: *Self, item: u8) !void { /// Append the slice of items to the list. Allocates more memory as necessary. /// Invalidates pointers if additional memory is needed. Is thread-safe. pub fn appendSlice(self: *Self, items: []const u8) !void { - self.mutex.lock(); - defer self.mutex.unlock(); + self.monitor.initWrite(); + defer self.monitor.deinitWrite(); try self.array.appendSlice(items); } diff --git a/client/main.zig b/client/main.zig index 81a0441..b99d5f0 100644 --- a/client/main.zig +++ b/client/main.zig @@ -81,9 +81,9 @@ pub fn main() !void { var byte_buf: [32]u8 = undefined; // Perform preliminary render. - chatlog.mutex.lock(); + chatlog.monitor.initRead(); try render(chatlog.array.items, buffer.items); - chatlog.mutex.unlock(); + chatlog.monitor.deinitRead(); // Inform the user we are attempting to connect to the server and do so. try chatlog_writer.print("Connecting to {s} on port {d}...\n", .{ args.server, args.port }); @@ -110,9 +110,9 @@ pub fn main() !void { const events_no = try std.os.poll(&fds, POLL_TIMEOUT_MS); const amt_read = try term.readInput(byte_buf[0..]); if (events_no < 1) { - chatlog.mutex.lock(); + chatlog.monitor.initRead(); try render(chatlog.array.items, buffer.items); - chatlog.mutex.unlock(); + chatlog.monitor.deinitRead(); continue; } @@ -172,9 +172,9 @@ pub fn main() !void { } // Render the terminal. - chatlog.mutex.lock(); + chatlog.monitor.initRead(); try render(chatlog.array.items, buffer.items); - chatlog.mutex.unlock(); + chatlog.monitor.deinitRead(); } receiver_thread.io_thread.join(); -- 2.45.2