this repo has no description
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

queue: add drain method, simplify popping

rockorager.dev 3af546c3 00a762b9

verified
+26 -10
+3 -1
src/Server.zig
··· 16 16 const ChannelPrivileges = irc.ChannelPrivileges; 17 17 const ChatHistory = irc.ChatHistory; 18 18 const ClientMessage = irc.ClientMessage; 19 + const Http = @import("http.zig"); 19 20 const Message = irc.Message; 20 21 const MessageIterator = irc.MessageIterator; 21 22 const Queue = @import("queue.zig").Queue; ··· 24 25 const Server = @import("Server.zig"); 25 26 const Timestamp = irc.Timestamp; 26 27 const User = irc.User; 27 - const Http = @import("http.zig"); 28 28 29 29 const assert = std.debug.assert; 30 30 ··· 59 59 realname: []const u8, 60 60 }; 61 61 }; 62 + 63 + pub const WorkerQueue = Queue(WakeupResult, 128); 62 64 63 65 pub const Options = struct { 64 66 hostname: []const u8 = "localhost",
+23 -9
src/queue.zig
··· 36 36 self.not_full.signal(); 37 37 } 38 38 39 - const result = self.buf[self.mask(self.read_index)]; 40 - self.read_index = self.mask2(self.read_index + 1); 41 - return result; 39 + return self.popLH(); 42 40 } 43 41 44 42 /// Push an item into the queue. Blocks until an item has been ··· 79 77 /// available. 80 78 pub fn tryPop(self: *Self) ?T { 81 79 self.mutex.lock(); 82 - if (self.isEmptyLH()) { 83 - self.mutex.unlock(); 84 - return null; 85 - } 86 - self.mutex.unlock(); 87 - return self.pop(); 80 + defer self.mutex.unlock(); 81 + if (self.isEmptyLH()) return null; 82 + return self.popLH(); 88 83 } 89 84 90 85 /// Poll the queue. This call blocks until events are in the queue ··· 97 92 std.debug.assert(!self.isEmptyLH()); 98 93 } 99 94 95 + pub fn lock(self: *Self) void { 96 + self.mutex.lock(); 97 + } 98 + 99 + pub fn unlock(self: *Self) void { 100 + self.mutex.unlock(); 101 + } 102 + 103 + /// Used to efficiently drain the queue while the lock is externally held 104 + pub fn drain(self: *Self) ?T { 105 + if (self.isEmptyLH()) return null; 106 + } 107 + 100 108 fn isEmptyLH(self: Self) bool { 101 109 return self.write_index == self.read_index; 102 110 } ··· 136 144 /// Returns `index` modulo twice the length of the backing slice. 137 145 fn mask2(self: Self, index: usize) usize { 138 146 return index % (2 * self.buf.len); 147 + } 148 + 149 + fn popLH(self: *Self) T { 150 + const result = self.buf[self.mask(self.read_index)]; 151 + self.read_index = self.mask2(self.read_index + 1); 152 + return result; 139 153 } 140 154 }; 141 155 }