| ... | @@ -183,11 +183,8 @@ pub fn poll( | ... | @@ -183,11 +183,8 @@ pub fn poll( |
| 183 | .count = 0, | 183 | .count = 0, |
| 184 | }; | 184 | }; |
| 185 | result.poll_fds[i] = .{ | 185 | result.poll_fds[i] = .{ |
| 186 | .fd = @field(files, enum_fields[i].name).file.handle, | 186 | .fd = @field(files, enum_fields[i].name).handle, |
| 187 | .events = switch (@field(files, enum_fields[i].name).direction) { | 187 | .events = os.POLL.IN, |
| 188 | .in => os.POLL.IN, | | |
| 189 | .out => os.POLL.OUT, | | |
| 190 | }, | | |
| 191 | .revents = undefined, | 188 | .revents = undefined, |
| 192 | }; | 189 | }; |
| 193 | } | 190 | } |
| ... | @@ -200,12 +197,15 @@ pub fn Poller(comptime StreamEnum: type) type { | ... | @@ -200,12 +197,15 @@ pub fn Poller(comptime StreamEnum: type) type { |
| 200 | const Fifo = std.fifo.LinearFifo(u8, .Dynamic); | 197 | const Fifo = std.fifo.LinearFifo(u8, .Dynamic); |
| 201 | | 198 | |
| 202 | fifos: [enum_fields.len]Fifo, | 199 | fifos: [enum_fields.len]Fifo, |
| 203 | //directions: [enum_fields.len]PollFile.Direction, | | |
| 204 | //handles: [enum_fields.len]std.fs.File.Handle, | | |
| 205 | poll_fds: [enum_fields.len]std.os.pollfd, | 200 | poll_fds: [enum_fields.len]std.os.pollfd, |
| 206 | | 201 | |
| 207 | const Self = @This(); | 202 | const Self = @This(); |
| 208 | | 203 | |
| | 204 | pub fn deinit(self: *Self) void { |
| | 205 | inline for (&self.fifos) |*q| q.deinit(); |
| | 206 | self.* = undefined; |
| | 207 | } |
| | 208 | |
| 209 | pub fn poll(self: *Self) !void { | 209 | pub fn poll(self: *Self) !void { |
| 210 | if (builtin.os.tag == .windows) { | 210 | if (builtin.os.tag == .windows) { |
| 211 | return pollWindows(self); | 211 | return pollWindows(self); |
| ... | @@ -241,31 +241,22 @@ pub fn Poller(comptime StreamEnum: type) type { | ... | @@ -241,31 +241,22 @@ pub fn Poller(comptime StreamEnum: type) type { |
| 241 | const events_len = try os.poll(&self.poll_fds, std.math.maxInt(i32)); | 241 | const events_len = try os.poll(&self.poll_fds, std.math.maxInt(i32)); |
| 242 | if (events_len == 0) return; | 242 | if (events_len == 0) return; |
| 243 | | 243 | |
| 244 | inline for (0..enum_fields.len) |i| { | 244 | inline for (&self.poll_fds, &self.fifos) |*poll_fd, *q| { |
| 245 | // Try reading whatever is available before checking the error | 245 | // Try reading whatever is available before checking the error |
| 246 | // conditions. | 246 | // conditions. |
| 247 | // It's still possible to read after a POLL.HUP is received, | 247 | // It's still possible to read after a POLL.HUP is received, |
| 248 | // always check if there's some data waiting to be read first. | 248 | // always check if there's some data waiting to be read first. |
| 249 | if (self.poll_fds[i].revents & os.POLL.IN != 0) { | 249 | if (poll_fd.revents & os.POLL.IN != 0) { |
| 250 | const q = &self.fifos[i]; | | |
| 251 | const buf = try q.writableWithSize(bump_amt); | 250 | const buf = try q.writableWithSize(bump_amt); |
| 252 | const amt = try os.read(self.poll_fds[i].fd, buf); | 251 | const amt = try os.read(poll_fd.fd, buf); |
| 253 | q.update(amt); | 252 | q.update(amt); |
| 254 | std.debug.print("read {d} bytes\n", .{amt}); | | |
| 255 | if (amt == 0) { | 253 | if (amt == 0) { |
| 256 | // Remove the fd when the EOF condition is met. | 254 | // Remove the fd when the EOF condition is met. |
| 257 | self.poll_fds[i].fd = -1; | 255 | poll_fd.fd = -1; |
| 258 | } | 256 | } |
| 259 | } else if (self.poll_fds[i].revents & err_mask != 0) { | 257 | } else if (poll_fd.revents & err_mask != 0) { |
| 260 | // Exclude the fds that signaled an error. | 258 | // Exclude the fds that signaled an error. |
| 261 | self.poll_fds[i].fd = -1; | 259 | poll_fd.fd = -1; |
| 262 | } else if (self.poll_fds[i].revents & os.POLL.OUT != 0) { | | |
| 263 | const q = &self.fifos[i]; | | |
| 264 | const amt = try os.write(self.poll_fds[i].fd, q.readableSlice(0)); | | |
| 265 | q.discard(amt); | | |
| 266 | if (amt == 0) { | | |
| 267 | self.poll_fds[i].fd = -1; | | |
| 268 | } | | |
| 269 | } | 260 | } |
| 270 | } | 261 | } |
| 271 | } | 262 | } |
| ... | @@ -280,10 +271,10 @@ pub fn PollFiles(comptime StreamEnum: type) type { | ... | @@ -280,10 +271,10 @@ pub fn PollFiles(comptime StreamEnum: type) type { |
| 280 | for (&struct_fields, enum_fields) |*struct_field, enum_field| { | 271 | for (&struct_fields, enum_fields) |*struct_field, enum_field| { |
| 281 | struct_field.* = .{ | 272 | struct_field.* = .{ |
| 282 | .name = enum_field.name, | 273 | .name = enum_field.name, |
| 283 | .type = PollFile, | 274 | .type = fs.File, |
| 284 | .default_value = null, | 275 | .default_value = null, |
| 285 | .is_comptime = false, | 276 | .is_comptime = false, |
| 286 | .alignment = @alignOf(PollFile), | 277 | .alignment = @alignOf(fs.File), |
| 287 | }; | 278 | }; |
| 288 | } | 279 | } |
| 289 | return @Type(.{ .Struct = .{ | 280 | return @Type(.{ .Struct = .{ |
| ... | @@ -294,13 +285,6 @@ pub fn PollFiles(comptime StreamEnum: type) type { | ... | @@ -294,13 +285,6 @@ pub fn PollFiles(comptime StreamEnum: type) type { |
| 294 | } }); | 285 | } }); |
| 295 | } | 286 | } |
| 296 | | 287 | |
| 297 | pub const PollFile = struct { | | |
| 298 | file: File, | | |
| 299 | direction: Direction, | | |
| 300 | | | |
| 301 | pub const Direction = enum { in, out }; | | |
| 302 | }; | | |
| 303 | | | |
| 304 | test { | 288 | test { |
| 305 | _ = @import("io/bit_reader.zig"); | 289 | _ = @import("io/bit_reader.zig"); |
| 306 | _ = @import("io/bit_writer.zig"); | 290 | _ = @import("io/bit_writer.zig"); |