authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-06 16:59:22-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-06 16:59:22-04:00
log7e59f4ff69f9a8634c4e8d49cea95a3b55dbe771
tree36535ecb427b8ef23e884965703a7198bd7cd797
parent1f2548ec5f19bf2e7ab66b0349f4953499897b10

std: add os.sleep


3 files changed, 28 insertions(+), 2 deletions(-)

std/os/index.zig+22
......@@ -924,3 +924,25 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
924924 return result_buf[0..ret_val];
925925 }
926926}
927
928pub fn sleep(seconds: u64, nanoseconds: u64) {
929 var req = posix.timespec {
930 .tv_sec = seconds,
931 .tv_nsec = nanoseconds,
932 };
933 var rem: posix.timespec = undefined;
934 while (true) {
935 const ret_val = posix.nanosleep(&req, &rem);
936 const err = posix.getErrno(ret_val);
937 if (err == 0) return;
938 switch (err) {
939 posix.EFAULT => unreachable,
940 posix.EINVAL => unreachable,
941 posix.EINTR => {
942 req = rem;
943 continue;
944 },
945 else => return,
946 }
947 }
948}
std/os/linux.zig+4
......@@ -459,6 +459,10 @@ pub fn waitpid(pid: i32, status: &i32, options: i32) -> usize {
459459 arch.syscall4(arch.SYS_wait4, usize(pid), @ptrToInt(status), @bitCast(usize, isize(options)), 0)
460460}
461461
462pub fn nanosleep(req: &const timespec, rem: ?&timespec) -> usize {
463 arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem))
464}
465
462466const NSIG = 65;
463467const sigset_t = [128]u8;
464468const all_mask = []u8 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
std/os/linux_x86_64.zig+2-2
......@@ -476,6 +476,6 @@ pub const Stat = extern struct {
476476};
477477
478478pub const timespec = extern struct {
479 tv_sec: isize,
480 tv_nsec: isize,
479 tv_sec: usize,
480 tv_nsec: usize,
481481};