From 715635aaa7289f9aeb0a6877d894bc28cb55a3bc Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 2 Feb 2025 17:04:58 +0100 Subject: [PATCH] Added time.sleep function --- examples/thread/thread.q | 2 ++ lib/sys/sys_linux.q | 4 ++-- lib/time/time.q | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/time/time.q diff --git a/examples/thread/thread.q b/examples/thread/thread.q index 350168c..929ebbb 100644 --- a/examples/thread/thread.q +++ b/examples/thread/thread.q @@ -1,5 +1,6 @@ import sys import thread +import time main() { thread.create(work) @@ -10,6 +11,7 @@ main() { work() { sys.write(1, "[ ] start\n", 10) + time.sleep(10 * 1000 * 1000) sys.write(1, "[x] end\n", 8) sys.exit(0) } \ No newline at end of file diff --git a/lib/sys/sys_linux.q b/lib/sys/sys_linux.q index 73fcb76..c1207f7 100644 --- a/lib/sys/sys_linux.q +++ b/lib/sys/sys_linux.q @@ -22,8 +22,8 @@ munmap(address Pointer, length Int) -> Int { return syscall(11, address, length) } -nanosleep(requested Pointer, remaining Pointer) -> Int { - return syscall(35, requested, remaining) +nanosleep(timespec Pointer) -> Int { + return syscall(35, timespec, 0) } clone(flags Int, stack Pointer) -> Int { diff --git a/lib/time/time.q b/lib/time/time.q new file mode 100644 index 0000000..0d2d749 --- /dev/null +++ b/lib/time/time.q @@ -0,0 +1,21 @@ +import mem +import sys + +sleep(nanoseconds Int) { + seconds := 0 + + loop { + if nanoseconds >= 1000000000 { + nanoseconds = nanoseconds - 1000000000 + seconds += 1 + } else { + timespec := mem.alloc(16) + store(timespec, 8, seconds) + offset := timespec + 8 + store(offset, 8, nanoseconds) + sys.nanosleep(timespec) + mem.free(timespec, 16) + return + } + } +} \ No newline at end of file