From 17276df488caff7ff9fd9dbb7e7a9418e924ecc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Tue, 10 Nov 2020 05:23:13 +0000 Subject: [PATCH 1/6] openbsd: add dlfcn.h definitions for dlopen() --- lib/std/os/bits/openbsd.zig | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/std/os/bits/openbsd.zig b/lib/std/os/bits/openbsd.zig index d265c68b42..0e66f14e39 100644 --- a/lib/std/os/bits/openbsd.zig +++ b/lib/std/os/bits/openbsd.zig @@ -32,6 +32,24 @@ pub const Kevent = extern struct { udata: usize, }; +// Modes and flags for dlopen() +// include/dlfcn.h + +/// Bind function calls lazily. +pub const RTLD_LAZY = 1; + +/// Bind function calls immediately. +pub const RTLD_NOW = 2; + +/// Make symbols globally available. +pub const RTLD_GLOBAL = 0x100; + +/// Opposite of RTLD_GLOBAL, and the default. +pub const RTLD_LOCAL = 0x000; + +/// Trace loaded objects and exit. +pub const RTLD_TRACE = 0x200; + pub const dl_phdr_info = extern struct { dlpi_addr: std.elf.Addr, dlpi_name: ?[*:0]const u8, From e4bc595bc6f4ffbfadf132778c4ca635e8e6f3e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Tue, 10 Nov 2020 05:24:03 +0000 Subject: [PATCH 2/6] openbsd: add sockets constants --- lib/std/os/bits/openbsd.zig | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/std/os/bits/openbsd.zig b/lib/std/os/bits/openbsd.zig index 0e66f14e39..5068bf56bf 100644 --- a/lib/std/os/bits/openbsd.zig +++ b/lib/std/os/bits/openbsd.zig @@ -468,6 +468,36 @@ pub const SOCK_SEQPACKET = 5; pub const SOCK_CLOEXEC = 0x8000; pub const SOCK_NONBLOCK = 0x4000; +pub const SO_DEBUG = 0x0001; +pub const SO_ACCEPTCONN = 0x0002; +pub const SO_REUSEADDR = 0x0004; +pub const SO_KEEPALIVE = 0x0008; +pub const SO_DONTROUTE = 0x0010; +pub const SO_BROADCAST = 0x0020; +pub const SO_USELOOPBACK = 0x0040; +pub const SO_LINGER = 0x0080; +pub const SO_OOBINLINE = 0x0100; +pub const SO_REUSEPORT = 0x0200; +pub const SO_TIMESTAMP = 0x0800; +pub const SO_BINDANY = 0x1000; +pub const SO_ZEROIZE = 0x2000; +pub const SO_SNDBUF = 0x1001; +pub const SO_RCVBUF = 0x1002; +pub const SO_SNDLOWAT = 0x1003; +pub const SO_RCVLOWAT = 0x1004; +pub const SO_SNDTIMEO = 0x1005; +pub const SO_RCVTIMEO = 0x1006; +pub const SO_ERROR = 0x1007; +pub const SO_TYPE = 0x1008; +pub const SO_NETPROC = 0x1020; +pub const SO_RTABLE = 0x1021; +pub const SO_PEERCRED = 0x1022; +pub const SO_SPLICE = 0x1023; +pub const SO_DOMAIN = 0x1024; +pub const SO_PROTOCOL = 0x1025; + +pub const SOL_SOCKET = 0xffff; + pub const PF_UNSPEC = AF_UNSPEC; pub const PF_LOCAL = AF_LOCAL; pub const PF_UNIX = AF_UNIX; From 20b19d0092a70999ff58a0f8969e64ec4ecf6c2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Tue, 10 Nov 2020 05:24:56 +0000 Subject: [PATCH 3/6] openbsd: add time definitions for gettimeofday() --- lib/std/os/bits/openbsd.zig | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/std/os/bits/openbsd.zig b/lib/std/os/bits/openbsd.zig index 5068bf56bf..472b93a34a 100644 --- a/lib/std/os/bits/openbsd.zig +++ b/lib/std/os/bits/openbsd.zig @@ -203,7 +203,17 @@ pub const libc_stat = extern struct { pub const timespec = extern struct { tv_sec: time_t, - tv_nsec: isize, + tv_nsec: c_long, +}; + +pub const timeval = extern struct { + tv_sec: time_t, + tv_usec: c_long, +}; + +pub const timezone = extern struct { + tz_minuteswest: c_int, + tz_dsttime: c_int, }; pub const MAXNAMLEN = 255; From 8784c7b581b8a21c3698c4564e2a912676adaa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Tue, 10 Nov 2020 05:25:59 +0000 Subject: [PATCH 4/6] openbsd: proper implementation for Thread.cpuCount() --- lib/std/os/bits/openbsd.zig | 3 +++ lib/std/thread.zig | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/std/os/bits/openbsd.zig b/lib/std/os/bits/openbsd.zig index 472b93a34a..24b4a5add3 100644 --- a/lib/std/os/bits/openbsd.zig +++ b/lib/std/os/bits/openbsd.zig @@ -292,10 +292,13 @@ pub const AI_ADDRCONFIG = 64; pub const CTL_KERN = 1; pub const CTL_DEBUG = 5; +pub const CTL_HW = 6; pub const KERN_PROC_ARGS = 55; pub const KERN_PROC_ARGV = 1; +pub const HW_NCPUONLINE = 25; + pub const PATH_MAX = 1024; pub const STDIN_FILENO = 0; diff --git a/lib/std/thread.zig b/lib/std/thread.zig index 330c425dd6..b45a37ca1b 100644 --- a/lib/std/thread.zig +++ b/lib/std/thread.zig @@ -491,6 +491,16 @@ pub const Thread = struct { if (std.Target.current.os.tag == .windows) { return os.windows.peb().NumberOfProcessors; } + if (std.Target.current.os.tag == .openbsd) { + var count: c_int = undefined; + var count_size: usize = @sizeOf(c_int); + const mib = [_]c_int{ os.CTL_HW, os.HW_NCPUONLINE }; + os.sysctl(&mib, &count, &count_size, null, 0) catch |err| switch (err) { + error.NameTooLong, error.UnknownName => unreachable, + else => |e| return e, + }; + return @intCast(usize, count); + } var count: c_int = undefined; var count_len: usize = @sizeOf(c_int); const name = if (comptime std.Target.current.isDarwin()) "hw.logicalcpu" else "hw.ncpu"; From 9d306e5c77b19fdb8a211f5460d4fd4a7d1fb915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Tue, 10 Nov 2020 05:26:35 +0000 Subject: [PATCH 5/6] openbsd: mutex or cond destroy function could return EINVAL --- lib/std/reset_event.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/std/reset_event.zig b/lib/std/reset_event.zig index a53af100db..eb43be9819 100644 --- a/lib/std/reset_event.zig +++ b/lib/std/reset_event.zig @@ -109,9 +109,12 @@ const PosixEvent = struct { } fn deinit(self: *PosixEvent) void { - // on dragonfly, *destroy() functions can return EINVAL + // on dragonfly or openbsd, *destroy() functions can return EINVAL // for statically initialized pthread structures - const err = if (builtin.os.tag == .dragonfly) os.EINVAL else 0; + const err = if (builtin.os.tag == .dragonfly or builtin.os.tag == .openbsd) + os.EINVAL + else + 0; const retm = c.pthread_mutex_destroy(&self.mutex); assert(retm == 0 or retm == err); From 678bd4fc8919d0683435712fbf256c3fbd4821f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Marie?= Date: Tue, 10 Nov 2020 05:29:53 +0000 Subject: [PATCH 6/6] "ResetEvent" test seems to have a too short timeout: the test is failing randomly on OpenBSD raise the timeout to 100ms to be sure that if it fails (timeout is returned) it is due to a real problem. the test shouldn't be longer: it will wait more time only on failure. --- lib/std/reset_event.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/reset_event.zig b/lib/std/reset_event.zig index eb43be9819..344df200f5 100644 --- a/lib/std/reset_event.zig +++ b/lib/std/reset_event.zig @@ -450,7 +450,7 @@ test "ResetEvent" { fn timedWaiter(self: *Self) !void { self.in.wait(); testing.expectError(error.TimedOut, self.out.timedWait(time.ns_per_us)); - try self.out.timedWait(time.ns_per_ms * 10); + try self.out.timedWait(time.ns_per_ms * 100); testing.expect(self.value == 5); } };