authorgravatar for ziga.zeljko@gmail.comŽiga Željko <ziga.zeljko@gmail.com> 2020-11-20 20:26:04+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-21 14:06:04-08:00
log2fbe9519acec0d7b9c9dcc41a877fec912337124
treec18e2776fb09ee85af1d5899bebde8995199408d
parenta2e95546995b126464728ecd2075ee93565005bf

std: add support for ppoll


3 files changed, 41 insertions(+), 11 deletions(-)

lib/std/c.zig+1
...@@ -295,6 +295,7 @@ pub extern "c" fn getnameinfo(...@@ -295,6 +295,7 @@ pub extern "c" fn getnameinfo(
295pub extern "c" fn gai_strerror(errcode: EAI) [*:0]const u8;295pub extern "c" fn gai_strerror(errcode: EAI) [*:0]const u8;
296296
297pub extern "c" fn poll(fds: [*]pollfd, nfds: nfds_t, timeout: c_int) c_int;297pub extern "c" fn poll(fds: [*]pollfd, nfds: nfds_t, timeout: c_int) c_int;
298pub extern "c" fn ppoll(fds: [*]pollfd, nfds: nfds_t, timeout: ?*const timespec, sigmask: ?*const sigset_t) c_int;
298299
299pub extern "c" fn dn_expand(300pub extern "c" fn dn_expand(
300 msg: [*:0]const u8,301 msg: [*:0]const u8,
lib/std/os.zig+34-8
...@@ -2298,7 +2298,7 @@ pub const ChangeCurDirError = error{...@@ -2298,7 +2298,7 @@ pub const ChangeCurDirError = error{
2298 SystemResources,2298 SystemResources,
2299 NotDir,2299 NotDir,
2300 BadPathName,2300 BadPathName,
2301 2301
2302 /// On Windows, file paths must be valid Unicode.2302 /// On Windows, file paths must be valid Unicode.
2303 InvalidUtf8,2303 InvalidUtf8,
2304} || UnexpectedError;2304} || UnexpectedError;
...@@ -5250,6 +5250,32 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {...@@ -5250,6 +5250,32 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {
5250 }5250 }
5251}5251}
52525252
5253pub 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
5261pub 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
5253pub const RecvFromError = error{5279pub const RecvFromError = error{
5254 /// The socket is marked nonblocking and the requested operation would block, and5280 /// The socket is marked nonblocking and the requested operation would block, and
5255 /// there is no global event loop configured.5281 /// 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 {...@@ -5565,7 +5591,7 @@ pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t {
5565 EMFILE => return error.ProcessResources,5591 EMFILE => return error.ProcessResources,
5566 ENODEV => return error.InodeMountFail,5592 ENODEV => return error.InodeMountFail,
5567 ENOSYS => return error.SystemOutdated,5593 ENOSYS => return error.SystemOutdated,
5568 else => |err| return std.os.unexpectedErrno(err),5594 else => |err| return unexpectedErrno(err),
5569 }5595 }
5570}5596}
55715597
...@@ -5590,7 +5616,7 @@ pub fn syncfs(fd: fd_t) SyncError!void {...@@ -5590,7 +5616,7 @@ pub fn syncfs(fd: fd_t) SyncError!void {
5590 EIO => return error.InputOutput,5616 EIO => return error.InputOutput,
5591 ENOSPC => return error.NoSpaceLeft,5617 ENOSPC => return error.NoSpaceLeft,
5592 EDQUOT => return error.DiskQuota,5618 EDQUOT => return error.DiskQuota,
5593 else => |err| return std.os.unexpectedErrno(err),5619 else => |err| return unexpectedErrno(err),
5594 }5620 }
5595}5621}
55965622
...@@ -5614,7 +5640,7 @@ pub fn fsync(fd: fd_t) SyncError!void {...@@ -5614,7 +5640,7 @@ pub fn fsync(fd: fd_t) SyncError!void {
5614 EIO => return error.InputOutput,5640 EIO => return error.InputOutput,
5615 ENOSPC => return error.NoSpaceLeft,5641 ENOSPC => return error.NoSpaceLeft,
5616 EDQUOT => return error.DiskQuota,5642 EDQUOT => return error.DiskQuota,
5617 else => |err| return std.os.unexpectedErrno(err),5643 else => |err| return unexpectedErrno(err),
5618 }5644 }
5619}5645}
56205646
...@@ -5633,7 +5659,7 @@ pub fn fdatasync(fd: fd_t) SyncError!void {...@@ -5633,7 +5659,7 @@ pub fn fdatasync(fd: fd_t) SyncError!void {
5633 EIO => return error.InputOutput,5659 EIO => return error.InputOutput,
5634 ENOSPC => return error.NoSpaceLeft,5660 ENOSPC => return error.NoSpaceLeft,
5635 EDQUOT => return error.DiskQuota,5661 EDQUOT => return error.DiskQuota,
5636 else => |err| return std.os.unexpectedErrno(err),5662 else => |err| return unexpectedErrno(err),
5637 }5663 }
5638}5664}
56395665
...@@ -5675,7 +5701,7 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 {...@@ -5675,7 +5701,7 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 {
5675 EOPNOTSUPP => return error.OperationNotSupported,5701 EOPNOTSUPP => return error.OperationNotSupported,
5676 EPERM, EBUSY => return error.PermissionDenied,5702 EPERM, EBUSY => return error.PermissionDenied,
5677 ERANGE => unreachable,5703 ERANGE => unreachable,
5678 else => |err| return std.os.unexpectedErrno(err),5704 else => |err| return unexpectedErrno(err),
5679 }5705 }
5680}5706}
56815707
...@@ -5688,7 +5714,7 @@ pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {...@@ -5688,7 +5714,7 @@ pub fn getrlimit(resource: rlimit_resource) GetrlimitError!rlimit {
5688 0 => return limits,5714 0 => return limits,
5689 EFAULT => unreachable, // bogus pointer5715 EFAULT => unreachable, // bogus pointer
5690 EINVAL => unreachable,5716 EINVAL => unreachable,
5691 else => |err| return std.os.unexpectedErrno(err),5717 else => |err| return unexpectedErrno(err),
5692 }5718 }
5693}5719}
56945720
...@@ -5701,6 +5727,6 @@ pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void...@@ -5701,6 +5727,6 @@ pub fn setrlimit(resource: rlimit_resource, limits: rlimit) SetrlimitError!void
5701 EFAULT => unreachable, // bogus pointer5727 EFAULT => unreachable, // bogus pointer
5702 EINVAL => unreachable,5728 EINVAL => unreachable,
5703 EPERM => return error.PermissionDenied,5729 EPERM => return error.PermissionDenied,
5704 else => |err| return std.os.unexpectedErrno(err),5730 else => |err| return unexpectedErrno(err),
5705 }5731 }
5706}5732}
lib/std/os/linux.zig+6-3
...@@ -278,7 +278,7 @@ pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize {...@@ -278,7 +278,7 @@ pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize {
278 if (@hasField(SYS, "poll")) {278 if (@hasField(SYS, "poll")) {
279 return syscall3(.poll, @ptrToInt(fds), n, @bitCast(u32, timeout));279 return syscall3(.poll, @ptrToInt(fds), n, @bitCast(u32, timeout));
280 } else {280 } else {
281 return syscall6(281 return syscall5(
282 .ppoll,282 .ppoll,
283 @ptrToInt(fds),283 @ptrToInt(fds),
284 n,284 n,
...@@ -290,12 +290,15 @@ pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize {...@@ -290,12 +290,15 @@ pub fn poll(fds: [*]pollfd, n: nfds_t, timeout: i32) usize {
290 else290 else
291 null),291 null),
292 0,292 0,
293 0,
294 NSIG / 8,293 NSIG / 8,
295 );294 );
296 }295 }
297}296}
298297
298pub 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
299pub fn read(fd: i32, buf: [*]u8, count: usize) usize {302pub fn read(fd: i32, buf: [*]u8, count: usize) usize {
300 return syscall3(.read, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count);303 return syscall3(.read, @bitCast(usize, @as(isize, fd)), @ptrToInt(buf), count);
301}304}
...@@ -1194,7 +1197,7 @@ pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout...@@ -1194,7 +1197,7 @@ pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout
1194 return epoll_pwait(epoll_fd, events, maxevents, timeout, null);1197 return epoll_pwait(epoll_fd, events, maxevents, timeout, null);
1195}1198}
11961199
1197pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize {1200pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*const sigset_t) usize {
1198 return syscall6(1201 return syscall6(
1199 .epoll_pwait,1202 .epoll_pwait,
1200 @bitCast(usize, @as(isize, epoll_fd)),1203 @bitCast(usize, @as(isize, epoll_fd)),