authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-02 14:16:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-02 14:16:59-04:00
log7998e2b0f41bff86d8fbbb8112dd6c629d47e849
tree9f7bca5991f9fcdb291da10e2c830a749769dfad
parent5656f5090d8646e076db50da03cfc6ae686eb76b
parent8ebcca6734e07aea29098ca4c63c0216b3099d0e

Merge remote-tracking branch 'origin/master' into FireFox317-windows-evented-io


8 files changed, 78 insertions(+), 27 deletions(-)

ci/azure/linux_script+1-1
......@@ -14,7 +14,7 @@ sudo apt-get remove -y llvm-*
1414sudo rm -rf /usr/local/*
1515sudo apt-get install -y libxml2-dev libclang-10-dev llvm-10 llvm-10-dev liblld-10-dev cmake s3cmd gcc-7 g++-7
1616
17QEMUBASE="qemu-linux-x86_64-5.0.0"
17QEMUBASE="qemu-linux-x86_64-5.0.0-z2"
1818wget https://ziglang.org/deps/$QEMUBASE.tar.xz
1919tar xf $QEMUBASE.tar.xz
2020PATH=$PWD/$QEMUBASE/bin:$PATH
lib/std/c/netbsd.zig+1
......@@ -9,6 +9,7 @@ pub const _errno = __errno;
99pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int;
1010pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
1111
12pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
1213pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int;
1314pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int;
1415pub extern "c" fn __clock_gettime50(clk_id: c_int, tp: *timespec) c_int;
lib/std/event/loop.zig+40-18
......@@ -467,18 +467,43 @@ pub const Loop = struct {
467467 }
468468
469469 pub fn waitUntilFdReadable(self: *Loop, fd: os.fd_t) void {
470 return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN);
470 switch (builtin.os.tag) {
471 .linux => {
472 self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN);
473 },
474 .macosx, .freebsd, .netbsd, .dragonfly => {
475 self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT);
476 },
477 else => @compileError("Unsupported OS"),
478 }
471479 }
472480
473481 pub fn waitUntilFdWritable(self: *Loop, fd: os.fd_t) void {
474 return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT);
482 switch (builtin.os.tag) {
483 .linux => {
484 self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT);
485 },
486 .macosx, .freebsd, .netbsd, .dragonfly => {
487 self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT);
488 },
489 else => @compileError("Unsupported OS"),
490 }
475491 }
476492
477493 pub fn waitUntilFdWritableOrReadable(self: *Loop, fd: os.fd_t) void {
478 return self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN);
494 switch (builtin.os.tag) {
495 .linux => {
496 self.linuxWaitFd(@intCast(usize, fd), os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN);
497 },
498 .macosx, .freebsd, .netbsd, .dragonfly => {
499 self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT);
500 self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT);
501 },
502 else => @compileError("Unsupported OS"),
503 }
479504 }
480505
481 pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !os.Kevent {
506 pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) void {
482507 var resume_node = ResumeNode.Basic{
483508 .base = ResumeNode{
484509 .id = ResumeNode.Id.Basic,
......@@ -489,40 +514,37 @@ pub const Loop = struct {
489514 };
490515 defer self.bsdRemoveKev(ident, filter);
491516 suspend {
492 try self.bsdAddKev(&resume_node, ident, filter, fflags);
517 self.bsdAddKev(&resume_node, ident, filter, fflags) catch unreachable;
493518 }
494 return resume_node.kev;
495519 }
496520
497521 /// resume_node must live longer than the anyframe that it holds a reference to.
498522 pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void {
499523 self.beginOneEvent();
500524 errdefer self.finishOneEvent();
501 var kev = os.Kevent{
525 var kev = [1]os.Kevent{os.Kevent{
502526 .ident = ident,
503527 .filter = filter,
504528 .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR,
505529 .fflags = fflags,
506530 .data = 0,
507531 .udata = @ptrToInt(&resume_node.base),
508 };
509 const kevent_array = (*const [1]os.Kevent)(&kev);
510 const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
511 _ = try os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null);
532 }};
533 const empty_kevs = &[0]os.Kevent{};
534 _ = try os.kevent(self.os_data.kqfd, &kev, empty_kevs, null);
512535 }
513536
514537 pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void {
515 var kev = os.Kevent{
538 var kev = [1]os.Kevent{os.Kevent{
516539 .ident = ident,
517540 .filter = filter,
518541 .flags = os.EV_DELETE,
519542 .fflags = 0,
520543 .data = 0,
521544 .udata = 0,
522 };
523 const kevent_array = (*const [1]os.Kevent)(&kev);
524 const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
525 _ = os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined;
545 }};
546 const empty_kevs = &[0]os.Kevent{};
547 _ = os.kevent(self.os_data.kqfd, &kev, empty_kevs, null) catch undefined;
526548 self.finishOneEvent();
527549 }
528550
......@@ -679,7 +701,7 @@ pub const Loop = struct {
679701 }
680702
681703 /// Performs an async `os.open` using a separate thread.
682 pub fn openZ(self: *Loop, file_path: [*:0]const u8, flags: u32, mode: usize) os.OpenError!os.fd_t {
704 pub fn openZ(self: *Loop, file_path: [*:0]const u8, flags: u32, mode: os.mode_t) os.OpenError!os.fd_t {
683705 var req_node = Request.Node{
684706 .data = .{
685707 .msg = .{
......@@ -700,7 +722,7 @@ pub const Loop = struct {
700722 }
701723
702724 /// Performs an async `os.opent` using a separate thread.
703 pub fn openatZ(self: *Loop, fd: os.fd_t, file_path: [*:0]const u8, flags: u32, mode: usize) os.OpenError!os.fd_t {
725 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 {
704726 var req_node = Request.Node{
705727 .data = .{
706728 .msg = .{
lib/std/fs.zig+3-3
......@@ -1449,7 +1449,7 @@ pub fn createFileAbsoluteW(absolute_path_w: [*:0]const u16, flags: File.CreateFl
14491449/// Asserts that the path is absolute. See `Dir.deleteFile` for a function that
14501450/// operates on both absolute and relative paths.
14511451/// Asserts that the path parameter has no null bytes.
1452pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void {
1452pub fn deleteFileAbsolute(absolute_path: []const u8) Dir.DeleteFileError!void {
14531453 assert(path.isAbsolute(absolute_path));
14541454 return cwd().deleteFile(absolute_path);
14551455}
......@@ -1457,13 +1457,13 @@ pub fn deleteFileAbsolute(absolute_path: []const u8) DeleteFileError!void {
14571457pub const deleteFileAbsoluteC = @compileError("deprecated: renamed to deleteFileAbsoluteZ");
14581458
14591459/// Same as `deleteFileAbsolute` except the parameter is null-terminated.
1460pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) DeleteFileError!void {
1460pub fn deleteFileAbsoluteZ(absolute_path_c: [*:0]const u8) Dir.DeleteFileError!void {
14611461 assert(path.isAbsoluteZ(absolute_path_c));
14621462 return cwd().deleteFileZ(absolute_path_c);
14631463}
14641464
14651465/// Same as `deleteFileAbsolute` except the parameter is WTF-16 encoded.
1466pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) DeleteFileError!void {
1466pub fn deleteFileAbsoluteW(absolute_path_w: [*:0]const u16) Dir.DeleteFileError!void {
14671467 assert(path.isAbsoluteWindowsW(absolute_path_w));
14681468 return cwd().deleteFileW(absolute_path_w);
14691469}
lib/std/fs/test.zig+16
......@@ -110,6 +110,22 @@ test "create file, lock and read from multiple process at once" {
110110 };
111111}
112112
113test "open file with exclusive nonblocking lock twice (absolute paths)" {
114 const allocator = std.testing.allocator;
115
116 const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"};
117 const filename = try fs.path.resolve(allocator, &file_paths);
118 defer allocator.free(filename);
119
120 const file1 = try fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
121
122 const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });
123 file1.close();
124 std.testing.expectError(error.WouldBlock, file2);
125
126 try fs.deleteFileAbsolute(filename);
127}
128
113129const FileLockTestContext = struct {
114130 filename: []const u8,
115131 pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null,
lib/std/os.zig+6-2
......@@ -153,6 +153,10 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
153153 }
154154 return;
155155 }
156 if (builtin.os.tag == .netbsd) {
157 netbsd.arc4random_buf(buffer.ptr, buffer.len);
158 return;
159 }
156160 if (builtin.os.tag == .wasi) {
157161 switch (wasi.random_get(buffer.ptr, buffer.len)) {
158162 0 => return,
......@@ -854,7 +858,7 @@ pub const OpenError = error{
854858
855859/// Open and possibly create a file. Keeps trying if it gets interrupted.
856860/// See also `openC`.
857pub fn open(file_path: []const u8, flags: u32, perm: usize) OpenError!fd_t {
861pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t {
858862 if (std.Target.current.os.tag == .windows) {
859863 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
860864 return openW(file_path_w.span(), flags, perm);
......@@ -867,7 +871,7 @@ pub const openC = @compileError("deprecated: renamed to openZ");
867871
868872/// Open and possibly create a file. Keeps trying if it gets interrupted.
869873/// See also `open`.
870pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: usize) OpenError!fd_t {
874pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: mode_t) OpenError!fd_t {
871875 if (std.Target.current.os.tag == .windows) {
872876 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
873877 return openW(file_path_w.span(), flags, perm);
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}
src/os.cpp+8
......@@ -1462,6 +1462,14 @@ static void init_rand() {
14621462 unsigned seed;
14631463 memcpy(&seed, ptr_random, sizeof(seed));
14641464 srand(seed);
1465#elif defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD)
1466 unsigned seed;
1467 size_t len = sizeof(seed);
1468 int mib[2] = { CTL_KERN, KERN_ARND };
1469 if (sysctl(mib, 2, &seed, &len, NULL, 0) != 0) {
1470 zig_panic("unable to query random data from sysctl");
1471 }
1472 srand(seed);
14651473#else
14661474 int fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
14671475 if (fd == -1) {