Skip to content

Commit 2fbe951

Browse files
zigazeljkoandrewrk
authored andcommitted
std: add support for ppoll
1 parent a2e9554 commit 2fbe951

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

lib/std/c.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ pub extern "c" fn getnameinfo(
295295
pub extern "c" fn gai_strerror(errcode: EAI) [*:0]const u8;
296296

297297
pub extern "c" fn poll(fds: [*]pollfd, nfds: nfds_t, timeout: c_int) c_int;
298+
pub extern "c" fn ppoll(fds: [*]pollfd, nfds: nfds_t, timeout: ?*const timespec, sigmask: ?*const sigset_t) c_int;
298299

299300
pub extern "c" fn dn_expand(
300301
msg: [*:0]const u8,

lib/std/os.zig

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,7 +2298,7 @@ pub const ChangeCurDirError = error{
22982298
SystemResources,
22992299
NotDir,
23002300
BadPathName,
2301-
2301+
23022302
/// On Windows, file paths must be valid Unicode.
23032303
InvalidUtf8,
23042304
} || UnexpectedError;
@@ -5250,6 +5250,32 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {
52505250
}
52515251
}
52525252

5253+
pub const PPollError = error{
5254+
/// The operation was interrupted by a delivery of a signal before it could complete.
5255+
SignalInterrupt,
5256+
5257+
/// The kernel had no space to allocate file descriptor tables.
5258+
SystemResources,
5259+
} || UnexpectedError;
5260+
5261+
pub fn ppoll(fds: []pollfd, timeout: ?*const timespec, mask: ?*const sigset_t) PPollError!usize {
5262+
var ts: timespec = undefined;
5263+
var ts_ptr: ?*timespec = null;
5264+
if (timeout) |timeout_ns| {
5265+
ts_ptr = &ts;
5266+
ts = timeout_ns.*;
5267+
}
5268+
const rc = system.ppoll(fds.ptr, fds.len, ts_ptr, mask);
5269+
switch (errno(rc)) {
5270+
0 => return @intCast(usize, rc),
5271+
EFAULT => unreachable,
5272+
EINTR => return error.SignalInterrupt,
5273+
EINVAL => unreachable,
5274+
ENOMEM => return error.SystemResources,
5275+
else => |err| return unexpectedErrno(err),
5276+
}
5277+
}
5278+
52535279
pub const RecvFromError = error{
52545280
/// The socket is marked nonblocking and the requested operation would block, and
52555281
/// there is no global event loop configured.
@@ -5565,7 +5591,7 @@ pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t {
55655591
EMFILE => return error.ProcessResources,
55665592
ENODEV => return error.InodeMountFail,
55675593
ENOSYS => return error.SystemOutdated,
5568-
else => |err| return std.os.unexpectedErrno(err),
5594+
else => |err| return unexpectedErrno(err),
55695595
}
55705596
}
55715597

@@ -5590,7 +5616,7 @@ pub fn syncfs(fd: fd_t) SyncError!void {
55905616
EIO => return error.InputOutput,
55915617
ENOSPC => return error.NoSpaceLeft,
55925618
EDQUOT => return error.DiskQuota,
5593-
else => |err| return std.os.unexpectedErrno(err),
5619+
else => |err| return unexpectedErrno(err),
55945620
}
55955621
}
55965622

@@ -5614,7 +5640,7 @@ pub fn fsync(fd: fd_t) SyncError!void {
56145640
EIO => return error.InputOutput,
56155641
ENOSPC => return error.NoSpaceLeft,
56165642
EDQUOT => return error.DiskQuota,
5617-
else => |err| return std.os.unexpectedErrno(err),
5643+
else => |err| return unexpectedErrno(err),
56185644
}
56195645
}
56205646

@@ -5633,7 +5659,7 @@ pub fn fdatasync(fd: fd_t) SyncError!void {
56335659
EIO => return error.InputOutput,
56345660
ENOSPC => return error.NoSpaceLeft,
56355661
EDQUOT => return error.DiskQuota,
5636-
else => |err| return std.os.unexpectedErrno(err),
5662+
else => |err| return unexpectedErrno(err),
56375663
}
56385664
}
56395665

@@ -5675,7 +5701,7 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 {
56755701
EOPNOTSUPP => return error.OperationNotSupported,
56765702
EPERM, EBUSY => return error.PermissionDenied,
56775703
ERANGE => unreachable,
5678-
else => |err| return std.os.unexpectedErrno(err),
5704+
else => |err| return unexpectedErrno(err),
56795705
}
56805706
}
56815707

@@ -5688,7 +5714,7 @@ pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {
56885714
0 => return limits,
56895715
EFAULT => unreachable, // bogus pointer
56905716
EINVAL => unreachable,
5691-
else => |err| return std.os.unexpectedErrno(err),
5717+
else => |err| return unexpectedErrno(err),
56925718
}
56935719
}
56945720

@@ -5701,6 +5727,6 @@ pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void
57015727
EFAULT => unreachable, // bogus pointer
57025728
EINVAL => unreachable,
57035729
EPERM => return error.PermissionDenied,
5704-
else => |err| return std.os.unexpectedErrno(err),
5730+
else => |err| return unexpectedErrno(err),
57055731
}
57065732
}

lib/std/os/linux.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize {
278278
if (@hasField(SYS, "poll")) {
279279
return syscall3(.poll, @ptrToInt(fds), n, @bitCast(u32, timeout));
280280
} else {
281-
return syscall6(
281+
return syscall5(
282282
.ppoll,
283283
@ptrToInt(fds),
284284
n,
@@ -290,12 +290,15 @@ pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize {
290290
else
291291
null),
292292
0,
293-
0,
294293
NSIG / 8,
295294
);
296295
}
297296
}
298297

298+
pub fn ppoll(fds: [*]pollfd, n: nfds_t, timeout: ?*timespec, sigmask: ?*const sigset_t) usize {
299+
return syscall5(.ppoll, @ptrToInt(fds), n, @ptrToInt(timeout), @ptrToInt(sigmask), NSIG / 8);
300+
}
301+
299302
pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
300303
return syscall3(.read, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count);
301304
}
@@ -1194,7 +1197,7 @@ pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout
11941197
return epoll_pwait(epoll_fd, events, maxevents, timeout, null);
11951198
}
11961199

1197-
pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize {
1200+
pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*const sigset_t) usize {
11981201
return syscall6(
11991202
.epoll_pwait,
12001203
@bitCast(usize, @as(isize, epoll_fd)),

0 commit comments

Comments
 (0)