From 5d14e92a2046e15668b8051a1a0c81a3a6704e2f Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Tue, 18 Jan 2022 13:12:45 -0600 Subject: [PATCH] added os.sleep --- core/os/os.onyx | 6 +++++- core/runtime/onyx_run.onyx | 2 ++ core/threads/thread.onyx | 3 +++ modules/onyx_runtime/onyx_runtime.c | 6 ++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core/os/os.onyx b/core/os/os.onyx index c5aacf79..7c5d59a8 100644 --- a/core/os/os.onyx +++ b/core/os/os.onyx @@ -43,4 +43,8 @@ list_directory :: (path: str) -> Iterator(DirectoryEntry) { exit :: (exitcode: i32) { runtime.__exit(exitcode); -} \ No newline at end of file +} + +#if #defined(runtime.__sleep) { + sleep :: runtime.__sleep +} diff --git a/core/runtime/onyx_run.onyx b/core/runtime/onyx_run.onyx index 13c3f429..6a1dcca1 100644 --- a/core/runtime/onyx_run.onyx +++ b/core/runtime/onyx_run.onyx @@ -30,6 +30,8 @@ __read_from_input :: (buffer: [] u8) -> i32 { __args_sizes_get :: (argc: ^i32, arg_buf_size: ^i32) -> void --- __exit :: (status: i32) -> void --- + + __sleep :: (milliseconds: i32) -> void --- } #export "_start" () { diff --git a/core/threads/thread.onyx b/core/threads/thread.onyx index a2c6a267..82f277a3 100644 --- a/core/threads/thread.onyx +++ b/core/threads/thread.onyx @@ -37,6 +37,9 @@ join :: (t: ^Thread) { while t.alive { #if runtime.Wait_Notify_Available { __atomic_wait(^t.id, t.id); + } else { + // To not completely kill the CPU. + runtime.__sleep(1); } } } diff --git a/modules/onyx_runtime/onyx_runtime.c b/modules/onyx_runtime/onyx_runtime.c index 0ddc9934..8ff8bc3d 100644 --- a/modules/onyx_runtime/onyx_runtime.c +++ b/modules/onyx_runtime/onyx_runtime.c @@ -780,6 +780,11 @@ ONYX_DEF(__exit, (WASM_I32), ()) { return NULL; } +ONYX_DEF(__sleep, (WASM_I32), ()) { + usleep(params->data[0].of.i32 * 1000); + return NULL; +} + // @@ -1006,6 +1011,7 @@ ONYX_LIBRARY { ONYX_FUNC(__args_sizes_get) ONYX_FUNC(__exit) + ONYX_FUNC(__sleep) ONYX_FUNC(__net_create_socket) ONYX_FUNC(__net_close_socket) -- 2.25.1