this repo has no description
13
fork

Configure Feed

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

queue: add drain, use drain in vxfw

+54 -30
+21 -3
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 ··· 97 95 std.debug.assert(!self.isEmptyLH()); 98 96 } 99 97 98 + pub fn lock(self: *Self) void { 99 + self.mutex.lock(); 100 + } 101 + 102 + pub fn unlock(self: *Self) void { 103 + self.mutex.unlock(); 104 + } 105 + 106 + /// Used to efficiently drain the queue while the lock is externally held 107 + pub fn drain(self: *Self) ?T { 108 + if (self.isEmptyLH()) return null; 109 + return self.popLH(); 110 + } 111 + 100 112 fn isEmptyLH(self: Self) bool { 101 113 return self.write_index == self.read_index; 102 114 } ··· 136 148 /// Returns `index` modulo twice the length of the backing slice. 137 149 fn mask2(self: Self, index: usize) usize { 138 150 return index % (2 * self.buf.len); 151 + } 152 + 153 + fn popLH(self: *Self) T { 154 + const result = self.buf[self.mask(self.read_index)]; 155 + self.read_index = self.mask2(self.read_index + 1); 156 + return result; 139 157 } 140 158 }; 141 159 }
+33 -27
src/vxfw/App.zig
··· 119 119 120 120 try self.checkTimers(&ctx); 121 121 122 - while (loop.tryEvent()) |event| { 123 - defer { 124 - // Reset our context 125 - ctx.consume_event = false; 126 - ctx.phase = .capturing; 127 - } 128 - switch (event) { 129 - .key_press => { 130 - try focus_handler.handleEvent(&ctx, event); 131 - try self.handleCommand(&ctx.cmds); 132 - }, 133 - .focus_out => { 134 - try mouse_handler.mouseExit(self, &ctx); 135 - try focus_handler.handleEvent(&ctx, .focus_out); 136 - }, 137 - .focus_in => { 138 - try focus_handler.handleEvent(&ctx, .focus_in); 139 - }, 140 - .mouse => |mouse| try mouse_handler.handleMouse(self, &ctx, mouse), 141 - .winsize => |ws| { 142 - try vx.resize(self.allocator, tty.anyWriter(), ws); 143 - ctx.redraw = true; 144 - }, 145 - else => { 146 - try focus_handler.handleEvent(&ctx, event); 147 - try self.handleCommand(&ctx.cmds); 148 - }, 122 + { 123 + loop.queue.lock(); 124 + defer loop.queue.unlock(); 125 + while (loop.queue.drain()) |event| { 126 + defer { 127 + // Reset our context 128 + ctx.consume_event = false; 129 + ctx.phase = .capturing; 130 + } 131 + switch (event) { 132 + .key_press => { 133 + try focus_handler.handleEvent(&ctx, event); 134 + try self.handleCommand(&ctx.cmds); 135 + }, 136 + .focus_out => { 137 + try mouse_handler.mouseExit(self, &ctx); 138 + try focus_handler.handleEvent(&ctx, .focus_out); 139 + try self.handleCommand(&ctx.cmds); 140 + }, 141 + .focus_in => { 142 + try focus_handler.handleEvent(&ctx, .focus_in); 143 + try self.handleCommand(&ctx.cmds); 144 + }, 145 + .mouse => |mouse| try mouse_handler.handleMouse(self, &ctx, mouse), 146 + .winsize => |ws| { 147 + try vx.resize(self.allocator, tty.anyWriter(), ws); 148 + ctx.redraw = true; 149 + }, 150 + else => { 151 + try focus_handler.handleEvent(&ctx, event); 152 + try self.handleCommand(&ctx.cmds); 153 + }, 154 + } 149 155 } 150 156 } 151 157