From 5762484550ddf65c7d26515211534e200ec79536 Mon Sep 17 00:00:00 2001 From: Mikhail Romanko Date: Mon, 10 Jun 2024 22:39:24 +0300 Subject: Add thread sleep function --- include/bh/thread.h | 2 ++ src/thread_null.c | 5 +++++ src/thread_posix.c | 13 +++++++++++++ src/thread_win.c | 6 ++++++ 4 files changed, 26 insertions(+) diff --git a/include/bh/thread.h b/include/bh/thread.h index 3360c42..598f4f7 100644 --- a/include/bh/thread.h +++ b/include/bh/thread.h @@ -60,6 +60,8 @@ void bh_task_reuse(bh_task_t *task, int bh_task_done(bh_task_t *task); +int bh_thread_sleep(unsigned long timeout); + int bh_thread_join(bh_thread_t *thread); int bh_thread_detach(bh_thread_t *thread); diff --git a/src/thread_null.c b/src/thread_null.c index 212174a..6aaefc2 100644 --- a/src/thread_null.c +++ b/src/thread_null.c @@ -41,6 +41,11 @@ int bh_thread_init(bh_thread_t *thread, return BH_NO_IMPL; } +int bh_thread_sleep(unsigned long timeout) +{ + return BH_NO_IMPL; +} + /** * Joins the \a thread. * diff --git a/src/thread_posix.c b/src/thread_posix.c index 84e8e09..1122baf 100644 --- a/src/thread_posix.c +++ b/src/thread_posix.c @@ -55,6 +55,19 @@ bh_thread_t *bh_thread_new(bh_task_t *task) return result; } +int bh_thread_sleep(unsigned long timeout) +{ + struct timespec ts; + + ts.tv_sec = timeout / 1000; + ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000; + + if (nanosleep(&ts, NULL)) + return BH_ERROR; + + return BH_OK; +} + int bh_thread_join(bh_thread_t *thread) { /* Join the thread */ diff --git a/src/thread_win.c b/src/thread_win.c index 92a5bb4..3b78f46 100644 --- a/src/thread_win.c +++ b/src/thread_win.c @@ -79,6 +79,12 @@ bh_thread_t *bh_thread_new_base(bh_task_t *task, return result; } +int bh_thread_sleep(unsigned long timeout) +{ + Sleep(timeout); + return BH_OK; +} + int bh_thread_join(bh_thread_t *thread) { /* Join the thread */ -- cgit v1.2.3