authorgravatar for rumcode@icloud.comChris Heyes <rumcode@icloud.com> 2020-05-02 19:14:46+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-02 14:14:46-04:00
log8ebcca6734e07aea29098ca4c63c0216b3099d0e
tree37f85f4c35f3107da778958ee2cedbe51192b03c
parent33705d06bb7025813422a7104c24370f0a29dd75
signature Signed by PGP key 4AEE18F83AFDEB23

Get evented io code paths to build on macOS (#5233)

* Get evented io code paths to build on macOS * Use mode_t instead of usize where appropriate

3 files changed, 46 insertions(+), 24 deletions(-)

lib/std/event/loop.zig+40-18
...@@ -502,18 +502,43 @@ pub const Loop = struct {...@@ -502,18 +502,43 @@ pub const Loop = struct {
502 }502 }
503503
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 }
507515
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 }
511527
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 }
515540
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 }
531555
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 }
548571
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 }
563585
...@@ -712,7 +734,7 @@ pub const Loop = struct {...@@ -712,7 +734,7 @@ pub const Loop = struct {
712 }734 }
713735
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 }
734756
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 = .{
lib/std/os.zig+3-3
...@@ -374,7 +374,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {...@@ -374,7 +374,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
374 const first = iov[0];374 const first = iov[0];
375 return read(fd, first.iov_base[0..first.iov_len]);375 return read(fd, first.iov_base[0..first.iov_len]);
376 }376 }
377 377
378 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);378 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
379 while (true) {379 while (true) {
380 // TODO handle the case when iov_len is too large and get rid of this @intCast380 // TODO handle the case when iov_len is too large and get rid of this @intCast
...@@ -859,7 +859,7 @@ pub const OpenError = error{...@@ -859,7 +859,7 @@ pub const OpenError = error{
859/// Open and possibly create a file. Keeps trying if it gets interrupted.859/// Open and possibly create a file. Keeps trying if it gets interrupted.
860/// See also `openC`.860/// See also `openC`.
861/// TODO support windows861/// TODO support windows
862pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!fd_t {862pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t {
863 const file_path_c = try toPosixPath(file_path);863 const file_path_c = try toPosixPath(file_path);
864 return openZ(&file_path_c, flags, perm);864 return openZ(&file_path_c, flags, perm);
865}865}
...@@ -869,7 +869,7 @@ pub const openC = @compileError("deprecated: renamed to openZ");...@@ -869,7 +869,7 @@ pub const openC = @compileError("deprecated: renamed to openZ");
869/// Open and possibly create a file. Keeps trying if it gets interrupted.869/// Open and possibly create a file. Keeps trying if it gets interrupted.
870/// See also `open`.870/// See also `open`.
871/// TODO support windows871/// TODO support windows
872pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: usize) OpenError!fd_t {872pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: mode_t) OpenError!fd_t {
873 while (true) {873 while (true) {
874 const rc = system.open(file_path, flags, perm);874 const rc = system.open(file_path, flags, perm);
875 switch (errno(rc)) {875 switch (errno(rc)) {
lib/std/os/linux.zig+3-3
...@@ -492,7 +492,7 @@ pub fn renameat2(oldfd: i32, oldpath: [*:0]const u8, newfd: i32, newpath: [*:0]c...@@ -492,7 +492,7 @@ pub fn renameat2(oldfd: i32, oldpath: [*:0]const u8, newfd: i32, newpath: [*:0]c
492 );492 );
493}493}
494494
495pub fn open(path: [*:0]const u8, flags: u32, perm: usize) usize {495pub fn open(path: [*:0]const u8, flags: u32, perm: mode_t) usize {
496 if (@hasField(SYS, "open")) {496 if (@hasField(SYS, "open")) {
497 return syscall3(.open, @ptrToInt(path), flags, perm);497 return syscall3(.open, @ptrToInt(path), flags, perm);
498 } else {498 } else {
...@@ -506,11 +506,11 @@ pub fn open(path: [*:0]const u8, flags: u32, perm: usize) usize {...@@ -506,11 +506,11 @@ pub fn open(path: [*:0]const u8, flags: u32, perm: usize) usize {
506 }506 }
507}507}
508508
509pub fn create(path: [*:0]const u8, perm: usize) usize {509pub fn create(path: [*:0]const u8, perm: mode_t) usize {
510 return syscall2(.creat, @ptrToInt(path), perm);510 return syscall2(.creat, @ptrToInt(path), perm);
511}511}
512512
513pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, mode: usize) usize {513pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, mode: mode_t) usize {
514 // dirfd could be negative, for example AT_FDCWD is -100514 // dirfd could be negative, for example AT_FDCWD is -100
515 return syscall4(.openat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags, mode);515 return syscall4(.openat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags, mode);
516}516}