| ... | @@ -502,18 +502,43 @@ pub const Loop = struct { | ... | @@ -502,18 +502,43 @@ pub const Loop = struct { |
| 502 | } | 502 | } |
| 503 | | 503 | |
| 504 | pub fn waitUntilFdReadable(self: *Loop, fd: os.fd_t) void { | 504 | pub fn waitUntilFdReadable(self: *Loop, fd: os.fd_t) void { |
| 505 | return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN); | 505 | switch (builtin.os.tag) { |
| | 506 | .linux => { |
| | 507 | self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN); |
| | 508 | }, |
| | 509 | .macosx, .freebsd, .netbsd, .dragonfly => { |
| | 510 | self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT); |
| | 511 | }, |
| | 512 | else => @compileError("Unsupported OS"), |
| | 513 | } |
| 506 | } | 514 | } |
| 507 | | 515 | |
| 508 | pub fn waitUntilFdWritable(self: *Loop, fd: os.fd_t) void { | 516 | pub fn waitUntilFdWritable(self: *Loop, fd: os.fd_t) void { |
| 509 | return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT); | 517 | switch (builtin.os.tag) { |
| | 518 | .linux => { |
| | 519 | self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT); |
| | 520 | }, |
| | 521 | .macosx, .freebsd, .netbsd, .dragonfly => { |
| | 522 | self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT); |
| | 523 | }, |
| | 524 | else => @compileError("Unsupported OS"), |
| | 525 | } |
| 510 | } | 526 | } |
| 511 | | 527 | |
| 512 | pub fn waitUntilFdWritableOrReadable(self: *Loop, fd: os.fd_t) void { | 528 | pub fn waitUntilFdWritableOrReadable(self: *Loop, fd: os.fd_t) void { |
| 513 | return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN); | 529 | switch (builtin.os.tag) { |
| | 530 | .linux => { |
| | 531 | self.linuxWaitFd(@intCast(usize, fd), os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN); |
| | 532 | }, |
| | 533 | .macosx, .freebsd, .netbsd, .dragonfly => { |
| | 534 | self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT); |
| | 535 | self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT); |
| | 536 | }, |
| | 537 | else => @compileError("Unsupported OS"), |
| | 538 | } |
| 514 | } | 539 | } |
| 515 | | 540 | |
| 516 | pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !os.Kevent { | 541 | pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) void { |
| 517 | var resume_node = ResumeNode.Basic{ | 542 | var resume_node = ResumeNode.Basic{ |
| 518 | .base = ResumeNode{ | 543 | .base = ResumeNode{ |
| 519 | .id = ResumeNode.Id.Basic, | 544 | .id = ResumeNode.Id.Basic, |
| ... | @@ -524,40 +549,37 @@ pub const Loop = struct { | ... | @@ -524,40 +549,37 @@ pub const Loop = struct { |
| 524 | }; | 549 | }; |
| 525 | defer self.bsdRemoveKev(ident, filter); | 550 | defer self.bsdRemoveKev(ident, filter); |
| 526 | suspend { | 551 | suspend { |
| 527 | try self.bsdAddKev(&resume_node, ident, filter, fflags); | 552 | self.bsdAddKev(&resume_node, ident, filter, fflags) catch unreachable; |
| 528 | } | 553 | } |
| 529 | return resume_node.kev; | | |
| 530 | } | 554 | } |
| 531 | | 555 | |
| 532 | /// resume_node must live longer than the anyframe that it holds a reference to. | 556 | /// resume_node must live longer than the anyframe that it holds a reference to. |
| 533 | pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void { | 557 | pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void { |
| 534 | self.beginOneEvent(); | 558 | self.beginOneEvent(); |
| 535 | errdefer self.finishOneEvent(); | 559 | errdefer self.finishOneEvent(); |
| 536 | var kev = os.Kevent{ | 560 | var kev = [1]os.Kevent{os.Kevent{ |
| 537 | .ident = ident, | 561 | .ident = ident, |
| 538 | .filter = filter, | 562 | .filter = filter, |
| 539 | .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR, | 563 | .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR, |
| 540 | .fflags = fflags, | 564 | .fflags = fflags, |
| 541 | .data = 0, | 565 | .data = 0, |
| 542 | .udata = @ptrToInt(&resume_node.base), | 566 | .udata = @ptrToInt(&resume_node.base), |
| 543 | }; | 567 | }}; |
| 544 | const kevent_array = (*const [1]os.Kevent)(&kev); | 568 | const empty_kevs = &[0]os.Kevent{}; |
| 545 | const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; | 569 | _ = try os.kevent(self.os_data.kqfd, &kev, empty_kevs, null); |
| 546 | _ = try os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null); | | |
| 547 | } | 570 | } |
| 548 | | 571 | |
| 549 | pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void { | 572 | pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void { |
| 550 | var kev = os.Kevent{ | 573 | var kev = [1]os.Kevent{os.Kevent{ |
| 551 | .ident = ident, | 574 | .ident = ident, |
| 552 | .filter = filter, | 575 | .filter = filter, |
| 553 | .flags = os.EV_DELETE, | 576 | .flags = os.EV_DELETE, |
| 554 | .fflags = 0, | 577 | .fflags = 0, |
| 555 | .data = 0, | 578 | .data = 0, |
| 556 | .udata = 0, | 579 | .udata = 0, |
| 557 | }; | 580 | }}; |
| 558 | const kevent_array = (*const [1]os.Kevent)(&kev); | 581 | const empty_kevs = &[0]os.Kevent{}; |
| 559 | const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; | 582 | _ = os.kevent(self.os_data.kqfd, &kev, empty_kevs, null) catch undefined; |
| 560 | _ = os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined; | | |
| 561 | self.finishOneEvent(); | 583 | self.finishOneEvent(); |
| 562 | } | 584 | } |
| 563 | | 585 | |
| ... | @@ -712,7 +734,7 @@ pub const Loop = struct { | ... | @@ -712,7 +734,7 @@ pub const Loop = struct { |
| 712 | } | 734 | } |
| 713 | | 735 | |
| 714 | /// Performs an async `os.open` using a separate thread. | 736 | /// Performs an async `os.open` using a separate thread. |
| 715 | pub fn openZ(self: *Loop, file_path: [*:0]const u8, flags: u32, mode: usize) os.OpenError!os.fd_t { | 737 | pub fn openZ(self: *Loop, file_path: [*:0]const u8, flags: u32, mode: os.mode_t) os.OpenError!os.fd_t { |
| 716 | var req_node = Request.Node{ | 738 | var req_node = Request.Node{ |
| 717 | .data = .{ | 739 | .data = .{ |
| 718 | .msg = .{ | 740 | .msg = .{ |
| ... | @@ -733,7 +755,7 @@ pub const Loop = struct { | ... | @@ -733,7 +755,7 @@ pub const Loop = struct { |
| 733 | } | 755 | } |
| 734 | | 756 | |
| 735 | /// Performs an async `os.opent` using a separate thread. | 757 | /// Performs an async `os.opent` using a separate thread. |
| 736 | pub fn openatZ(self: *Loop, fd: os.fd_t, file_path: [*:0]const u8, flags: u32, mode: usize) os.OpenError!os.fd_t { | 758 | pub fn openatZ(self: *Loop, fd: os.fd_t, file_path: [*:0]const u8, flags: u32, mode: os.mode_t) os.OpenError!os.fd_t { |
| 737 | var req_node = Request.Node{ | 759 | var req_node = Request.Node{ |
| 738 | .data = .{ | 760 | .data = .{ |
| 739 | .msg = .{ | 761 | .msg = .{ |