Commit 7cb6279ac0
Changed files (28)
std
fs
special
std/atomic/queue.zig
@@ -186,13 +186,13 @@ test "std.atomic.Queue" {
}
}
} else {
- var putters: [put_thread_count]*std.os.Thread = undefined;
+ var putters: [put_thread_count]*std.Thread = undefined;
for (putters) |*t| {
- t.* = try std.os.spawnThread(&context, startPuts);
+ t.* = try std.Thread.spawn(&context, startPuts);
}
- var getters: [put_thread_count]*std.os.Thread = undefined;
+ var getters: [put_thread_count]*std.Thread = undefined;
for (getters) |*t| {
- t.* = try std.os.spawnThread(&context, startGets);
+ t.* = try std.Thread.spawn(&context, startGets);
}
for (putters) |t|
std/atomic/stack.zig
@@ -120,13 +120,13 @@ test "std.atomic.stack" {
}
}
} else {
- var putters: [put_thread_count]*std.os.Thread = undefined;
+ var putters: [put_thread_count]*std.Thread = undefined;
for (putters) |*t| {
- t.* = try std.os.spawnThread(&context, startPuts);
+ t.* = try std.Thread.spawn(&context, startPuts);
}
- var getters: [put_thread_count]*std.os.Thread = undefined;
+ var getters: [put_thread_count]*std.Thread = undefined;
for (getters) |*t| {
- t.* = try std.os.spawnThread(&context, startGets);
+ t.* = try std.Thread.spawn(&context, startGets);
}
for (putters) |t|
std/c/darwin.zig
@@ -3,7 +3,7 @@ const assert = std.debug.assert;
const builtin = @import("builtin");
const macho = std.macho;
-use @import("posix/darwin.zig");
+use @import("../os/bits.zig");
extern "c" fn __error() *c_int;
pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int;
std/c/linux.zig
@@ -2,6 +2,7 @@ const std = @import("../std.zig");
use std.c;
pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;
+pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
extern "c" fn __errno_location() *c_int;
pub const _errno = __errno_location;
std/event/fs.zig
@@ -5,10 +5,9 @@ const assert = std.debug.assert;
const testing = std.testing;
const os = std.os;
const mem = std.mem;
-const posix = os.posix;
const windows = os.windows;
const Loop = event.Loop;
-const fd_t = posix.fd_t;
+const fd_t = os.fd_t;
const File = std.fs.File;
pub const RequestNode = std.atomic.Queue(Request).Node;
@@ -33,7 +32,7 @@ pub const Request = struct {
pub const PWriteV = struct {
fd: fd_t,
- iov: []const os.posix.iovec_const,
+ iov: []const os.iovec_const,
offset: usize,
result: Error!void,
@@ -42,7 +41,7 @@ pub const Request = struct {
pub const PReadV = struct {
fd: fd_t,
- iov: []const os.posix.iovec,
+ iov: []const os.iovec,
offset: usize,
result: Error!usize,
@@ -89,11 +88,11 @@ pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: us
builtin.Os.freebsd,
builtin.Os.netbsd,
=> {
- const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len);
+ const iovecs = try loop.allocator.alloc(os.iovec_const, data.len);
defer loop.allocator.free(iovecs);
for (data) |buf, i| {
- iovecs[i] = os.posix.iovec_const{
+ iovecs[i] = os.iovec_const{
.iov_base = buf.ptr,
.iov_len = buf.len,
};
@@ -171,7 +170,7 @@ pub async fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64)
pub async fn pwritevPosix(
loop: *Loop,
fd: fd_t,
- iovecs: []const posix.iovec_const,
+ iovecs: []const os.iovec_const,
offset: usize,
) os.PosixWriteError!void {
// workaround for https://github.com/ziglang/zig/issues/1194
@@ -226,11 +225,11 @@ pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PR
builtin.Os.freebsd,
builtin.Os.netbsd,
=> {
- const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len);
+ const iovecs = try loop.allocator.alloc(os.iovec, data.len);
defer loop.allocator.free(iovecs);
for (data) |buf, i| {
- iovecs[i] = os.posix.iovec{
+ iovecs[i] = os.iovec{
.iov_base = buf.ptr,
.iov_len = buf.len,
};
@@ -319,7 +318,7 @@ pub async fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize
pub async fn preadvPosix(
loop: *Loop,
fd: fd_t,
- iovecs: []const posix.iovec,
+ iovecs: []const os.iovec,
offset: usize,
) os.PosixReadError!usize {
// workaround for https://github.com/ziglang/zig/issues/1194
@@ -405,7 +404,7 @@ pub async fn openPosix(
pub async fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t {
switch (builtin.os) {
builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => {
- const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC;
+ const flags = os.O_LARGEFILE | os.O_RDONLY | os.O_CLOEXEC;
return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable);
},
@@ -435,7 +434,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File.
builtin.Os.freebsd,
builtin.Os.netbsd,
=> {
- const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;
+ const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC | os.O_TRUNC;
return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable);
},
builtin.Os.windows => return os.windowsOpen(
@@ -457,7 +456,7 @@ pub async fn openReadWrite(
) File.OpenError!fd_t {
switch (builtin.os) {
builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => {
- const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC;
+ const flags = os.O_LARGEFILE | os.O_RDWR | os.O_CREAT | os.O_CLOEXEC;
return await (async openPosix(loop, path, flags, mode) catch unreachable);
},
@@ -888,10 +887,7 @@ pub fn Watch(comptime V: type) type {
var close_op_consumed = false;
defer if (!close_op_consumed) close_op.finish();
- const flags = switch (builtin.os) {
- builtin.Os.macosx => posix.O_SYMLINK | posix.O_EVTONLY,
- else => 0,
- };
+ const flags = if (os.darwin.is_the_target) os.O_SYMLINK | os.O_EVTONLY else 0;
const mode = 0;
const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable);
close_op.setHandle(fd);
@@ -943,16 +939,16 @@ pub fn Watch(comptime V: type) type {
while (true) {
if (await (async self.channel.loop.bsdWaitKev(
@intCast(usize, close_op.getHandle()),
- posix.EVFILT_VNODE,
- posix.NOTE_WRITE | posix.NOTE_DELETE,
+ os.EVFILT_VNODE,
+ os.NOTE_WRITE | os.NOTE_DELETE,
) catch unreachable)) |kev| {
// TODO handle EV_ERROR
- if (kev.fflags & posix.NOTE_DELETE != 0) {
+ if (kev.fflags & os.NOTE_DELETE != 0) {
await (async self.channel.put(Self.Event{
.id = Event.Id.Delete,
.data = value_copy,
}) catch unreachable);
- } else if (kev.fflags & posix.NOTE_WRITE != 0) {
+ } else if (kev.fflags & os.NOTE_WRITE != 0) {
await (async self.channel.put(Self.Event{
.id = Event.Id.CloseWrite,
.data = value_copy,
std/event/loop.zig
@@ -7,9 +7,9 @@ const AtomicRmwOp = builtin.AtomicRmwOp;
const AtomicOrder = builtin.AtomicOrder;
const fs = std.event.fs;
const os = std.os;
-const posix = os.posix;
const windows = os.windows;
const maxInt = std.math.maxInt;
+const Thread = std.Thread;
pub const Loop = struct {
allocator: *mem.Allocator,
@@ -17,7 +17,7 @@ pub const Loop = struct {
os_data: OsData,
final_resume_node: ResumeNode,
pending_event_count: usize,
- extra_threads: []*os.Thread,
+ extra_threads: []*Thread,
// pre-allocated eventfds. all permanently active.
// this is how we send promises to be resumed on other threads.
@@ -65,7 +65,7 @@ pub const Loop = struct {
const KEventFd = struct {
base: ResumeNode,
- kevent: posix.Kevent,
+ kevent: os.Kevent,
};
pub const Basic = switch (builtin.os) {
@@ -81,7 +81,7 @@ pub const Loop = struct {
const KEventBasic = struct {
base: ResumeNode,
- kev: posix.Kevent,
+ kev: os.Kevent,
};
};
@@ -127,7 +127,7 @@ pub const Loop = struct {
);
errdefer self.allocator.free(self.eventfd_resume_nodes);
- self.extra_threads = try self.allocator.alloc(*os.Thread, extra_thread_count);
+ self.extra_threads = try self.allocator.alloc(*Thread, extra_thread_count);
errdefer self.allocator.free(self.extra_threads);
try self.initOsData(extra_thread_count);
@@ -172,32 +172,32 @@ pub const Loop = struct {
.handle = undefined,
.overlapped = ResumeNode.overlapped_init,
},
- .eventfd = try os.linuxEventFd(1, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK),
- .epoll_op = posix.EPOLL_CTL_ADD,
+ .eventfd = try os.linuxEventFd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK),
+ .epoll_op = os.EPOLL_CTL_ADD,
},
.next = undefined,
};
self.available_eventfd_resume_nodes.push(eventfd_node);
}
- self.os_data.epollfd = try os.linuxEpollCreate(posix.EPOLL_CLOEXEC);
+ self.os_data.epollfd = try os.linuxEpollCreate(os.EPOLL_CLOEXEC);
errdefer os.close(self.os_data.epollfd);
- self.os_data.final_eventfd = try os.linuxEventFd(0, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK);
+ self.os_data.final_eventfd = try os.linuxEventFd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK);
errdefer os.close(self.os_data.final_eventfd);
- self.os_data.final_eventfd_event = posix.epoll_event{
- .events = posix.EPOLLIN,
- .data = posix.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) },
+ self.os_data.final_eventfd_event = os.epoll_event{
+ .events = os.EPOLLIN,
+ .data = os.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) },
};
try os.linuxEpollCtl(
self.os_data.epollfd,
- posix.EPOLL_CTL_ADD,
+ os.EPOLL_CTL_ADD,
self.os_data.final_eventfd,
&self.os_data.final_eventfd_event,
);
- self.os_data.fs_thread = try os.spawnThread(self, posixFsRun);
+ self.os_data.fs_thread = try Thread.spawn(self, posixFsRun);
errdefer {
self.posixFsRequest(&self.os_data.fs_end_request);
self.os_data.fs_thread.wait();
@@ -218,7 +218,7 @@ pub const Loop = struct {
}
}
while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) {
- self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun);
+ self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun);
}
},
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
@@ -240,7 +240,7 @@ pub const Loop = struct {
},
};
- const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
+ const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
for (self.eventfd_resume_nodes) |*eventfd_node, i| {
eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{
@@ -251,10 +251,10 @@ pub const Loop = struct {
.overlapped = ResumeNode.overlapped_init,
},
// this one is for sending events
- .kevent = posix.Kevent{
+ .kevent = os.Kevent{
.ident = i,
- .filter = posix.EVFILT_USER,
- .flags = posix.EV_CLEAR | posix.EV_ADD | posix.EV_DISABLE,
+ .filter = os.EVFILT_USER,
+ .flags = os.EV_CLEAR | os.EV_ADD | os.EV_DISABLE,
.fflags = 0,
.data = 0,
.udata = @ptrToInt(&eventfd_node.data.base),
@@ -263,46 +263,46 @@ pub const Loop = struct {
.next = undefined,
};
self.available_eventfd_resume_nodes.push(eventfd_node);
- const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.data.kevent);
+ const kevent_array = (*const [1]os.Kevent)(&eventfd_node.data.kevent);
_ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null);
- eventfd_node.data.kevent.flags = posix.EV_CLEAR | posix.EV_ENABLE;
- eventfd_node.data.kevent.fflags = posix.NOTE_TRIGGER;
+ eventfd_node.data.kevent.flags = os.EV_CLEAR | os.EV_ENABLE;
+ eventfd_node.data.kevent.fflags = os.NOTE_TRIGGER;
}
// Pre-add so that we cannot get error.SystemResources
// later when we try to activate it.
- self.os_data.final_kevent = posix.Kevent{
+ self.os_data.final_kevent = os.Kevent{
.ident = extra_thread_count,
- .filter = posix.EVFILT_USER,
- .flags = posix.EV_ADD | posix.EV_DISABLE,
+ .filter = os.EVFILT_USER,
+ .flags = os.EV_ADD | os.EV_DISABLE,
.fflags = 0,
.data = 0,
.udata = @ptrToInt(&self.final_resume_node),
};
- const final_kev_arr = (*const [1]posix.Kevent)(&self.os_data.final_kevent);
+ const final_kev_arr = (*const [1]os.Kevent)(&self.os_data.final_kevent);
_ = try os.bsdKEvent(self.os_data.kqfd, final_kev_arr, empty_kevs, null);
- self.os_data.final_kevent.flags = posix.EV_ENABLE;
- self.os_data.final_kevent.fflags = posix.NOTE_TRIGGER;
+ self.os_data.final_kevent.flags = os.EV_ENABLE;
+ self.os_data.final_kevent.fflags = os.NOTE_TRIGGER;
- self.os_data.fs_kevent_wake = posix.Kevent{
+ self.os_data.fs_kevent_wake = os.Kevent{
.ident = 0,
- .filter = posix.EVFILT_USER,
- .flags = posix.EV_ADD | posix.EV_ENABLE,
- .fflags = posix.NOTE_TRIGGER,
+ .filter = os.EVFILT_USER,
+ .flags = os.EV_ADD | os.EV_ENABLE,
+ .fflags = os.NOTE_TRIGGER,
.data = 0,
.udata = undefined,
};
- self.os_data.fs_kevent_wait = posix.Kevent{
+ self.os_data.fs_kevent_wait = os.Kevent{
.ident = 0,
- .filter = posix.EVFILT_USER,
- .flags = posix.EV_ADD | posix.EV_CLEAR,
+ .filter = os.EVFILT_USER,
+ .flags = os.EV_ADD | os.EV_CLEAR,
.fflags = 0,
.data = 0,
.udata = undefined,
};
- self.os_data.fs_thread = try os.spawnThread(self, posixFsRun);
+ self.os_data.fs_thread = try Thread.spawn(self, posixFsRun);
errdefer {
self.posixFsRequest(&self.os_data.fs_end_request);
self.os_data.fs_thread.wait();
@@ -322,7 +322,7 @@ pub const Loop = struct {
}
}
while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) {
- self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun);
+ self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun);
}
},
builtin.Os.windows => {
@@ -371,7 +371,7 @@ pub const Loop = struct {
}
}
while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) {
- self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun);
+ self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun);
}
},
else => {},
@@ -400,19 +400,19 @@ pub const Loop = struct {
/// resume_node must live longer than the promise that it holds a reference to.
/// flags must contain EPOLLET
pub fn linuxAddFd(self: *Loop, fd: i32, resume_node: *ResumeNode, flags: u32) !void {
- assert(flags & posix.EPOLLET == posix.EPOLLET);
+ assert(flags & os.EPOLLET == os.EPOLLET);
self.beginOneEvent();
errdefer self.finishOneEvent();
try self.linuxModFd(
fd,
- posix.EPOLL_CTL_ADD,
+ os.EPOLL_CTL_ADD,
flags,
resume_node,
);
}
pub fn linuxModFd(self: *Loop, fd: i32, op: u32, flags: u32, resume_node: *ResumeNode) !void {
- assert(flags & posix.EPOLLET == posix.EPOLLET);
+ assert(flags & os.EPOLLET == os.EPOLLET);
var ev = os.linux.epoll_event{
.events = flags,
.data = os.linux.epoll_data{ .ptr = @ptrToInt(resume_node) },
@@ -440,7 +440,7 @@ pub const Loop = struct {
}
}
- pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !posix.Kevent {
+ pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !os.Kevent {
// TODO #1194
suspend {
resume @handle();
@@ -464,30 +464,30 @@ pub const Loop = struct {
pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void {
self.beginOneEvent();
errdefer self.finishOneEvent();
- var kev = posix.Kevent{
+ var kev = os.Kevent{
.ident = ident,
.filter = filter,
- .flags = posix.EV_ADD | posix.EV_ENABLE | posix.EV_CLEAR,
+ .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR,
.fflags = fflags,
.data = 0,
.udata = @ptrToInt(&resume_node.base),
};
- const kevent_array = (*const [1]posix.Kevent)(&kev);
- const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
+ const kevent_array = (*const [1]os.Kevent)(&kev);
+ const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
_ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null);
}
pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void {
- var kev = posix.Kevent{
+ var kev = os.Kevent{
.ident = ident,
.filter = filter,
- .flags = posix.EV_DELETE,
+ .flags = os.EV_DELETE,
.fflags = 0,
.data = 0,
.udata = 0,
};
- const kevent_array = (*const [1]posix.Kevent)(&kev);
- const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
+ const kevent_array = (*const [1]os.Kevent)(&kev);
+ const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
_ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined;
self.finishOneEvent();
}
@@ -502,8 +502,8 @@ pub const Loop = struct {
eventfd_node.base.handle = next_tick_node.data;
switch (builtin.os) {
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
- const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.kevent);
- const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
+ const kevent_array = (*const [1]os.Kevent)(&eventfd_node.kevent);
+ const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
_ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch {
self.next_tick_queue.unget(next_tick_node);
self.available_eventfd_resume_nodes.push(resume_stack_node);
@@ -512,7 +512,7 @@ pub const Loop = struct {
},
builtin.Os.linux => {
// the pending count is already accounted for
- const epoll_events = posix.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT |
+ const epoll_events = os.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT |
os.linux.EPOLLET;
self.linuxModFd(
eventfd_node.eventfd,
@@ -631,8 +631,8 @@ pub const Loop = struct {
},
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
self.posixFsRequest(&self.os_data.fs_end_request);
- const final_kevent = (*const [1]posix.Kevent)(&self.os_data.final_kevent);
- const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
+ const final_kevent = (*const [1]os.Kevent)(&self.os_data.final_kevent);
+ const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
// cannot fail because we already added it and this just enables it
_ = os.bsdKEvent(self.os_data.kqfd, final_kevent, empty_kevs, null) catch unreachable;
return;
@@ -676,7 +676,7 @@ pub const Loop = struct {
ResumeNode.Id.Stop => return,
ResumeNode.Id.EventFd => {
const event_fd_node = @fieldParentPtr(ResumeNode.EventFd, "base", resume_node);
- event_fd_node.epoll_op = posix.EPOLL_CTL_MOD;
+ event_fd_node.epoll_op = os.EPOLL_CTL_MOD;
const stack_node = @fieldParentPtr(std.atomic.Stack(ResumeNode.EventFd).Node, "data", event_fd_node);
self.available_eventfd_resume_nodes.push(stack_node);
},
@@ -688,8 +688,8 @@ pub const Loop = struct {
}
},
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
- var eventlist: [1]posix.Kevent = undefined;
- const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
+ var eventlist: [1]os.Kevent = undefined;
+ const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
const count = os.bsdKEvent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable;
for (eventlist[0..count]) |ev| {
const resume_node = @intToPtr(*ResumeNode, ev.udata);
@@ -751,8 +751,8 @@ pub const Loop = struct {
self.os_data.fs_queue.put(request_node);
switch (builtin.os) {
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
- const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wake);
- const empty_kevs = ([*]posix.Kevent)(undefined)[0..0];
+ const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wake);
+ const empty_kevs = ([*]os.Kevent)(undefined)[0..0];
_ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable;
},
builtin.Os.linux => {
@@ -760,7 +760,7 @@ pub const Loop = struct {
const rc = os.linux.futex_wake(&self.os_data.fs_queue_item, os.linux.FUTEX_WAKE, 1);
switch (os.linux.getErrno(rc)) {
0 => {},
- posix.EINVAL => unreachable,
+ os.EINVAL => unreachable,
else => unreachable,
}
},
@@ -793,8 +793,8 @@ pub const Loop = struct {
},
@TagType(fs.Request.Msg).Close => |*msg| os.close(msg.fd),
@TagType(fs.Request.Msg).WriteFile => |*msg| blk: {
- const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT |
- posix.O_CLOEXEC | posix.O_TRUNC;
+ const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT |
+ os.O_CLOEXEC | os.O_TRUNC;
const fd = os.openC(msg.path.ptr, flags, msg.mode) catch |err| {
msg.result = err;
break :blk;
@@ -816,13 +816,13 @@ pub const Loop = struct {
builtin.Os.linux => {
const rc = os.linux.futex_wait(&self.os_data.fs_queue_item, os.linux.FUTEX_WAIT, 0, null);
switch (os.linux.getErrno(rc)) {
- 0, posix.EINTR, posix.EAGAIN => continue,
+ 0, os.EINTR, os.EAGAIN => continue,
else => unreachable,
}
},
builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
- const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wait);
- var out_kevs: [1]posix.Kevent = undefined;
+ const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wait);
+ var out_kevs: [1]os.Kevent = undefined;
_ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable;
},
else => @compileError("Unsupported OS"),
@@ -842,10 +842,10 @@ pub const Loop = struct {
const KEventData = struct {
kqfd: i32,
- final_kevent: posix.Kevent,
- fs_kevent_wake: posix.Kevent,
- fs_kevent_wait: posix.Kevent,
- fs_thread: *os.Thread,
+ final_kevent: os.Kevent,
+ fs_kevent_wake: os.Kevent,
+ fs_kevent_wait: os.Kevent,
+ fs_thread: *Thread,
fs_kqfd: i32,
fs_queue: std.atomic.Queue(fs.Request),
fs_end_request: fs.RequestNode,
@@ -855,7 +855,7 @@ pub const Loop = struct {
epollfd: i32,
final_eventfd: i32,
final_eventfd_event: os.linux.epoll_event,
- fs_thread: *os.Thread,
+ fs_thread: *Thread,
fs_queue_item: i32,
fs_queue: std.atomic.Queue(fs.Request),
fs_end_request: fs.RequestNode,
std/event/net.zig
@@ -4,11 +4,9 @@ const testing = std.testing;
const event = std.event;
const mem = std.mem;
const os = std.os;
-const posix = os.posix;
const Loop = std.event.Loop;
const File = std.fs.File;
-
-const fd_t = posix.fd_t;
+const fd_t = os.fd_t;
pub const Server = struct {
handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, File) void,
@@ -47,19 +45,19 @@ pub const Server = struct {
) !void {
self.handleRequestFn = handleRequestFn;
- const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
+ const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp);
errdefer os.close(sockfd);
self.sockfd = sockfd;
try os.posixBind(sockfd, &address.os_addr);
- try os.posixListen(sockfd, posix.SOMAXCONN);
+ try os.posixListen(sockfd, os.SOMAXCONN);
self.listen_address = std.net.Address.initPosix(try os.posixGetSockName(sockfd));
self.accept_coro = try async<self.loop.allocator> Server.handler(self);
errdefer cancel self.accept_coro.?;
self.listen_resume_node.handle = self.accept_coro.?;
- try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET);
+ try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET);
errdefer self.loop.removeFd(sockfd);
}
@@ -78,7 +76,7 @@ pub const Server = struct {
while (true) {
var accepted_addr: std.net.Address = undefined;
// TODO just inline the following function here and don't expose it as posixAsyncAccept
- if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| {
+ if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| {
if (accepted_fd == -1) {
// would block
suspend; // we will get resumed by epoll_wait in the event loop
@@ -108,22 +106,22 @@ pub const Server = struct {
pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 {
const sockfd = try os.posixSocket(
- posix.AF_UNIX,
- posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK,
+ os.AF_UNIX,
+ os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK,
0,
);
errdefer os.close(sockfd);
- var sock_addr = posix.sockaddr_un{
- .family = posix.AF_UNIX,
+ var sock_addr = os.sockaddr_un{
+ .family = os.AF_UNIX,
.path = undefined,
};
if (path.len > @typeOf(sock_addr.path).len) return error.NameTooLong;
mem.copy(u8, sock_addr.path[0..], path);
- const size = @intCast(u32, @sizeOf(posix.sa_family_t) + path.len);
+ const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len);
try os.posixConnectAsync(sockfd, &sock_addr, size);
- try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET);
+ try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET);
try os.posixGetSockOptConnectError(sockfd);
return sockfd;
@@ -143,50 +141,48 @@ pub const ReadError = error{
/// returns number of bytes read. 0 means EOF.
pub async fn read(loop: *std.event.Loop, fd: fd_t, buffer: []u8) ReadError!usize {
- const iov = posix.iovec{
+ const iov = os.iovec{
.iov_base = buffer.ptr,
.iov_len = buffer.len,
};
- const iovs: *const [1]posix.iovec = &iov;
+ const iovs: *const [1]os.iovec = &iov;
return await (async readvPosix(loop, fd, iovs, 1) catch unreachable);
}
pub const WriteError = error{};
pub async fn write(loop: *std.event.Loop, fd: fd_t, buffer: []const u8) WriteError!void {
- const iov = posix.iovec_const{
+ const iov = os.iovec_const{
.iov_base = buffer.ptr,
.iov_len = buffer.len,
};
- const iovs: *const [1]posix.iovec_const = &iov;
+ const iovs: *const [1]os.iovec_const = &iov;
return await (async writevPosix(loop, fd, iovs, 1) catch unreachable);
}
-pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, count: usize) !void {
+pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const os.iovec_const, count: usize) !void {
while (true) {
switch (builtin.os) {
- builtin.Os.macosx, builtin.Os.linux => {
- const rc = posix.writev(fd, iov, count);
- const err = posix.getErrno(rc);
- switch (err) {
+ .macosx, .linux => {
+ switch (os.errno(os.system.writev(fd, iov, count))) {
0 => return,
- posix.EINTR => continue,
- posix.ESPIPE => unreachable,
- posix.EINVAL => unreachable,
- posix.EFAULT => unreachable,
- posix.EAGAIN => {
- try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLOUT) catch unreachable);
+ os.EINTR => continue,
+ os.ESPIPE => unreachable,
+ os.EINVAL => unreachable,
+ os.EFAULT => unreachable,
+ os.EAGAIN => {
+ try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLOUT) catch unreachable);
continue;
},
- posix.EBADF => unreachable, // always a race condition
- posix.EDESTADDRREQ => unreachable, // connect was never called
- posix.EDQUOT => unreachable,
- posix.EFBIG => unreachable,
- posix.EIO => return error.InputOutput,
- posix.ENOSPC => unreachable,
- posix.EPERM => return error.AccessDenied,
- posix.EPIPE => unreachable,
- else => return os.unexpectedErrorPosix(err),
+ os.EBADF => unreachable, // always a race condition
+ os.EDESTADDRREQ => unreachable, // connect was never called
+ os.EDQUOT => unreachable,
+ os.EFBIG => unreachable,
+ os.EIO => return error.InputOutput,
+ os.ENOSPC => unreachable,
+ os.EPERM => return error.AccessDenied,
+ os.EPIPE => unreachable,
+ else => |err| return os.unexpectedErrno(err),
}
},
else => @compileError("Unsupported OS"),
@@ -195,27 +191,26 @@ pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const,
}
/// returns number of bytes read. 0 means EOF.
-pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, count: usize) !usize {
+pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]os.iovec, count: usize) !usize {
while (true) {
switch (builtin.os) {
builtin.Os.linux, builtin.Os.freebsd, builtin.Os.macosx => {
- const rc = posix.readv(fd, iov, count);
- const err = posix.getErrno(rc);
- switch (err) {
+ const rc = os.system.readv(fd, iov, count);
+ switch (os.errno(rc)) {
0 => return rc,
- posix.EINTR => continue,
- posix.EINVAL => unreachable,
- posix.EFAULT => unreachable,
- posix.EAGAIN => {
- try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLIN) catch unreachable);
+ os.EINTR => continue,
+ os.EINVAL => unreachable,
+ os.EFAULT => unreachable,
+ os.EAGAIN => {
+ try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLIN) catch unreachable);
continue;
},
- posix.EBADF => unreachable, // always a race condition
- posix.EIO => return error.InputOutput,
- posix.EISDIR => unreachable,
- posix.ENOBUFS => return error.SystemResources,
- posix.ENOMEM => return error.SystemResources,
- else => return os.unexpectedErrorPosix(err),
+ os.EBADF => unreachable, // always a race condition
+ os.EIO => return error.InputOutput,
+ os.EISDIR => unreachable,
+ os.ENOBUFS => return error.SystemResources,
+ os.ENOMEM => return error.SystemResources,
+ else => |err| return os.unexpectedErrno(err),
}
},
else => @compileError("Unsupported OS"),
@@ -224,11 +219,11 @@ pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, cou
}
pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void {
- const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len);
+ const iovecs = try loop.allocator.alloc(os.iovec_const, data.len);
defer loop.allocator.free(iovecs);
for (data) |buf, i| {
- iovecs[i] = os.posix.iovec_const{
+ iovecs[i] = os.iovec_const{
.iov_base = buf.ptr,
.iov_len = buf.len,
};
@@ -238,11 +233,11 @@ pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void {
}
pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize {
- const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len);
+ const iovecs = try loop.allocator.alloc(os.iovec, data.len);
defer loop.allocator.free(iovecs);
for (data) |buf, i| {
- iovecs[i] = os.posix.iovec{
+ iovecs[i] = os.iovec{
.iov_base = buf.ptr,
.iov_len = buf.len,
};
@@ -254,11 +249,11 @@ pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize {
pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File {
var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592
- const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp);
+ const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp);
errdefer os.close(sockfd);
- try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(posix.sockaddr_in));
- try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET);
+ try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in));
+ try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET);
try os.posixGetSockOptConnectError(sockfd);
return File.openHandle(sockfd);
std/fs/path.zig
@@ -9,24 +9,21 @@ const fmt = std.fmt;
const Allocator = mem.Allocator;
const os = std.os;
const math = std.math;
-const posix = os.posix;
const windows = os.windows;
const cstr = std.cstr;
pub const sep_windows = '\\';
pub const sep_posix = '/';
-pub const sep = if (is_windows) sep_windows else sep_posix;
+pub const sep = if (windows.is_the_target) sep_windows else sep_posix;
pub const sep_str = [1]u8{sep};
pub const delimiter_windows = ';';
pub const delimiter_posix = ':';
-pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix;
-
-const is_windows = builtin.os == builtin.Os.windows;
+pub const delimiter = if (windows.is_the_target) delimiter_windows else delimiter_posix;
pub fn isSep(byte: u8) bool {
- if (is_windows) {
+ if (windows.is_the_target) {
return byte == '/' or byte == '\\';
} else {
return byte == '/';
@@ -76,7 +73,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u
return buf;
}
-pub const join = if (is_windows) joinWindows else joinPosix;
+pub const join = if (windows.is_the_target) joinWindows else joinPosix;
/// Naively combines a series of paths with the native path seperator.
/// Allocates memory for the result, which must be freed by the caller.
@@ -133,7 +130,7 @@ test "join" {
}
pub fn isAbsolute(path: []const u8) bool {
- if (is_windows) {
+ if (windows.is_the_target) {
return isAbsoluteWindows(path);
} else {
return isAbsolutePosix(path);
@@ -312,7 +309,7 @@ test "windowsParsePath" {
}
pub fn diskDesignator(path: []const u8) []const u8 {
- if (is_windows) {
+ if (windows.is_the_target) {
return diskDesignatorWindows(path);
} else {
return "";
@@ -377,7 +374,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
- if (is_windows) {
+ if (windows.is_the_target) {
return resolveWindows(allocator, paths);
} else {
return resolvePosix(allocator, paths);
@@ -394,7 +391,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
/// Without performing actual syscalls, resolving `..` could be incorrect.
pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (paths.len == 0) {
- assert(is_windows); // resolveWindows called on non windows can't use getCwd
+ assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd
return os.getCwdAlloc(allocator);
}
@@ -489,7 +486,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
result_disk_designator = result[0..result_index];
},
WindowsPath.Kind.None => {
- assert(is_windows); // resolveWindows called on non windows can't use getCwd
+ assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd
const cwd = try os.getCwdAlloc(allocator);
defer allocator.free(cwd);
const parsed_cwd = windowsParsePath(cwd);
@@ -504,7 +501,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
},
}
} else {
- assert(is_windows); // resolveWindows called on non windows can't use getCwd
+ assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd
// TODO call get cwd for the result_disk_designator instead of the global one
const cwd = try os.getCwdAlloc(allocator);
defer allocator.free(cwd);
@@ -575,7 +572,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
/// Without performing actual syscalls, resolving `..` could be incorrect.
pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (paths.len == 0) {
- assert(!is_windows); // resolvePosix called on windows can't use getCwd
+ assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd
return os.getCwdAlloc(allocator);
}
@@ -597,7 +594,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (have_abs) {
result = try allocator.alloc(u8, max_size);
} else {
- assert(!is_windows); // resolvePosix called on windows can't use getCwd
+ assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd
const cwd = try os.getCwdAlloc(allocator);
defer allocator.free(cwd);
result = try allocator.alloc(u8, max_size + cwd.len + 1);
@@ -638,7 +635,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
test "resolve" {
const cwd = try os.getCwdAlloc(debug.global_allocator);
- if (is_windows) {
+ if (windows.is_the_target) {
if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
cwd[0] = asciiUpper(cwd[0]);
}
@@ -650,7 +647,7 @@ test "resolve" {
}
test "resolveWindows" {
- if (is_windows) {
+ if (windows.is_the_target) {
const cwd = try os.getCwdAlloc(debug.global_allocator);
const parsed_cwd = windowsParsePath(cwd);
{
@@ -716,7 +713,7 @@ fn testResolvePosix(paths: []const []const u8) []u8 {
/// If the path is a file in the current directory (no directory component)
/// then returns null
pub fn dirname(path: []const u8) ?[]const u8 {
- if (is_windows) {
+ if (windows.is_the_target) {
return dirnameWindows(path);
} else {
return dirnamePosix(path);
@@ -848,7 +845,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void {
}
pub fn basename(path: []const u8) []const u8 {
- if (is_windows) {
+ if (windows.is_the_target) {
return basenameWindows(path);
} else {
return basenamePosix(path);
@@ -964,7 +961,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void {
/// string is returned.
/// On Windows this canonicalizes the drive to a capital letter and paths to `\\`.
pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {
- if (is_windows) {
+ if (windows.is_the_target) {
return relativeWindows(allocator, from, to);
} else {
return relativePosix(allocator, from, to);
std/os/bits/linux.zig
@@ -1,3 +1,5 @@
+const std = @import("../../std.zig");
+
pub use @import("linux/errno.zig");
pub use switch (builtin.arch) {
.x86_64 => @import("linux/x86_64.zig"),
@@ -907,3 +909,26 @@ pub const pthread_attr_t = extern struct {
__size: [56]u8,
__align: c_long,
};
+
+pub const CPU_SETSIZE = 128;
+pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize;
+pub const cpu_count_t = @IntType(false, std.math.log2(CPU_SETSIZE * 8));
+
+pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t {
+ var sum: cpu_count_t = 0;
+ for (set) |x| {
+ sum += @popCount(usize, x);
+ }
+ return sum;
+}
+
+// TODO port these over
+//#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set)
+//#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set)
+//#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set)
+//#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2)
+//#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2)
+//#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2)
+//#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set)
+//#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set)
+//#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2)
std/os/linux/sys.zig
@@ -1,859 +0,0 @@
-// This file provides the system interface functions for Linux matching those
-// that are provided by libc, whether or not libc is linked. The following
-// abstractions are made:
-// * Work around kernel bugs and limitations. For example, see sendmmsg.
-// * Implement all the syscalls in the same way that libc functions will
-// provide `rename` when only the `renameat` syscall exists.
-// * Does not support POSIX thread cancellation.
-const std = @import("../../std.zig");
-const builtin = @import("builtin");
-const assert = std.debug.assert;
-const maxInt = std.math.maxInt;
-const elf = std.elf;
-const vdso = @import("vdso.zig");
-const dl = @import("../../dynamic_library.zig");
-pub use switch (builtin.arch) {
- .x86_64 => @import("x86_64.zig"),
- .aarch64 => @import("arm64.zig"),
- else => struct {},
-};
-pub use @import("../bits.zig");
-
-/// See `std.os.posix.getauxval`.
-pub var elf_aux_maybe: ?[*]std.elf.Auxv = null;
-
-/// Get the errno from a syscall return value, or 0 for no error.
-pub fn getErrno(r: usize) u12 {
- const signed_r = @bitCast(isize, r);
- return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0;
-}
-
-pub fn dup2(old: i32, new: i32) usize {
- if (@hasDecl(@This(), "SYS_dup2")) {
- return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)));
- } else {
- if (old == new) {
- if (std.debug.runtime_safety) {
- const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD);
- if (@bitCast(isize, rc) < 0) return rc;
- }
- return @intCast(usize, old);
- } else {
- return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0);
- }
- }
-}
-
-pub fn dup3(old: i32, new: i32, flags: u32) usize {
- return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn chdir(path: [*]const u8) usize {
- return syscall1(SYS_chdir, @ptrToInt(path));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn chroot(path: [*]const u8) usize {
- return syscall1(SYS_chroot, @ptrToInt(path));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize {
- return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp));
-}
-
-pub fn fork() usize {
- if (@hasDecl(@This(), "SYS_fork")) {
- return syscall0(SYS_fork);
- } else {
- return syscall2(SYS_clone, SIGCHLD, 0);
- }
-}
-
-/// This must be inline, and inline call the syscall function, because if the
-/// child does a return it will clobber the parent's stack.
-/// It is advised to avoid this function and use clone instead, because
-/// the compiler is not aware of how vfork affects control flow and you may
-/// see different results in optimized builds.
-pub inline fn vfork() usize {
- return @inlineCall(syscall0, SYS_vfork);
-}
-
-pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize {
- return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout));
-}
-
-pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize {
- return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val));
-}
-
-pub fn getcwd(buf: [*]u8, size: usize) usize {
- return syscall2(SYS_getcwd, @ptrToInt(buf), size);
-}
-
-pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
- return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
-}
-
-pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize {
- return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
-}
-
-pub fn inotify_init1(flags: u32) usize {
- return syscall1(SYS_inotify_init1, flags);
-}
-
-pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize {
- return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask);
-}
-
-pub fn inotify_rm_watch(fd: i32, wd: i32) usize {
- return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd)));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
- if (@hasDecl(@This(), "SYS_readlink")) {
- return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
- } else {
- return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
- }
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
- return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn mkdir(path: [*]const u8, mode: u32) usize {
- if (@hasDecl(@This(), "SYS_mkdir")) {
- return syscall2(SYS_mkdir, @ptrToInt(path), mode);
- } else {
- return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode);
- }
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize {
- return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize {
- return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn umount(special: [*]const u8) usize {
- return syscall2(SYS_umount2, @ptrToInt(special), 0);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn umount2(special: [*]const u8, flags: u32) usize {
- return syscall2(SYS_umount2, @ptrToInt(special), flags);
-}
-
-pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
- return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
-}
-
-pub fn mprotect(address: usize, length: usize, protection: usize) usize {
- return syscall3(SYS_mprotect, address, length, protection);
-}
-
-pub fn munmap(address: usize, length: usize) usize {
- return syscall2(SYS_munmap, address, length);
-}
-
-pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
- return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
-}
-
-pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize {
- return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
-}
-
-pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize {
- return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count);
-}
-
-pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize {
- return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count);
-}
-
-pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize {
- return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn rmdir(path: [*]const u8) usize {
- if (@hasDecl(@This(), "SYS_rmdir")) {
- return syscall1(SYS_rmdir, @ptrToInt(path));
- } else {
- return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR);
- }
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
- if (@hasDecl(@This(), "SYS_symlink")) {
- return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new));
- } else {
- return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new));
- }
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
- return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
- return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn access(path: [*]const u8, mode: u32) usize {
- return syscall2(SYS_access, @ptrToInt(path), mode);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize {
- return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode);
-}
-
-pub fn pipe(fd: *[2]i32) usize {
- if (@hasDecl(@This(), "SYS_pipe")) {
- return syscall1(SYS_pipe, @ptrToInt(fd));
- } else {
- return syscall2(SYS_pipe2, @ptrToInt(fd), 0);
- }
-}
-
-pub fn pipe2(fd: *[2]i32, flags: u32) usize {
- return syscall2(SYS_pipe2, @ptrToInt(fd), flags);
-}
-
-pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
- return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
-}
-
-pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
- return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn rename(old: [*]const u8, new: [*]const u8) usize {
- if (@hasDecl(@This(), "SYS_rename")) {
- return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new));
- } else if (@hasDecl(@This(), "SYS_renameat")) {
- return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new));
- } else {
- return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0);
- }
-}
-
-pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
- if (@hasDecl(@This(), "SYS_renameat")) {
- return syscall4(
- SYS_renameat,
- @bitCast(usize, isize(oldfd)),
- @ptrToInt(old),
- @bitCast(usize, isize(newfd)),
- @ptrToInt(new),
- );
- } else {
- return syscall5(
- SYS_renameat2,
- @bitCast(usize, isize(oldfd)),
- @ptrToInt(old),
- @bitCast(usize, isize(newfd)),
- @ptrToInt(new),
- 0,
- );
- }
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize {
- return syscall5(
- SYS_renameat2,
- @bitCast(usize, isize(oldfd)),
- @ptrToInt(oldpath),
- @bitCast(usize, isize(newfd)),
- @ptrToInt(newpath),
- flags,
- );
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn open(path: [*]const u8, flags: u32, perm: usize) usize {
- return syscall3(SYS_open, @ptrToInt(path), flags, perm);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn create(path: [*]const u8, perm: usize) usize {
- return syscall2(SYS_creat, @ptrToInt(path), perm);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize {
- // dirfd could be negative, for example AT_FDCWD is -100
- return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode);
-}
-
-/// See also `clone` (from the arch-specific include)
-pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize {
- return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls);
-}
-
-/// See also `clone` (from the arch-specific include)
-pub fn clone2(flags: u32, child_stack_ptr: usize) usize {
- return syscall2(SYS_clone, flags, child_stack_ptr);
-}
-
-pub fn close(fd: i32) usize {
- return syscall1(SYS_close, @bitCast(usize, isize(fd)));
-}
-
-/// Can only be called on 32 bit systems. For 64 bit see `lseek`.
-pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize {
- return syscall5(
- SYS__llseek,
- @bitCast(usize, isize(fd)),
- @truncate(usize, offset >> 32),
- @truncate(usize, offset),
- @ptrToInt(result),
- whence,
- );
-}
-
-/// Can only be called on 64 bit systems. For 32 bit see `llseek`.
-pub fn lseek(fd: i32, offset: i64, whence: usize) usize {
- return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence);
-}
-
-pub fn exit(status: i32) noreturn {
- _ = syscall1(SYS_exit, @bitCast(usize, isize(status)));
- unreachable;
-}
-
-pub fn exit_group(status: i32) noreturn {
- _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status)));
- unreachable;
-}
-
-pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
- return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags);
-}
-
-pub fn kill(pid: i32, sig: i32) usize {
- return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig)));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn unlink(path: [*]const u8) usize {
- if (@hasDecl(@This(), "SYS_unlink")) {
- return syscall1(SYS_unlink, @ptrToInt(path));
- } else {
- return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0);
- }
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize {
- return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags);
-}
-
-pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
- return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
-}
-
-var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime);
-
-// We must follow the C calling convention when we call into the VDSO
-const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize;
-
-pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
- if (VDSO_CGT_SYM.len != 0) {
- const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered);
- if (ptr) |fn_ptr| {
- const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
- const rc = f(clk_id, tp);
- switch (rc) {
- 0, @bitCast(usize, isize(-EINVAL)) => return rc,
- else => {},
- }
- }
- }
- return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
-}
-
-extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize {
- const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM));
- // Note that we may not have a VDSO at all, update the stub address anyway
- // so that clock_gettime will fall back on the good old (and slow) syscall
- _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic);
- // Call into the VDSO if available
- if (ptr) |fn_ptr| {
- const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
- return f(clk, ts);
- }
- return @bitCast(usize, isize(-ENOSYS));
-}
-
-pub fn clock_getres(clk_id: i32, tp: *timespec) usize {
- return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
-}
-
-pub fn clock_settime(clk_id: i32, tp: *const timespec) usize {
- return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
-}
-
-pub fn gettimeofday(tv: *timeval, tz: *timezone) usize {
- return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz));
-}
-
-pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize {
- return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz));
-}
-
-pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
- return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));
-}
-
-pub fn setuid(uid: u32) usize {
- return syscall1(SYS_setuid, uid);
-}
-
-pub fn setgid(gid: u32) usize {
- return syscall1(SYS_setgid, gid);
-}
-
-pub fn setreuid(ruid: u32, euid: u32) usize {
- return syscall2(SYS_setreuid, ruid, euid);
-}
-
-pub fn setregid(rgid: u32, egid: u32) usize {
- return syscall2(SYS_setregid, rgid, egid);
-}
-
-pub fn getuid() u32 {
- return u32(syscall0(SYS_getuid));
-}
-
-pub fn getgid() u32 {
- return u32(syscall0(SYS_getgid));
-}
-
-pub fn geteuid() u32 {
- return u32(syscall0(SYS_geteuid));
-}
-
-pub fn getegid() u32 {
- return u32(syscall0(SYS_getegid));
-}
-
-pub fn seteuid(euid: u32) usize {
- return syscall1(SYS_seteuid, euid);
-}
-
-pub fn setegid(egid: u32) usize {
- return syscall1(SYS_setegid, egid);
-}
-
-pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize {
- return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid));
-}
-
-pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize {
- return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid));
-}
-
-pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize {
- return syscall3(SYS_setresuid, ruid, euid, suid);
-}
-
-pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize {
- return syscall3(SYS_setresgid, rgid, egid, sgid);
-}
-
-pub fn getgroups(size: usize, list: *u32) usize {
- return syscall2(SYS_getgroups, size, @ptrToInt(list));
-}
-
-pub fn setgroups(size: usize, list: *const u32) usize {
- return syscall2(SYS_setgroups, size, @ptrToInt(list));
-}
-
-pub fn getpid() i32 {
- return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));
-}
-
-pub fn gettid() i32 {
- return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid)));
-}
-
-pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
- return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
-}
-
-pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize {
- assert(sig >= 1);
- assert(sig != SIGKILL);
- assert(sig != SIGSTOP);
- var ksa = k_sigaction{
- .handler = act.handler,
- .flags = act.flags | SA_RESTORER,
- .mask = undefined,
- .restorer = @ptrCast(extern fn () void, restore_rt),
- };
- var ksa_old: k_sigaction = undefined;
- @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8);
- const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask)));
- const err = getErrno(result);
- if (err != 0) {
- return result;
- }
- if (oact) |old| {
- old.handler = ksa_old.handler;
- old.flags = @truncate(u32, ksa_old.flags);
- @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask)));
- }
- return 0;
-}
-
-fn blockAllSignals(set: *sigset_t) void {
- _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8);
-}
-
-fn blockAppSignals(set: *sigset_t) void {
- _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8);
-}
-
-fn restoreSignals(set: *sigset_t) void {
- _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8);
-}
-
-pub fn sigaddset(set: *sigset_t, sig: u6) void {
- const s = sig - 1;
- (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1));
-}
-
-pub fn sigismember(set: *const sigset_t, sig: u6) bool {
- const s = sig - 1;
- return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0;
-}
-
-pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
- return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len));
-}
-
-pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
- return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len));
-}
-
-pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
- return syscall3(SYS_socket, domain, socket_type, protocol);
-}
-
-pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize {
- return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen));
-}
-
-pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize {
- return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
-}
-
-pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize {
- return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags);
-}
-
-pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize {
- if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) {
- // workaround kernel brokenness:
- // if adding up all iov_len overflows a i32 then split into multiple calls
- // see https://www.openwall.com/lists/musl/2014/06/07/5
- const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel
- var next_unsent: usize = 0;
- for (msgvec[0..kvlen]) |*msg, i| {
- var size: i32 = 0;
- const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned
- for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| {
- if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) {
- // batch-send all messages up to the current message
- if (next_unsent < i) {
- const batch_size = i - next_unsent;
- const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags);
- if (getErrno(r) != 0) return next_unsent;
- if (r < batch_size) return next_unsent + r;
- }
- // send current message as own packet
- const r = sendmsg(fd, &msg.msg_hdr, flags);
- if (getErrno(r) != 0) return r;
- // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe.
- msg.msg_len = @intCast(u32, r);
- next_unsent = i + 1;
- break;
- }
- }
- }
- if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR)
- const batch_size = kvlen - next_unsent;
- const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags);
- if (getErrno(r) != 0) return r;
- return next_unsent + r;
- }
- return kvlen;
- }
- return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags);
-}
-
-pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize {
- return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len);
-}
-
-pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize {
- return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags);
-}
-
-pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize {
- return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
-}
-
-pub fn shutdown(fd: i32, how: i32) usize {
- return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how)));
-}
-
-pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
- return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len));
-}
-
-pub fn listen(fd: i32, backlog: u32) usize {
- return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog);
-}
-
-pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize {
- return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen));
-}
-
-pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
- return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0]));
-}
-
-pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
- return accept4(fd, addr, len, 0);
-}
-
-pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize {
- return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags);
-}
-
-pub fn fstat(fd: i32, stat_buf: *Stat) usize {
- return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize {
- return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize {
- return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize {
- return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize {
- return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize {
- return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size);
-}
-
-pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize {
- return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize {
- return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize {
- return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize {
- return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize {
- return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize {
- return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize {
- return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags);
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn removexattr(path: [*]const u8, name: [*]const u8) usize {
- return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize {
- return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name));
-}
-
-// TODO https://github.com/ziglang/zig/issues/265
-pub fn fremovexattr(fd: usize, name: [*]const u8) usize {
- return syscall2(SYS_fremovexattr, fd, @ptrToInt(name));
-}
-
-pub fn sched_getaffinity(pid: i32, set: []usize) usize {
- return syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), set.len * @sizeOf(usize), @ptrToInt(set.ptr));
-}
-
-pub fn epoll_create() usize {
- return epoll_create1(0);
-}
-
-pub fn epoll_create1(flags: usize) usize {
- return syscall1(SYS_epoll_create1, flags);
-}
-
-pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
- return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev));
-}
-
-pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
- return syscall4(
- SYS_epoll_wait,
- @bitCast(usize, isize(epoll_fd)),
- @ptrToInt(events),
- maxevents,
- @bitCast(usize, isize(timeout)),
- );
-}
-
-pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize {
- return syscall6(
- SYS_epoll_pwait,
- @bitCast(usize, isize(epoll_fd)),
- @ptrToInt(events),
- @intCast(usize, maxevents),
- @bitCast(usize, isize(timeout)),
- @ptrToInt(sigmask),
- @sizeOf(sigset_t),
- );
-}
-
-pub fn eventfd(count: u32, flags: u32) usize {
- return syscall2(SYS_eventfd2, count, flags);
-}
-
-pub fn timerfd_create(clockid: i32, flags: u32) usize {
- return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags);
-}
-
-pub const itimerspec = extern struct {
- it_interval: timespec,
- it_value: timespec,
-};
-
-pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
- return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value));
-}
-
-pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
- return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value));
-}
-
-pub fn unshare(flags: usize) usize {
- return syscall1(SYS_unshare, flags);
-}
-
-pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {
- return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap));
-}
-
-pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize {
- return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap));
-}
-
-// XXX: This should be weak
-extern const __ehdr_start: elf.Ehdr = undefined;
-
-pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize {
- if (builtin.link_libc) {
- return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data));
- }
-
- const elf_base = @ptrToInt(&__ehdr_start);
- const n_phdr = __ehdr_start.e_phnum;
- const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr];
-
- var it = dl.linkmap_iterator(phdrs) catch return 0;
-
- // The executable has no dynamic link segment, create a single entry for
- // the whole ELF image
- if (it.end()) {
- var info = dl_phdr_info{
- .dlpi_addr = elf_base,
- .dlpi_name = c"/proc/self/exe",
- .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff),
- .dlpi_phnum = __ehdr_start.e_phnum,
- };
-
- return callback(&info, @sizeOf(dl_phdr_info), data);
- }
-
- // Last return value from the callback function
- var last_r: isize = 0;
- while (it.next()) |entry| {
- var dlpi_phdr: usize = undefined;
- var dlpi_phnum: u16 = undefined;
-
- if (entry.l_addr != 0) {
- const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr);
- dlpi_phdr = entry.l_addr + elf_header.e_phoff;
- dlpi_phnum = elf_header.e_phnum;
- } else {
- // This is the running ELF image
- dlpi_phdr = elf_base + __ehdr_start.e_phoff;
- dlpi_phnum = __ehdr_start.e_phnum;
- }
-
- var info = dl_phdr_info{
- .dlpi_addr = entry.l_addr,
- .dlpi_name = entry.l_name,
- .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr),
- .dlpi_phnum = dlpi_phnum,
- };
-
- last_r = callback(&info, @sizeOf(dl_phdr_info), data);
- if (last_r != 0) break;
- }
-
- return last_r;
-}
std/os/linux/tls.zig
@@ -1,6 +1,6 @@
const std = @import("std");
+const os = std.os;
const mem = std.mem;
-const posix = std.os.posix;
const elf = std.elf;
const builtin = @import("builtin");
const assert = std.debug.assert;
@@ -237,9 +237,14 @@ pub fn allocateTLS(size: usize) usize {
return @ptrToInt(&main_thread_tls_buffer);
}
- const addr = posix.mmap(null, size, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS, -1, 0);
-
- if (posix.getErrno(addr) != 0) @panic("out of memory");
+ const addr = os.mmap(
+ null,
+ size,
+ os.PROT_READ | os.PROT_WRITE,
+ os.MAP_PRIVATE | os.MAP_ANONYMOUS,
+ -1,
+ 0,
+ ) catch @panic("out of memory");
return addr;
}
std/os/linux/x86_64.zig
@@ -1,4 +1,4 @@
-use @import("posix/x86_64.zig");
+use @import("../bits.zig");
pub fn syscall0(number: usize) usize {
return asm volatile ("syscall"
std/os/linux.zig
@@ -1,8 +1,878 @@
+// This file provides the system interface functions for Linux matching those
+// that are provided by libc, whether or not libc is linked. The following
+// abstractions are made:
+// * Work around kernel bugs and limitations. For example, see sendmmsg.
+// * Implement all the syscalls in the same way that libc functions will
+// provide `rename` when only the `renameat` syscall exists.
+// * Does not support POSIX thread cancellation.
const std = @import("../std.zig");
const builtin = @import("builtin");
+const assert = std.debug.assert;
+const maxInt = std.math.maxInt;
+const elf = std.elf;
+const vdso = @import("linux/vdso.zig");
+const dl = @import("../dynamic_library.zig");
+
pub const is_the_target = builtin.os == .linux;
-pub const sys = @import("linux/sys.zig");
-pub use if (builtin.link_libc) std.c else sys;
+pub use switch (builtin.arch) {
+ .x86_64 => @import("linux/x86_64.zig"),
+ .aarch64 => @import("linux/arm64.zig"),
+ else => struct {},
+};
+pub use @import("bits.zig");
+
+/// Set by startup code, used by `getauxval`.
+pub var elf_aux_maybe: ?[*]std.elf.Auxv = null;
+
+/// See `std.elf` for the constants.
+pub fn getauxval(index: usize) usize {
+ const auxv = elf_aux_maybe orelse return 0;
+ var i: usize = 0;
+ while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
+ if (auxv[i].a_type == index)
+ return auxv[i].a_un.a_val;
+ }
+ return 0;
+}
+
+/// Get the errno from a syscall return value, or 0 for no error.
+pub fn getErrno(r: usize) u12 {
+ const signed_r = @bitCast(isize, r);
+ return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0;
+}
+
+pub fn dup2(old: i32, new: i32) usize {
+ if (@hasDecl(@This(), "SYS_dup2")) {
+ return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)));
+ } else {
+ if (old == new) {
+ if (std.debug.runtime_safety) {
+ const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD);
+ if (@bitCast(isize, rc) < 0) return rc;
+ }
+ return @intCast(usize, old);
+ } else {
+ return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0);
+ }
+ }
+}
+
+pub fn dup3(old: i32, new: i32, flags: u32) usize {
+ return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn chdir(path: [*]const u8) usize {
+ return syscall1(SYS_chdir, @ptrToInt(path));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn chroot(path: [*]const u8) usize {
+ return syscall1(SYS_chroot, @ptrToInt(path));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize {
+ return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp));
+}
+
+pub fn fork() usize {
+ if (@hasDecl(@This(), "SYS_fork")) {
+ return syscall0(SYS_fork);
+ } else {
+ return syscall2(SYS_clone, SIGCHLD, 0);
+ }
+}
+
+/// This must be inline, and inline call the syscall function, because if the
+/// child does a return it will clobber the parent's stack.
+/// It is advised to avoid this function and use clone instead, because
+/// the compiler is not aware of how vfork affects control flow and you may
+/// see different results in optimized builds.
+pub inline fn vfork() usize {
+ return @inlineCall(syscall0, SYS_vfork);
+}
+
+pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize {
+ return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout));
+}
+
+pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize {
+ return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val));
+}
+
+pub fn getcwd(buf: [*]u8, size: usize) usize {
+ return syscall2(SYS_getcwd, @ptrToInt(buf), size);
+}
+
+pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize {
+ return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
+}
+
+pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize {
+ return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count);
+}
+
+pub fn inotify_init1(flags: u32) usize {
+ return syscall1(SYS_inotify_init1, flags);
+}
+
+pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize {
+ return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask);
+}
+
+pub fn inotify_rm_watch(fd: i32, wd: i32) usize {
+ return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd)));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
+ if (@hasDecl(@This(), "SYS_readlink")) {
+ return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
+ } else {
+ return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
+ }
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize {
+ return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn mkdir(path: [*]const u8, mode: u32) usize {
+ if (@hasDecl(@This(), "SYS_mkdir")) {
+ return syscall2(SYS_mkdir, @ptrToInt(path), mode);
+ } else {
+ return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode);
+ }
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize {
+ return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize {
+ return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn umount(special: [*]const u8) usize {
+ return syscall2(SYS_umount2, @ptrToInt(special), 0);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn umount2(special: [*]const u8, flags: u32) usize {
+ return syscall2(SYS_umount2, @ptrToInt(special), flags);
+}
+
+pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize {
+ return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
+}
+
+pub fn mprotect(address: usize, length: usize, protection: usize) usize {
+ return syscall3(SYS_mprotect, address, length, protection);
+}
+
+pub fn munmap(address: usize, length: usize) usize {
+ return syscall2(SYS_munmap, address, length);
+}
+
+pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
+ return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
+}
+
+pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize {
+ return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
+}
+
+pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize {
+ return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count);
+}
+
+pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize {
+ return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count);
+}
+
+pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize {
+ return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn rmdir(path: [*]const u8) usize {
+ if (@hasDecl(@This(), "SYS_rmdir")) {
+ return syscall1(SYS_rmdir, @ptrToInt(path));
+ } else {
+ return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR);
+ }
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn symlink(existing: [*]const u8, new: [*]const u8) usize {
+ if (@hasDecl(@This(), "SYS_symlink")) {
+ return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new));
+ } else {
+ return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new));
+ }
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
+ return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize {
+ return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn access(path: [*]const u8, mode: u32) usize {
+ return syscall2(SYS_access, @ptrToInt(path), mode);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize {
+ return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode);
+}
+
+pub fn pipe(fd: *[2]i32) usize {
+ if (@hasDecl(@This(), "SYS_pipe")) {
+ return syscall1(SYS_pipe, @ptrToInt(fd));
+ } else {
+ return syscall2(SYS_pipe2, @ptrToInt(fd), 0);
+ }
+}
+
+pub fn pipe2(fd: *[2]i32, flags: u32) usize {
+ return syscall2(SYS_pipe2, @ptrToInt(fd), flags);
+}
+
+pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
+ return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count);
+}
+
+pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize {
+ return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn rename(old: [*]const u8, new: [*]const u8) usize {
+ if (@hasDecl(@This(), "SYS_rename")) {
+ return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new));
+ } else if (@hasDecl(@This(), "SYS_renameat")) {
+ return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new));
+ } else {
+ return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0);
+ }
+}
+
+pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize {
+ if (@hasDecl(@This(), "SYS_renameat")) {
+ return syscall4(
+ SYS_renameat,
+ @bitCast(usize, isize(oldfd)),
+ @ptrToInt(old),
+ @bitCast(usize, isize(newfd)),
+ @ptrToInt(new),
+ );
+ } else {
+ return syscall5(
+ SYS_renameat2,
+ @bitCast(usize, isize(oldfd)),
+ @ptrToInt(old),
+ @bitCast(usize, isize(newfd)),
+ @ptrToInt(new),
+ 0,
+ );
+ }
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize {
+ return syscall5(
+ SYS_renameat2,
+ @bitCast(usize, isize(oldfd)),
+ @ptrToInt(oldpath),
+ @bitCast(usize, isize(newfd)),
+ @ptrToInt(newpath),
+ flags,
+ );
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn open(path: [*]const u8, flags: u32, perm: usize) usize {
+ return syscall3(SYS_open, @ptrToInt(path), flags, perm);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn create(path: [*]const u8, perm: usize) usize {
+ return syscall2(SYS_creat, @ptrToInt(path), perm);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize {
+ // dirfd could be negative, for example AT_FDCWD is -100
+ return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode);
+}
+
+/// See also `clone` (from the arch-specific include)
+pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize {
+ return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls);
+}
+
+/// See also `clone` (from the arch-specific include)
+pub fn clone2(flags: u32, child_stack_ptr: usize) usize {
+ return syscall2(SYS_clone, flags, child_stack_ptr);
+}
+
+pub fn close(fd: i32) usize {
+ return syscall1(SYS_close, @bitCast(usize, isize(fd)));
+}
+
+/// Can only be called on 32 bit systems. For 64 bit see `lseek`.
+pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize {
+ return syscall5(
+ SYS__llseek,
+ @bitCast(usize, isize(fd)),
+ @truncate(usize, offset >> 32),
+ @truncate(usize, offset),
+ @ptrToInt(result),
+ whence,
+ );
+}
+
+/// Can only be called on 64 bit systems. For 32 bit see `llseek`.
+pub fn lseek(fd: i32, offset: i64, whence: usize) usize {
+ return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence);
+}
+
+pub fn exit(status: i32) noreturn {
+ _ = syscall1(SYS_exit, @bitCast(usize, isize(status)));
+ unreachable;
+}
+
+pub fn exit_group(status: i32) noreturn {
+ _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status)));
+ unreachable;
+}
+
+pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize {
+ return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags);
+}
+
+pub fn kill(pid: i32, sig: i32) usize {
+ return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig)));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn unlink(path: [*]const u8) usize {
+ if (@hasDecl(@This(), "SYS_unlink")) {
+ return syscall1(SYS_unlink, @ptrToInt(path));
+ } else {
+ return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0);
+ }
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize {
+ return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags);
+}
+
+pub fn waitpid(pid: i32, status: *i32, options: i32) usize {
+ return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
+}
+
+var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime);
+
+// We must follow the C calling convention when we call into the VDSO
+const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize;
+
+pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
+ if (VDSO_CGT_SYM.len != 0) {
+ const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered);
+ if (ptr) |fn_ptr| {
+ const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
+ const rc = f(clk_id, tp);
+ switch (rc) {
+ 0, @bitCast(usize, isize(-EINVAL)) => return rc,
+ else => {},
+ }
+ }
+ }
+ return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
+}
+
+extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize {
+ const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM));
+ // Note that we may not have a VDSO at all, update the stub address anyway
+ // so that clock_gettime will fall back on the good old (and slow) syscall
+ _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic);
+ // Call into the VDSO if available
+ if (ptr) |fn_ptr| {
+ const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
+ return f(clk, ts);
+ }
+ return @bitCast(usize, isize(-ENOSYS));
+}
+
+pub fn clock_getres(clk_id: i32, tp: *timespec) usize {
+ return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
+}
+
+pub fn clock_settime(clk_id: i32, tp: *const timespec) usize {
+ return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
+}
+
+pub fn gettimeofday(tv: *timeval, tz: *timezone) usize {
+ return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz));
+}
+
+pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize {
+ return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz));
+}
+
+pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize {
+ return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));
+}
+
+pub fn setuid(uid: u32) usize {
+ return syscall1(SYS_setuid, uid);
+}
+
+pub fn setgid(gid: u32) usize {
+ return syscall1(SYS_setgid, gid);
+}
+
+pub fn setreuid(ruid: u32, euid: u32) usize {
+ return syscall2(SYS_setreuid, ruid, euid);
+}
+
+pub fn setregid(rgid: u32, egid: u32) usize {
+ return syscall2(SYS_setregid, rgid, egid);
+}
+
+pub fn getuid() u32 {
+ return u32(syscall0(SYS_getuid));
+}
+
+pub fn getgid() u32 {
+ return u32(syscall0(SYS_getgid));
+}
+
+pub fn geteuid() u32 {
+ return u32(syscall0(SYS_geteuid));
+}
+
+pub fn getegid() u32 {
+ return u32(syscall0(SYS_getegid));
+}
+
+pub fn seteuid(euid: u32) usize {
+ return syscall1(SYS_seteuid, euid);
+}
+
+pub fn setegid(egid: u32) usize {
+ return syscall1(SYS_setegid, egid);
+}
+
+pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize {
+ return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid));
+}
+
+pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize {
+ return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid));
+}
+
+pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize {
+ return syscall3(SYS_setresuid, ruid, euid, suid);
+}
+
+pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize {
+ return syscall3(SYS_setresgid, rgid, egid, sgid);
+}
+
+pub fn getgroups(size: usize, list: *u32) usize {
+ return syscall2(SYS_getgroups, size, @ptrToInt(list));
+}
+
+pub fn setgroups(size: usize, list: *const u32) usize {
+ return syscall2(SYS_setgroups, size, @ptrToInt(list));
+}
+
+pub fn getpid() i32 {
+ return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));
+}
+
+pub fn gettid() i32 {
+ return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid)));
+}
+
+pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
+ return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
+}
+
+pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize {
+ assert(sig >= 1);
+ assert(sig != SIGKILL);
+ assert(sig != SIGSTOP);
+ var ksa = k_sigaction{
+ .handler = act.handler,
+ .flags = act.flags | SA_RESTORER,
+ .mask = undefined,
+ .restorer = @ptrCast(extern fn () void, restore_rt),
+ };
+ var ksa_old: k_sigaction = undefined;
+ @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8);
+ const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask)));
+ const err = getErrno(result);
+ if (err != 0) {
+ return result;
+ }
+ if (oact) |old| {
+ old.handler = ksa_old.handler;
+ old.flags = @truncate(u32, ksa_old.flags);
+ @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask)));
+ }
+ return 0;
+}
+
+fn blockAllSignals(set: *sigset_t) void {
+ _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8);
+}
+
+fn blockAppSignals(set: *sigset_t) void {
+ _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8);
+}
+
+fn restoreSignals(set: *sigset_t) void {
+ _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8);
+}
+
+pub fn sigaddset(set: *sigset_t, sig: u6) void {
+ const s = sig - 1;
+ (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1));
+}
+
+pub fn sigismember(set: *const sigset_t, sig: u6) bool {
+ const s = sig - 1;
+ return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0;
+}
+
+pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
+ return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len));
+}
+
+pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
+ return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len));
+}
+
+pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize {
+ return syscall3(SYS_socket, domain, socket_type, protocol);
+}
+
+pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize {
+ return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen));
+}
+
+pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize {
+ return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen));
+}
+
+pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize {
+ return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags);
+}
+
+pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize {
+ if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) {
+ // workaround kernel brokenness:
+ // if adding up all iov_len overflows a i32 then split into multiple calls
+ // see https://www.openwall.com/lists/musl/2014/06/07/5
+ const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel
+ var next_unsent: usize = 0;
+ for (msgvec[0..kvlen]) |*msg, i| {
+ var size: i32 = 0;
+ const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned
+ for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| {
+ if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) {
+ // batch-send all messages up to the current message
+ if (next_unsent < i) {
+ const batch_size = i - next_unsent;
+ const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags);
+ if (getErrno(r) != 0) return next_unsent;
+ if (r < batch_size) return next_unsent + r;
+ }
+ // send current message as own packet
+ const r = sendmsg(fd, &msg.msg_hdr, flags);
+ if (getErrno(r) != 0) return r;
+ // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe.
+ msg.msg_len = @intCast(u32, r);
+ next_unsent = i + 1;
+ break;
+ }
+ }
+ }
+ if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR)
+ const batch_size = kvlen - next_unsent;
+ const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags);
+ if (getErrno(r) != 0) return r;
+ return next_unsent + r;
+ }
+ return kvlen;
+ }
+ return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags);
+}
+
+pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize {
+ return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len);
+}
+
+pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize {
+ return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags);
+}
+
+pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize {
+ return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen));
+}
+
+pub fn shutdown(fd: i32, how: i32) usize {
+ return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how)));
+}
+
+pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize {
+ return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len));
+}
+
+pub fn listen(fd: i32, backlog: u32) usize {
+ return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog);
+}
+
+pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize {
+ return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen));
+}
+
+pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize {
+ return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0]));
+}
+
+pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize {
+ return accept4(fd, addr, len, 0);
+}
+
+pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize {
+ return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags);
+}
+
+pub fn fstat(fd: i32, stat_buf: *Stat) usize {
+ return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize {
+ return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize {
+ return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize {
+ return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize {
+ return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize {
+ return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size);
+}
+
+pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize {
+ return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize {
+ return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize {
+ return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize {
+ return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize {
+ return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize {
+ return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize {
+ return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags);
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn removexattr(path: [*]const u8, name: [*]const u8) usize {
+ return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize {
+ return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name));
+}
+
+// TODO https://github.com/ziglang/zig/issues/265
+pub fn fremovexattr(fd: usize, name: [*]const u8) usize {
+ return syscall2(SYS_fremovexattr, fd, @ptrToInt(name));
+}
+
+pub fn sched_getaffinity(pid: i32, size: usize, set: *cpu_set_t) usize {
+ const rc = syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), size, @ptrToInt(set));
+ if (@bitCast(isize, rc) < 0) return rc;
+ if (rc < size) @memset(@ptrCast([*]u8, set) + rc, 0, size - rc);
+ return 0;
+}
+
+pub fn epoll_create() usize {
+ return epoll_create1(0);
+}
+
+pub fn epoll_create1(flags: usize) usize {
+ return syscall1(SYS_epoll_create1, flags);
+}
+
+pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize {
+ return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev));
+}
+
+pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize {
+ return syscall4(
+ SYS_epoll_wait,
+ @bitCast(usize, isize(epoll_fd)),
+ @ptrToInt(events),
+ maxevents,
+ @bitCast(usize, isize(timeout)),
+ );
+}
+
+pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize {
+ return syscall6(
+ SYS_epoll_pwait,
+ @bitCast(usize, isize(epoll_fd)),
+ @ptrToInt(events),
+ @intCast(usize, maxevents),
+ @bitCast(usize, isize(timeout)),
+ @ptrToInt(sigmask),
+ @sizeOf(sigset_t),
+ );
+}
+
+pub fn eventfd(count: u32, flags: u32) usize {
+ return syscall2(SYS_eventfd2, count, flags);
+}
+
+pub fn timerfd_create(clockid: i32, flags: u32) usize {
+ return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags);
+}
+
+pub const itimerspec = extern struct {
+ it_interval: timespec,
+ it_value: timespec,
+};
+
+pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize {
+ return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value));
+}
+
+pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize {
+ return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value));
+}
+
+pub fn unshare(flags: usize) usize {
+ return syscall1(SYS_unshare, flags);
+}
+
+pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize {
+ return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap));
+}
+
+pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize {
+ return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap));
+}
+
+// XXX: This should be weak
+extern const __ehdr_start: elf.Ehdr = undefined;
+
+pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize {
+ if (builtin.link_libc) {
+ return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data));
+ }
+
+ const elf_base = @ptrToInt(&__ehdr_start);
+ const n_phdr = __ehdr_start.e_phnum;
+ const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr];
+
+ var it = dl.linkmap_iterator(phdrs) catch return 0;
+
+ // The executable has no dynamic link segment, create a single entry for
+ // the whole ELF image
+ if (it.end()) {
+ var info = dl_phdr_info{
+ .dlpi_addr = elf_base,
+ .dlpi_name = c"/proc/self/exe",
+ .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff),
+ .dlpi_phnum = __ehdr_start.e_phnum,
+ };
+
+ return callback(&info, @sizeOf(dl_phdr_info), data);
+ }
+
+ // Last return value from the callback function
+ var last_r: isize = 0;
+ while (it.next()) |entry| {
+ var dlpi_phdr: usize = undefined;
+ var dlpi_phnum: u16 = undefined;
+
+ if (entry.l_addr != 0) {
+ const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr);
+ dlpi_phdr = entry.l_addr + elf_header.e_phoff;
+ dlpi_phnum = elf_header.e_phnum;
+ } else {
+ // This is the running ELF image
+ dlpi_phdr = elf_base + __ehdr_start.e_phoff;
+ dlpi_phnum = __ehdr_start.e_phnum;
+ }
+
+ var info = dl_phdr_info{
+ .dlpi_addr = entry.l_addr,
+ .dlpi_name = entry.l_name,
+ .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr),
+ .dlpi_phnum = dlpi_phnum,
+ };
+
+ last_r = callback(&info, @sizeOf(dl_phdr_info), data);
+ if (last_r != 0) break;
+ }
+
+ return last_r;
+}
test "" {
if (is_the_target) {
std/os/test.zig
@@ -5,6 +5,7 @@ const expect = std.testing.expect;
const io = std.io;
const mem = std.mem;
const File = std.fs.File;
+const Thread = std.Thread;
const a = std.debug.global_allocator;
@@ -37,25 +38,25 @@ test "access file" {
try os.deleteTree(a, "os_test_tmp");
}
-fn testThreadIdFn(thread_id: *os.Thread.Id) void {
- thread_id.* = os.Thread.getCurrentId();
+fn testThreadIdFn(thread_id: *Thread.Id) void {
+ thread_id.* = Thread.getCurrentId();
}
-test "std.os.Thread.getCurrentId" {
+test "std.Thread.getCurrentId" {
if (builtin.single_threaded) return error.SkipZigTest;
- var thread_current_id: os.Thread.Id = undefined;
- const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
+ var thread_current_id: Thread.Id = undefined;
+ const thread = try Thread.spawn(&thread_current_id, testThreadIdFn);
const thread_id = thread.handle();
thread.wait();
- if (os.Thread.use_pthreads) {
+ if (Thread.use_pthreads) {
expect(thread_current_id == thread_id);
} else {
switch (builtin.os) {
- builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id),
+ builtin.Os.windows => expect(Thread.getCurrentId() != thread_current_id),
else => {
// If the thread completes very quickly, then thread_id can be 0. See the
- // documentation comments for `std.os.Thread.handle`.
+ // documentation comments for `std.Thread.handle`.
expect(thread_id == 0 or thread_current_id == thread_id);
},
}
@@ -67,10 +68,10 @@ test "spawn threads" {
var shared_ctx: i32 = 1;
- const thread1 = try std.os.spawnThread({}, start1);
- const thread2 = try std.os.spawnThread(&shared_ctx, start2);
- const thread3 = try std.os.spawnThread(&shared_ctx, start2);
- const thread4 = try std.os.spawnThread(&shared_ctx, start2);
+ const thread1 = try Thread.spawn({}, start1);
+ const thread2 = try Thread.spawn(&shared_ctx, start2);
+ const thread3 = try Thread.spawn(&shared_ctx, start2);
+ const thread4 = try Thread.spawn(&shared_ctx, start2);
thread1.wait();
thread2.wait();
@@ -116,8 +117,8 @@ test "AtomicFile" {
test "thread local storage" {
if (builtin.single_threaded) return error.SkipZigTest;
- const thread1 = try std.os.spawnThread({}, testTls);
- const thread2 = try std.os.spawnThread({}, testTls);
+ const thread1 = try Thread.spawn({}, testTls);
+ const thread2 = try Thread.spawn({}, testTls);
testTls({});
thread1.wait();
thread2.wait();
std/os/windows.zig
@@ -591,7 +591,7 @@ pub fn CreateFileW(
ERROR.ACCESS_DENIED => return error.AccessDenied,
ERROR.PIPE_BUSY => return error.PipeBusy,
ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong,
- else => |err| return unexpectedErrorWindows(err),
+ else => |err| return unexpectedError(err),
}
}
@@ -1089,6 +1089,20 @@ pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError
}
}
+pub const VirtualAllocError = error{Unexpected};
+
+pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID {
+ return kernel32.VirtualAlloc(addr, size, alloc_type, flProtect) orelse {
+ switch (kernel32.GetLastError()) {
+ else => |err| return unexpectedError(err),
+ }
+ };
+}
+
+pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void {
+ assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0);
+}
+
pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 {
return sliceToPrefixedFileW(mem.toSliceConst(u8, s));
}
std/special/bootstrap.zig
@@ -92,14 +92,14 @@ fn posixCallMainAndExit() noreturn {
}
}
- std.os.posix.exit(callMainWithArgs(argc, argv, envp));
+ std.os.exit(callMainWithArgs(argc, argv, envp));
}
// This is marked inline because for some reason LLVM in release mode fails to inline it,
// and we want fewer call frames in stack traces.
inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
std.os.ArgIteratorPosix.raw = argv[0..argc];
- std.os.posix.environ = envp;
+ std.os.environ = envp;
return callMain();
}
std/child_process.zig
@@ -351,22 +351,22 @@ pub const ChildProcess = struct {
const err_pipe = try os.pipe();
errdefer destroyPipe(err_pipe);
- const pid_result = try posix.fork();
+ const pid_result = try os.fork();
if (pid_result == 0) {
// we are the child
- setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
- setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
- setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stdin_behavior, stdin_pipe[0], os.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stdout_behavior, stdout_pipe[1], os.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stderr_behavior, stderr_pipe[1], os.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
- if (self.stdin_behavior == StdIo.Pipe) {
+ if (self.stdin_behavior == .Pipe) {
os.close(stdin_pipe[0]);
os.close(stdin_pipe[1]);
}
- if (self.stdout_behavior == StdIo.Pipe) {
+ if (self.stdout_behavior == .Pipe) {
os.close(stdout_pipe[0]);
os.close(stdout_pipe[1]);
}
- if (self.stderr_behavior == StdIo.Pipe) {
+ if (self.stderr_behavior == .Pipe) {
os.close(stderr_pipe[0]);
os.close(stderr_pipe[1]);
}
@@ -383,7 +383,7 @@ pub const ChildProcess = struct {
os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err);
}
- os.posix.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err);
+ os.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err);
}
// we are the parent
@@ -736,7 +736,7 @@ fn destroyPipe(pipe: [2]i32) void {
// Then the child exits.
fn forkChildErrReport(fd: i32, err: ChildProcess.SpawnError) noreturn {
writeIntFd(fd, ErrInt(@errorToInt(err))) catch {};
- posix.exit(1);
+ os.exit(1);
}
const ErrInt = @IntType(false, @sizeOf(anyerror) * 8);
std/crypto.zig
@@ -33,7 +33,7 @@ pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305;
pub const X25519 = @import("crypto/x25519.zig").X25519;
const std = @import("std.zig");
-pub const randomBytes = std.posix.getrandom;
+pub const randomBytes = std.os.getrandom;
test "crypto" {
_ = @import("crypto/blake2.zig");
std/debug.zig
@@ -1012,16 +1012,15 @@ fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo {
errdefer S.self_exe_file.close();
const self_exe_mmap_len = try S.self_exe_file.getEndPos();
- const self_exe_mmap = os.posix.mmap(
+ const self_exe_mmap = try os.mmap(
null,
self_exe_mmap_len,
- os.posix.PROT_READ,
- os.posix.MAP_SHARED,
+ os.PROT_READ,
+ os.MAP_SHARED,
S.self_exe_file.handle,
0,
);
- if (self_exe_mmap == os.posix.MAP_FAILED) return error.OutOfMemory;
- errdefer assert(os.posix.munmap(self_exe_mmap, self_exe_mmap_len) == 0);
+ errdefer os.munmap(self_exe_mmap, self_exe_mmap_len);
const file_mmap_slice = @intToPtr([*]const u8, self_exe_mmap)[0..self_exe_mmap_len];
S.self_exe_mmap_seekable = io.SliceSeekableInStream.init(file_mmap_slice);
std/heap.zig
@@ -5,7 +5,6 @@ const testing = std.testing;
const mem = std.mem;
const os = std.os;
const builtin = @import("builtin");
-const Os = builtin.Os;
const c = std.c;
const maxInt = std.math.maxInt;
@@ -51,201 +50,190 @@ pub const DirectAllocator = struct {
if (n == 0)
return (([*]u8)(undefined))[0..0];
- switch (builtin.os) {
- Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
- const p = os.posix;
- const alloc_size = if (alignment <= os.page_size) n else n + alignment;
- const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0);
- if (addr == p.MAP_FAILED) return error.OutOfMemory;
- if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n];
-
- const aligned_addr = mem.alignForward(addr, alignment);
-
- // Unmap the extra bytes that were only requested in order to guarantee
- // that the range of memory we were provided had a proper alignment in
- // it somewhere. The extra bytes could be at the beginning, or end, or both.
- const unused_start_len = aligned_addr - addr;
- if (unused_start_len != 0) {
- const err = p.munmap(addr, unused_start_len);
- assert(p.getErrno(err) == 0);
- }
- const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size);
- const unused_end_len = addr + alloc_size - aligned_end_addr;
- if (unused_end_len != 0) {
- const err = p.munmap(aligned_end_addr, unused_end_len);
- assert(p.getErrno(err) == 0);
- }
+ if (os.windows.is_the_target) {
+ const w = os.windows;
+
+ // Although officially it's at least aligned to page boundary,
+ // Windows is known to reserve pages on a 64K boundary. It's
+ // even more likely that the requested alignment is <= 64K than
+ // 4K, so we're just allocating blindly and hoping for the best.
+ // see https://devblogs.microsoft.com/oldnewthing/?p=42223
+ const addr = w.VirtualAlloc(
+ null,
+ n,
+ w.MEM_COMMIT | w.MEM_RESERVE,
+ w.PAGE_READWRITE,
+ ) catch return error.OutOfMemory;
+
+ // If the allocation is sufficiently aligned, use it.
+ if (@ptrToInt(addr) & (alignment - 1) == 0) {
+ return @ptrCast([*]u8, addr)[0..n];
+ }
- return @intToPtr([*]u8, aligned_addr)[0..n];
- },
- .windows => {
- const w = os.windows;
-
- // Although officially it's at least aligned to page boundary,
- // Windows is known to reserve pages on a 64K boundary. It's
- // even more likely that the requested alignment is <= 64K than
- // 4K, so we're just allocating blindly and hoping for the best.
- // see https://devblogs.microsoft.com/oldnewthing/?p=42223
- const addr = w.VirtualAlloc(
+ // If it wasn't, actually do an explicitely aligned allocation.
+ w.VirtualFree(addr, 0, w.MEM_RELEASE);
+ const alloc_size = n + alignment;
+
+ const final_addr = while (true) {
+ // Reserve a range of memory large enough to find a sufficiently
+ // aligned address.
+ const reserved_addr = w.VirtualAlloc(
null,
+ alloc_size,
+ w.MEM_RESERVE,
+ w.PAGE_NOACCESS,
+ ) catch return error.OutOfMemory;
+ const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment);
+
+ // Release the reserved pages (not actually used).
+ w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE);
+
+ // At this point, it is possible that another thread has
+ // obtained some memory space that will cause the next
+ // VirtualAlloc call to fail. To handle this, we will retry
+ // until it succeeds.
+ return w.VirtualAlloc(
+ @intToPtr(*c_void, aligned_addr),
n,
w.MEM_COMMIT | w.MEM_RESERVE,
w.PAGE_READWRITE,
- ) orelse return error.OutOfMemory;
+ ) catch continue;
+ };
- // If the allocation is sufficiently aligned, use it.
- if (@ptrToInt(addr) & (alignment - 1) == 0) {
- return @ptrCast([*]u8, addr)[0..n];
- }
+ return @ptrCast([*]u8, final_addr)[0..n];
+ }
- // If it wasn't, actually do an explicitely aligned allocation.
- if (w.VirtualFree(addr, 0, w.MEM_RELEASE) == 0) unreachable;
- const alloc_size = n + alignment;
-
- const final_addr = while (true) {
- // Reserve a range of memory large enough to find a sufficiently
- // aligned address.
- const reserved_addr = w.VirtualAlloc(
- null,
- alloc_size,
- w.MEM_RESERVE,
- w.PAGE_NOACCESS,
- ) orelse return error.OutOfMemory;
- const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment);
-
- // Release the reserved pages (not actually used).
- if (w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE) == 0) unreachable;
-
- // At this point, it is possible that another thread has
- // obtained some memory space that will cause the next
- // VirtualAlloc call to fail. To handle this, we will retry
- // until it succeeds.
- if (w.VirtualAlloc(
- @intToPtr(*c_void, aligned_addr),
- n,
- w.MEM_COMMIT | w.MEM_RESERVE,
- w.PAGE_READWRITE,
- )) |ptr| break ptr;
- } else unreachable; // TODO else unreachable should not be necessary
-
- return @ptrCast([*]u8, final_addr)[0..n];
- },
- else => @compileError("Unsupported OS"),
+ const alloc_size = if (alignment <= os.page_size) n else n + alignment;
+ const addr = os.mmap(
+ null,
+ alloc_size,
+ os.PROT_READ | os.PROT_WRITE,
+ os.MAP_PRIVATE | os.MAP_ANONYMOUS,
+ -1,
+ 0,
+ ) catch return error.OutOfMemory;
+ if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n];
+
+ const aligned_addr = mem.alignForward(addr, alignment);
+
+ // Unmap the extra bytes that were only requested in order to guarantee
+ // that the range of memory we were provided had a proper alignment in
+ // it somewhere. The extra bytes could be at the beginning, or end, or both.
+ const unused_start_len = aligned_addr - addr;
+ if (unused_start_len != 0) {
+ os.munmap(addr, unused_start_len);
+ }
+ const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size);
+ const unused_end_len = addr + alloc_size - aligned_end_addr;
+ if (unused_end_len != 0) {
+ os.munmap(aligned_end_addr, unused_end_len);
}
+
+ return @intToPtr([*]u8, aligned_addr)[0..n];
}
fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
- switch (builtin.os) {
- Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
+ if (os.windows.is_the_target) {
+ const w = os.windows;
+ if (new_size == 0) {
+ // From the docs:
+ // "If the dwFreeType parameter is MEM_RELEASE, this parameter
+ // must be 0 (zero). The function frees the entire region that
+ // is reserved in the initial allocation call to VirtualAlloc."
+ // So we can only use MEM_RELEASE when actually releasing the
+ // whole allocation.
+ w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE);
+ } else {
const base_addr = @ptrToInt(old_mem.ptr);
const old_addr_end = base_addr + old_mem.len;
const new_addr_end = base_addr + new_size;
const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size);
if (old_addr_end > new_addr_end_rounded) {
- _ = os.posix.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded);
- }
- return old_mem[0..new_size];
- },
- .windows => {
- const w = os.windows;
- if (new_size == 0) {
- // From the docs:
- // "If the dwFreeType parameter is MEM_RELEASE, this parameter
- // must be 0 (zero). The function frees the entire region that
- // is reserved in the initial allocation call to VirtualAlloc."
- // So we can only use MEM_RELEASE when actually releasing the
- // whole allocation.
- if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable;
- } else {
- const base_addr = @ptrToInt(old_mem.ptr);
- const old_addr_end = base_addr + old_mem.len;
- const new_addr_end = base_addr + new_size;
- const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size);
- if (old_addr_end > new_addr_end_rounded) {
- // For shrinking that is not releasing, we will only
- // decommit the pages not needed anymore.
- if (w.VirtualFree(
- @intToPtr(*c_void, new_addr_end_rounded),
- old_addr_end - new_addr_end_rounded,
- w.MEM_DECOMMIT,
- ) == 0) unreachable;
- }
+ // For shrinking that is not releasing, we will only
+ // decommit the pages not needed anymore.
+ w.VirtualFree(
+ @intToPtr(*c_void, new_addr_end_rounded),
+ old_addr_end - new_addr_end_rounded,
+ w.MEM_DECOMMIT,
+ );
}
- return old_mem[0..new_size];
- },
- else => @compileError("Unsupported OS"),
+ }
+ return old_mem[0..new_size];
}
+ const base_addr = @ptrToInt(old_mem.ptr);
+ const old_addr_end = base_addr + old_mem.len;
+ const new_addr_end = base_addr + new_size;
+ const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size);
+ if (old_addr_end > new_addr_end_rounded) {
+ os.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded);
+ }
+ return old_mem[0..new_size];
}
fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
- switch (builtin.os) {
- Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => {
- if (new_size <= old_mem.len and new_align <= old_align) {
- return shrink(allocator, old_mem, old_align, new_size, new_align);
- }
- const result = try alloc(allocator, new_size, new_align);
- if (old_mem.len != 0) {
- @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
- _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
- }
- return result;
- },
- .windows => {
- if (old_mem.len == 0) {
- return alloc(allocator, new_size, new_align);
- }
-
- if (new_size <= old_mem.len and new_align <= old_align) {
- return shrink(allocator, old_mem, old_align, new_size, new_align);
- }
-
- const w = os.windows;
- const base_addr = @ptrToInt(old_mem.ptr);
+ if (os.windows.is_the_target) {
+ if (old_mem.len == 0) {
+ return alloc(allocator, new_size, new_align);
+ }
- if (new_align > old_align and base_addr & (new_align - 1) != 0) {
- // Current allocation doesn't satisfy the new alignment.
- // For now we'll do a new one no matter what, but maybe
- // there is something smarter to do instead.
- const result = try alloc(allocator, new_size, new_align);
- assert(old_mem.len != 0);
- @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
- if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable;
+ if (new_size <= old_mem.len and new_align <= old_align) {
+ return shrink(allocator, old_mem, old_align, new_size, new_align);
+ }
- return result;
- }
+ const w = os.windows;
+ const base_addr = @ptrToInt(old_mem.ptr);
- const old_addr_end = base_addr + old_mem.len;
- const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size);
- const new_addr_end = base_addr + new_size;
- const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size);
- if (new_addr_end_rounded == old_addr_end_rounded) {
- // The reallocation fits in the already allocated pages.
- return @ptrCast([*]u8, old_mem.ptr)[0..new_size];
- }
- assert(new_addr_end_rounded > old_addr_end_rounded);
+ if (new_align > old_align and base_addr & (new_align - 1) != 0) {
+ // Current allocation doesn't satisfy the new alignment.
+ // For now we'll do a new one no matter what, but maybe
+ // there is something smarter to do instead.
+ const result = try alloc(allocator, new_size, new_align);
+ assert(old_mem.len != 0);
+ @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
+ w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE);
- // We need to commit new pages.
- const additional_size = new_addr_end - old_addr_end_rounded;
- const realloc_addr = w.VirtualAlloc(
- @intToPtr(*c_void, old_addr_end_rounded),
- additional_size,
- w.MEM_COMMIT | w.MEM_RESERVE,
- w.PAGE_READWRITE,
- ) orelse {
- // Committing new pages at the end of the existing allocation
- // failed, we need to try a new one.
- const new_alloc_mem = try alloc(allocator, new_size, new_align);
- @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len);
- if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable;
-
- return new_alloc_mem;
- };
+ return result;
+ }
- assert(@ptrToInt(realloc_addr) == old_addr_end_rounded);
+ const old_addr_end = base_addr + old_mem.len;
+ const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size);
+ const new_addr_end = base_addr + new_size;
+ const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size);
+ if (new_addr_end_rounded == old_addr_end_rounded) {
+ // The reallocation fits in the already allocated pages.
return @ptrCast([*]u8, old_mem.ptr)[0..new_size];
- },
- else => @compileError("Unsupported OS"),
+ }
+ assert(new_addr_end_rounded > old_addr_end_rounded);
+
+ // We need to commit new pages.
+ const additional_size = new_addr_end - old_addr_end_rounded;
+ const realloc_addr = w.kernel32.VirtualAlloc(
+ @intToPtr(*c_void, old_addr_end_rounded),
+ additional_size,
+ w.MEM_COMMIT | w.MEM_RESERVE,
+ w.PAGE_READWRITE,
+ ) orelse {
+ // Committing new pages at the end of the existing allocation
+ // failed, we need to try a new one.
+ const new_alloc_mem = try alloc(allocator, new_size, new_align);
+ @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len);
+ w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE);
+
+ return new_alloc_mem;
+ };
+
+ assert(@ptrToInt(realloc_addr) == old_addr_end_rounded);
+ return @ptrCast([*]u8, old_mem.ptr)[0..new_size];
+ }
+ if (new_size <= old_mem.len and new_align <= old_align) {
+ return shrink(allocator, old_mem, old_align, new_size, new_align);
}
+ const result = try alloc(allocator, new_size, new_align);
+ if (old_mem.len != 0) {
+ @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len));
+ os.munmap(@ptrToInt(old_mem.ptr), old_mem.len);
+ }
+ return result;
}
};
std/mem.zig
@@ -1466,17 +1466,17 @@ test "std.mem.alignForward" {
pub fn getBaseAddress() usize {
switch (builtin.os) {
.linux => {
- const base = std.os.posix.getauxval(std.elf.AT_BASE);
+ const base = std.os.system.getauxval(std.elf.AT_BASE);
if (base != 0) {
return base;
}
- const phdr = std.os.posix.getauxval(std.elf.AT_PHDR);
+ const phdr = std.os.system.getauxval(std.elf.AT_PHDR);
return phdr - @sizeOf(std.elf.Ehdr);
},
.macosx, .freebsd, .netbsd => {
return @ptrToInt(&std.c._mh_execute_header);
},
- .windows => return @ptrToInt(windows.GetModuleHandleW(null)),
+ .windows => return @ptrToInt(windows.kernel32.GetModuleHandleW(null)),
else => @compileError("Unsupported OS"),
}
}
std/mutex.zig
@@ -152,9 +152,9 @@ test "std.Mutex" {
testing.expect(context.data == TestContext.incr_count);
} else {
const thread_count = 10;
- var threads: [thread_count]*std.os.Thread = undefined;
+ var threads: [thread_count]*std.Thread = undefined;
for (threads) |*t| {
- t.* = try std.os.spawnThread(&context, worker);
+ t.* = try std.Thread.spawn(&context, worker);
}
for (threads) |t|
t.wait();
std/net.zig
@@ -2,8 +2,8 @@ const std = @import("std.zig");
const builtin = @import("builtin");
const assert = std.debug.assert;
const net = @This();
-const posix = std.os.posix;
const mem = std.mem;
+const os = std.os;
pub const TmpWinAddr = struct {
family: u8,
@@ -12,7 +12,7 @@ pub const TmpWinAddr = struct {
pub const OsAddress = switch (builtin.os) {
builtin.Os.windows => TmpWinAddr,
- else => posix.sockaddr,
+ else => os.sockaddr,
};
pub const Address = struct {
@@ -20,9 +20,9 @@ pub const Address = struct {
pub fn initIp4(ip4: u32, _port: u16) Address {
return Address{
- .os_addr = posix.sockaddr{
- .in = posix.sockaddr_in{
- .family = posix.AF_INET,
+ .os_addr = os.sockaddr{
+ .in = os.sockaddr_in{
+ .family = os.AF_INET,
.port = mem.nativeToBig(u16, _port),
.addr = ip4,
.zero = []u8{0} ** 8,
@@ -33,10 +33,10 @@ pub const Address = struct {
pub fn initIp6(ip6: *const Ip6Addr, _port: u16) Address {
return Address{
- .family = posix.AF_INET6,
- .os_addr = posix.sockaddr{
- .in6 = posix.sockaddr_in6{
- .family = posix.AF_INET6,
+ .family = os.AF_INET6,
+ .os_addr = os.sockaddr{
+ .in6 = os.sockaddr_in6{
+ .family = os.AF_INET6,
.port = mem.nativeToBig(u16, _port),
.flowinfo = 0,
.addr = ip6.addr,
@@ -50,18 +50,18 @@ pub const Address = struct {
return mem.bigToNative(u16, self.os_addr.in.port);
}
- pub fn initPosix(addr: posix.sockaddr) Address {
+ pub fn initPosix(addr: os.sockaddr) Address {
return Address{ .os_addr = addr };
}
pub fn format(self: *const Address, out_stream: var) !void {
switch (self.os_addr.in.family) {
- posix.AF_INET => {
+ os.AF_INET => {
const native_endian_port = mem.bigToNative(u16, self.os_addr.in.port);
const bytes = ([]const u8)((*self.os_addr.in.addr)[0..1]);
try out_stream.print("{}.{}.{}.{}:{}", bytes[0], bytes[1], bytes[2], bytes[3], native_endian_port);
},
- posix.AF_INET6 => {
+ os.AF_INET6 => {
const native_endian_port = mem.bigToNative(u16, self.os_addr.in6.port);
try out_stream.print("[TODO render ip6 address]:{}", native_endian_port);
},
std/os.zig
@@ -164,7 +164,7 @@ pub fn raise(sig: u8) RaiseError!void {
}
if (windows.is_the_target) {
- @compileError("TODO implement std.posix.raise for Windows");
+ @compileError("TODO implement std.os.raise for Windows");
}
var set: system.sigset_t = undefined;
@@ -690,20 +690,6 @@ pub fn getenvC(key: [*]const u8) ?[]const u8 {
return getenv(mem.toSliceConst(u8, key));
}
-/// See std.elf for the constants.
-pub fn getauxval(index: usize) usize {
- if (builtin.link_libc) {
- return usize(system.getauxval(index));
- } else if (linux.elf_aux_maybe) |auxv| {
- var i: usize = 0;
- while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) {
- if (auxv[i].a_type == index)
- return auxv[i].a_un.a_val;
- }
- }
- return 0;
-}
-
pub const GetCwdError = error{
NameTooLong,
CurrentWorkingDirectoryUnlinked,
@@ -1198,7 +1184,7 @@ pub fn isatty(handle: fd_t) bool {
return windows.kernel32.GetConsoleMode(handle, &out) != 0;
}
if (wasi.is_the_target) {
- @compileError("TODO implement std.os.posix.isatty for WASI");
+ @compileError("TODO implement std.os.isatty for WASI");
}
if (linux.is_the_target) {
var wsz: system.winsize = undefined;
@@ -2365,6 +2351,23 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
}
}
+pub const SchedGetAffinityError = error{
+ PermissionDenied,
+ Unexpected,
+};
+
+pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t {
+ var set: cpu_set_t = undefined;
+ switch (errno(system.sched_getaffinity(pid, &set))) {
+ 0 => return set,
+ EFAULT => unreachable,
+ EINVAL => unreachable,
+ ESRCH => unreachable,
+ EPERM => return error.PermissionDenied,
+ else => |err| return unexpectedErrno(err),
+ }
+}
+
/// Used to convert a slice to a null terminated slice on the stack.
/// TODO https://github.com/ziglang/zig/issues/287
pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 {
std/process.zig
@@ -1,13 +1,13 @@
const std = @import("std.zig");
-const posix = std.os.posix;
+const os = std.os;
const BufMap = std.BufMap;
const mem = std.mem;
const Allocator = mem.Allocator;
const assert = std.debug.assert;
const testing = std.testing;
-pub const abort = posix.abort;
-pub const exit = posix.exit;
+pub const abort = os.abort;
+pub const exit = os.exit;
/// Caller must free result when done.
/// TODO make this go through libc when we have it
@@ -42,13 +42,13 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
try result.setMove(key, value);
}
- } else if (builtin.os == Os.wasi) {
+ } else if (builtin.os == .wasi) {
var environ_count: usize = undefined;
var environ_buf_size: usize = undefined;
- const environ_sizes_get_ret = std.os.wasi.environ_sizes_get(&environ_count, &environ_buf_size);
+ const environ_sizes_get_ret = os.wasi.environ_sizes_get(&environ_count, &environ_buf_size);
if (environ_sizes_get_ret != os.wasi.ESUCCESS) {
- return unexpectedErrorPosix(environ_sizes_get_ret);
+ return os.unexpectedErrno(environ_sizes_get_ret);
}
// TODO: Verify that the documentation is incorrect
@@ -58,9 +58,9 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
var environ_buf = try std.heap.wasm_allocator.alloc(u8, environ_buf_size);
defer allocator.free(environ_buf);
- const environ_get_ret = std.os.wasi.environ_get(environ.ptr, environ_buf.ptr);
+ const environ_get_ret = os.wasi.environ_get(environ.ptr, environ_buf.ptr);
if (environ_get_ret != os.wasi.ESUCCESS) {
- return unexpectedErrorPosix(environ_get_ret);
+ return os.unexpectedErrno(environ_get_ret);
}
for (environ) |env| {
@@ -74,7 +74,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
}
return result;
} else {
- for (posix.environ) |ptr| {
+ for (os.environ) |ptr| {
var line_i: usize = 0;
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
const key = ptr[0..line_i];
@@ -331,12 +331,12 @@ pub const ArgIteratorWindows = struct {
};
pub const ArgIterator = struct {
- const InnerType = if (builtin.os == Os.windows) ArgIteratorWindows else ArgIteratorPosix;
+ const InnerType = if (builtin.os == .windows) ArgIteratorWindows else ArgIteratorPosix;
inner: InnerType,
pub fn init() ArgIterator {
- if (builtin.os == Os.wasi) {
+ if (builtin.os == .wasi) {
// TODO: Figure out a compatible interface accomodating WASI
@compileError("ArgIterator is not yet supported in WASI. Use argsAlloc and argsFree instead.");
}
@@ -348,7 +348,7 @@ pub const ArgIterator = struct {
/// You must free the returned memory when done.
pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) {
- if (builtin.os == Os.windows) {
+ if (builtin.os == .windows) {
return self.inner.next(allocator);
} else {
return mem.dupe(allocator, u8, self.inner.next() orelse return null);
@@ -373,13 +373,13 @@ pub fn args() ArgIterator {
/// Caller must call argsFree on result.
pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 {
- if (builtin.os == Os.wasi) {
+ if (builtin.os == .wasi) {
var count: usize = undefined;
var buf_size: usize = undefined;
const args_sizes_get_ret = os.wasi.args_sizes_get(&count, &buf_size);
if (args_sizes_get_ret != os.wasi.ESUCCESS) {
- return unexpectedErrorPosix(args_sizes_get_ret);
+ return os.unexpectedErrno(args_sizes_get_ret);
}
var argv = try allocator.alloc([*]u8, count);
@@ -388,7 +388,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 {
var argv_buf = try allocator.alloc(u8, buf_size);
const args_get_ret = os.wasi.args_get(argv.ptr, argv_buf.ptr);
if (args_get_ret != os.wasi.ESUCCESS) {
- return unexpectedErrorPosix(args_get_ret);
+ return os.unexpectedErrno(args_get_ret);
}
var result_slice = try allocator.alloc([]u8, count);
@@ -438,7 +438,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 {
}
pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void {
- if (builtin.os == Os.wasi) {
+ if (builtin.os == .wasi) {
const last_item = args_alloc[args_alloc.len - 1];
const last_byte_addr = @ptrToInt(last_item.ptr) + last_item.len + 1; // null terminated
const first_item_ptr = args_alloc[0].ptr;
@@ -491,7 +491,7 @@ pub const UserInfo = struct {
/// POSIX function which gets a uid from username.
pub fn getUserInfo(name: []const u8) !UserInfo {
return switch (builtin.os) {
- .linux, .macosx, .ios, .freebsd, .netbsd => posixGetUserInfo(name),
+ .linux, .macosx, .watchos, .tvos, .ios, .freebsd, .netbsd => posixGetUserInfo(name),
else => @compileError("Unsupported OS"),
};
}
std/statically_initialized_mutex.zig
@@ -96,9 +96,9 @@ test "std.StaticallyInitializedMutex" {
expect(context.data == TestContext.incr_count);
} else {
const thread_count = 10;
- var threads: [thread_count]*std.os.Thread = undefined;
+ var threads: [thread_count]*std.Thread = undefined;
for (threads) |*t| {
- t.* = try std.os.spawnThread(&context, TestContext.worker);
+ t.* = try std.Thread.spawn(&context, TestContext.worker);
}
for (threads) |t|
t.wait();
std/thread.zig
@@ -1,6 +1,8 @@
const builtin = @import("builtin");
const std = @import("std.zig");
+const os = std.os;
const windows = std.os.windows;
+const c = std.c;
pub const Thread = struct {
data: Data,
@@ -13,8 +15,8 @@ pub const Thread = struct {
pub const Handle = if (use_pthreads)
c.pthread_t
else switch (builtin.os) {
- builtin.Os.linux => i32,
- builtin.Os.windows => windows.HANDLE,
+ .linux => i32,
+ .windows => windows.HANDLE,
else => @compileError("Unsupported OS"),
};
@@ -22,7 +24,7 @@ pub const Thread = struct {
/// May be an integer or pointer depending on the platform.
/// On Linux and POSIX, this is the same as Handle.
pub const Id = switch (builtin.os) {
- builtin.Os.windows => windows.DWORD,
+ .windows => windows.DWORD,
else => Handle,
};
@@ -33,12 +35,12 @@ pub const Thread = struct {
mmap_len: usize,
}
else switch (builtin.os) {
- builtin.Os.linux => struct {
+ .linux => struct {
handle: Thread.Handle,
mmap_addr: usize,
mmap_len: usize,
},
- builtin.Os.windows => struct {
+ .windows => struct {
handle: Thread.Handle,
alloc_start: *c_void,
heap_handle: windows.HANDLE,
@@ -54,8 +56,8 @@ pub const Thread = struct {
return c.pthread_self();
} else
return switch (builtin.os) {
- builtin.Os.linux => linux.gettid(),
- builtin.Os.windows => windows.GetCurrentThreadId(),
+ .linux => linux.gettid(),
+ .windows => windows.GetCurrentThreadId(),
else => @compileError("Unsupported OS"),
};
}
@@ -75,28 +77,28 @@ pub const Thread = struct {
const err = c.pthread_join(self.data.handle, null);
switch (err) {
0 => {},
- posix.EINVAL => unreachable,
- posix.ESRCH => unreachable,
- posix.EDEADLK => unreachable,
+ os.EINVAL => unreachable,
+ os.ESRCH => unreachable,
+ os.EDEADLK => unreachable,
else => unreachable,
}
- assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0);
+ os.munmap(self.data.mmap_addr, self.data.mmap_len);
} else switch (builtin.os) {
- builtin.Os.linux => {
+ .linux => {
while (true) {
const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst);
if (pid_value == 0) break;
const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null);
switch (linux.getErrno(rc)) {
0 => continue,
- posix.EINTR => continue,
- posix.EAGAIN => continue,
+ os.EINTR => continue,
+ os.EAGAIN => continue,
else => unreachable,
}
}
- assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0);
+ os.munmap(self.data.mmap_addr, self.data.mmap_len);
},
- builtin.Os.windows => {
+ .windows => {
assert(windows.WaitForSingleObject(self.data.handle, windows.INFINITE) == windows.WAIT_OBJECT_0);
assert(windows.CloseHandle(self.data.handle) != 0);
assert(windows.HeapFree(self.data.heap_handle, 0, self.data.alloc_start) != 0);
@@ -153,10 +155,10 @@ pub const Thread = struct {
extern fn threadMain(raw_arg: windows.LPVOID) windows.DWORD {
const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*;
switch (@typeId(@typeOf(startFn).ReturnType)) {
- builtin.TypeId.Int => {
+ .Int => {
return startFn(arg);
},
- builtin.TypeId.Void => {
+ .Void => {
startFn(arg);
return 0;
},
@@ -196,10 +198,10 @@ pub const Thread = struct {
const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*;
switch (@typeId(@typeOf(startFn).ReturnType)) {
- builtin.TypeId.Int => {
+ .Int => {
return startFn(arg);
},
- builtin.TypeId.Void => {
+ .Void => {
startFn(arg);
return 0;
},
@@ -217,7 +219,7 @@ pub const Thread = struct {
}
};
- const MAP_GROWSDOWN = if (builtin.os == builtin.Os.linux) linux.MAP_GROWSDOWN else 0;
+ const MAP_GROWSDOWN = if (builtin.os == .linux) linux.MAP_GROWSDOWN else 0;
var stack_end_offset: usize = undefined;
var thread_start_offset: usize = undefined;
@@ -247,9 +249,8 @@ pub const Thread = struct {
}
break :blk l;
};
- const mmap_addr = posix.mmap(null, mmap_len, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0);
- if (mmap_addr == posix.MAP_FAILED) return error.OutOfMemory;
- errdefer assert(posix.munmap(mmap_addr, mmap_len) == 0);
+ const mmap_addr = try os.mmap(null, mmap_len, os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0);
+ errdefer os.munmap(mmap_addr, mmap_len);
const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset));
thread_ptr.data.mmap_addr = mmap_addr;
@@ -273,31 +274,30 @@ pub const Thread = struct {
const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg));
switch (err) {
0 => return thread_ptr,
- posix.EAGAIN => return error.SystemResources,
- posix.EPERM => unreachable,
- posix.EINVAL => unreachable,
- else => return unexpectedErrorPosix(@intCast(usize, err)),
+ os.EAGAIN => return error.SystemResources,
+ os.EPERM => unreachable,
+ os.EINVAL => unreachable,
+ else => return os.unexpectedErrno(@intCast(usize, err)),
}
- } else if (builtin.os == builtin.Os.linux) {
- var flags: u32 = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND |
- posix.CLONE_THREAD | posix.CLONE_SYSVSEM | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID |
- posix.CLONE_DETACHED;
+ } else if (builtin.os == .linux) {
+ var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |
+ os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
+ os.CLONE_DETACHED;
var newtls: usize = undefined;
if (linux.tls.tls_image) |tls_img| {
newtls = linux.tls.copyTLS(mmap_addr + tls_start_offset);
- flags |= posix.CLONE_SETTLS;
+ flags |= os.CLONE_SETTLS;
}
- const rc = posix.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle);
- const err = posix.getErrno(rc);
- switch (err) {
+ const rc = os.linux.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle);
+ switch (os.errno(rc)) {
0 => return thread_ptr,
- posix.EAGAIN => return error.ThreadQuotaExceeded,
- posix.EINVAL => unreachable,
- posix.ENOMEM => return error.SystemResources,
- posix.ENOSPC => unreachable,
- posix.EPERM => unreachable,
- posix.EUSERS => unreachable,
- else => return unexpectedErrorPosix(err),
+ os.EAGAIN => return error.ThreadQuotaExceeded,
+ os.EINVAL => unreachable,
+ os.ENOMEM => return error.SystemResources,
+ os.ENOSPC => unreachable,
+ os.EPERM => unreachable,
+ os.EUSERS => unreachable,
+ else => |err| return os.unexpectedErrno(err),
}
} else {
@compileError("Unsupported OS");
@@ -310,56 +310,20 @@ pub const Thread = struct {
Unexpected,
};
- pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize {
- switch (builtin.os) {
- .macosx, .freebsd, .netbsd => {
- var count: c_int = undefined;
- var count_len: usize = @sizeOf(c_int);
- const name = switch (builtin.os) {
- builtin.Os.macosx => c"hw.logicalcpu",
- else => c"hw.ncpu",
- };
- try posix.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0);
- return @intCast(usize, count);
- },
- .linux => {
- const usize_count = 16;
- const allocator = std.heap.stackFallback(usize_count * @sizeOf(usize), fallback_allocator).get();
-
- var set = try allocator.alloc(usize, usize_count);
- defer allocator.free(set);
-
- while (true) {
- const rc = posix.sched_getaffinity(0, set);
- const err = posix.getErrno(rc);
- switch (err) {
- 0 => {
- if (rc < set.len * @sizeOf(usize)) {
- const result = set[0 .. rc / @sizeOf(usize)];
- var sum: usize = 0;
- for (result) |x| {
- sum += @popCount(usize, x);
- }
- return sum;
- } else {
- set = try allocator.realloc(set, set.len * 2);
- continue;
- }
- },
- posix.EFAULT => unreachable,
- posix.EINVAL => unreachable,
- posix.EPERM => return CpuCountError.PermissionDenied,
- posix.ESRCH => unreachable,
- else => return os.unexpectedErrorPosix(err),
- }
- }
- },
- .windows => {
- var system_info: windows.SYSTEM_INFO = undefined;
- windows.GetSystemInfo(&system_info);
- return @intCast(usize, system_info.dwNumberOfProcessors);
- },
- else => @compileError("unsupported OS"),
+ pub fn cpuCount() CpuCountError!usize {
+ if (os.linux.is_the_target) {
+ const cpu_set = try os.sched_getaffinity(0);
+ return os.CPU_COUNT(cpu_set);
+ }
+ if (os.windows.is_the_target) {
+ var system_info: windows.SYSTEM_INFO = undefined;
+ windows.GetSystemInfo(&system_info);
+ return @intCast(usize, system_info.dwNumberOfProcessors);
}
+ var count: c_int = undefined;
+ var count_len: usize = @sizeOf(c_int);
+ const name = if (os.darwin.is_the_target) c"hw.logicalcpu" else c"hw.ncpu";
+ try os.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0);
+ return @intCast(usize, count);
}
};
CMakeLists.txt
@@ -620,7 +620,6 @@ set(ZIG_STD_FILES
"os/freebsd.zig"
"os/linux.zig"
"os/linux/arm64.zig"
- "os/linux/sys.zig"
"os/linux/tls.zig"
"os/linux/vdso.zig"
"os/linux/x86_64.zig"