From ca237761bf0f9b16838e2d3f7079d1725e3bd87b Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Thu, 30 Jul 2020 19:22:40 -0700 Subject: 6686 --- 402time.mu | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 402time.mu (limited to '402time.mu') diff --git a/402time.mu b/402time.mu new file mode 100644 index 00000000..c95c4e14 --- /dev/null +++ b/402time.mu @@ -0,0 +1,39 @@ +type timespec { + tv_sec: int + tv_nsec: int +} + +# TODO: y2038 +fn time -> secs/eax: int { + var t: timespec + var clock/ebx: int <- copy 0 # CLOCK_MONOTONIC + var t-addr/ecx: (addr timespec) <- address t + syscall_clock_gettime + var t-secs-addr/ecx: (addr int) <- get t-addr, tv_sec + secs <- copy *t-secs-addr +} + +fn ntime -> nsecs/eax: int { + var t: timespec + var clock/ebx: int <- copy 0 # CLOCK_MONOTONIC + var t-addr/ecx: (addr timespec) <- address t + syscall_clock_gettime + var t-nsecs-addr/ecx: (addr int) <- get t-addr, tv_nsec + nsecs <- copy *t-nsecs-addr +} + +# nsecs must be less than 999999999 or 0x3b9ac9ff nanoseconds +fn sleep secs: int, nsecs: int { + var t: timespec + # initialize t + var tmp/eax: (addr int) <- get t, tv_sec + var tmp2/ecx: int <- copy secs + copy-to *tmp, tmp2 + tmp <- get t, tv_nsec + tmp2 <- copy nsecs + copy-to *tmp, tmp2 + # perform the syscall + var t-addr/ebx: (addr timespec) <- address t + var rem-addr/ecx: (addr timespec) <- copy 0 + syscall_nanosleep +} -- cgit 1.4.1-2-gfad0