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
signaturebadge-question-mark 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 {
502502 }
503503
504504 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 }
506514 }
507515
508516 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 }
510526 }
511527
512528 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 }
514539 }
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 {
517542 var resume_node = ResumeNode.Basic{
518543 .base = ResumeNode{
519544 .id = ResumeNode.Id.Basic,
......@@ -524,40 +549,37 @@ pub const Loop = struct {
524549 };
525550 defer self.bsdRemoveKev(ident, filter);
526551 suspend {
527 try self.bsdAddKev(&resume_node, ident, filter, fflags);
552 self.bsdAddKev(&resume_node, ident, filter, fflags) catch unreachable;
528553 }
529 return resume_node.kev;
530554 }
531555
532556 /// resume_node must live longer than the anyframe that it holds a reference to.
533557 pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void {
534558 self.beginOneEvent();
535559 errdefer self.finishOneEvent();
536 var kev = os.Kevent{
560 var kev = [1]os.Kevent{os.Kevent{
537561 .ident = ident,
538562 .filter = filter,
539563 .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR,
540564 .fflags = fflags,
541565 .data = 0,
542566 .udata = @ptrToInt(&resume_node.base),
543 };
544 const kevent_array = (*const [1]os.Kevent)(&kev);
545 const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
546 _ = try os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null);
567 }};
568 const empty_kevs = &[0]os.Kevent{};
569 _ = try os.kevent(self.os_data.kqfd, &kev, empty_kevs, null);
547570 }
548571
549572 pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void {
550 var kev = os.Kevent{
573 var kev = [1]os.Kevent{os.Kevent{
551574 .ident = ident,
552575 .filter = filter,
553576 .flags = os.EV_DELETE,
554577 .fflags = 0,
555578 .data = 0,
556579 .udata = 0,
557 };
558 const kevent_array = (*const [1]os.Kevent)(&kev);
559 const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
560 _ = os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined;
580 }};
581 const empty_kevs = &[0]os.Kevent{};
582 _ = os.kevent(self.os_data.kqfd, &kev, empty_kevs, null) catch undefined;
561583 self.finishOneEvent();
562584 }
563585
......@@ -712,7 +734,7 @@ pub const Loop = struct {
712734 }
713735
714736 /// 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 {
716738 var req_node = Request.Node{
717739 .data = .{
718740 .msg = .{
......@@ -733,7 +755,7 @@ pub const Loop = struct {
733755 }
734756
735757 /// 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 {
737759 var req_node = Request.Node{
738760 .data = .{
739761 .msg = .{
lib/std/os.zig+3-3
......@@ -374,7 +374,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
374374 const first = iov[0];
375375 return read(fd, first.iov_base[0..first.iov_len]);
376376 }
377
377
378378 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
379379 while (true) {
380380 // TODO handle the case when iov_len is too large and get rid of this @intCast
......@@ -859,7 +859,7 @@ pub const OpenError = error{
859859/// Open and possibly create a file. Keeps trying if it gets interrupted.
860860/// See also `openC`.
861861/// 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 {
863863 const file_path_c = try toPosixPath(file_path);
864864 return openZ(&file_path_c, flags, perm);
865865}
......@@ -869,7 +869,7 @@ pub const openC = @compileError("deprecated: renamed to openZ");
869869/// Open and possibly create a file. Keeps trying if it gets interrupted.
870870/// See also `open`.
871871/// 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 {
873873 while (true) {
874874 const rc = system.open(file_path, flags, perm);
875875 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
492492 );
493493}
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 {
496496 if (@hasField(SYS, "open")) {
497497 return syscall3(.open, @ptrToInt(path), flags, perm);
498498 } else {
......@@ -506,11 +506,11 @@ pub fn open(path: [*:0]const u8, flags: u32, perm: usize) usize {
506506 }
507507}
508508
509pub fn create(path: [*:0]const u8, perm: usize) usize {
509pub fn create(path: [*:0]const u8, perm: mode_t) usize {
510510 return syscall2(.creat, @ptrToInt(path), perm);
511511}
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 {
514514 // dirfd could be negative, for example AT_FDCWD is -100
515515 return syscall4(.openat, @bitCast(usize, @as(isize, dirfd)), @ptrToInt(path), flags, mode);
516516}