motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit 7e59f4ff69f9a8634c4e8d49cea95a3b55dbe771 (tree)
parent 1f2548ec5f19bf2e7ab66b0349f4953499897b10
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Wed,  6 Sep 2017 16:59:22 -0400

std: add os.sleep

Diffstat:
Mstd/os/index.zig | 22++++++++++++++++++++++
Mstd/os/linux.zig | 4++++
Mstd/os/linux_x86_64.zig | 4++--
3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/std/os/index.zig b/std/os/index.zig @@ -924,3 +924,25 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 { return result_buf[0..ret_val]; } } + +pub fn sleep(seconds: u64, nanoseconds: u64) { + var req = posix.timespec { + .tv_sec = seconds, + .tv_nsec = nanoseconds, + }; + var rem: posix.timespec = undefined; + while (true) { + const ret_val = posix.nanosleep(&req, &rem); + const err = posix.getErrno(ret_val); + if (err == 0) return; + switch (err) { + posix.EFAULT => unreachable, + posix.EINVAL => unreachable, + posix.EINTR => { + req = rem; + continue; + }, + else => return, + } + } +} diff --git a/std/os/linux.zig b/std/os/linux.zig @@ -459,6 +459,10 @@ pub fn waitpid(pid: i32, status: &i32, options: i32) -> usize { arch.syscall4(arch.SYS_wait4, usize(pid), @ptrToInt(status), @bitCast(usize, isize(options)), 0) } +pub fn nanosleep(req: &const timespec, rem: ?&timespec) -> usize { + arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)) +} + const NSIG = 65; const sigset_t = [128]u8; const all_mask = []u8 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }; diff --git a/std/os/linux_x86_64.zig b/std/os/linux_x86_64.zig @@ -476,6 +476,6 @@ pub const Stat = extern struct { }; pub const timespec = extern struct { - tv_sec: isize, - tv_nsec: isize, + tv_sec: usize, + tv_nsec: usize, };