From 9bd200702311fdf3794e53c56c212d9ac31728ca Mon Sep 17 00:00:00 2001 From: Mikhail Romanko Date: Sun, 21 Sep 2025 21:20:31 +0300 Subject: Add BH_ThreadSleep, fix Timespec.h --- doc/Manual/en/BH_Thread.pod | 9 +++++++++ doc/Manual/ru/BH_Thread.pod | 9 +++++++++ include/BH/Thread.h | 3 +++ src/Platform/Dummy/Thread.c | 6 ++++++ src/Platform/Posix/Thread.c | 19 +++++++++++++++++++ src/Platform/Posix/Timespec.h | 17 +++++++++-------- src/Platform/Win32/Thread.c | 6 ++++++ test/src/TestThread.c | 13 +++++++++++++ 8 files changed, 74 insertions(+), 8 deletions(-) diff --git a/doc/Manual/en/BH_Thread.pod b/doc/Manual/en/BH_Thread.pod index 2e28fda..7520c82 100644 --- a/doc/Manual/en/BH_Thread.pod +++ b/doc/Manual/en/BH_Thread.pod @@ -58,6 +58,15 @@ Upon completion of the thread, the resources of the I are freed. Returns 0 on success, otherwise an error code. +=head2 BH_ThreadSleep + + void BH_ThreadSleep(uint32_t timeout) + +Makes thread sleep for the specified amount of time. + +The I parameter specifies the waiting time in milliseconds. + + =head2 BH_MutexNew BH_Mutex *BH_MutexNew(void); diff --git a/doc/Manual/ru/BH_Thread.pod b/doc/Manual/ru/BH_Thread.pod index 5016ea8..3a2b2e5 100644 --- a/doc/Manual/ru/BH_Thread.pod +++ b/doc/Manual/ru/BH_Thread.pod @@ -57,6 +57,15 @@ I и данными I. В случае успеха возвращает 0, иначе код ошибки. +=head2 BH_ThreadSleep + + void BH_ThreadSleep(uint32_t timeout) + +Останавливает выполнения потока на указнный промежуток времени. + +Параметр I задаёт время ожидания в миллисекундах. + + =head2 BH_MutexNew BH_Mutex *BH_MutexNew(void); diff --git a/include/BH/Thread.h b/include/BH/Thread.h index a28f997..cffd3be 100644 --- a/include/BH/Thread.h +++ b/include/BH/Thread.h @@ -27,6 +27,9 @@ int BH_ThreadJoin(BH_Thread *thread); int BH_ThreadDetach(BH_Thread *thread); +void BH_ThreadSleep(uint32_t timeout); + + BH_Mutex *BH_MutexNew(void); diff --git a/src/Platform/Dummy/Thread.c b/src/Platform/Dummy/Thread.c index 7463269..ef8fad3 100644 --- a/src/Platform/Dummy/Thread.c +++ b/src/Platform/Dummy/Thread.c @@ -27,3 +27,9 @@ int BH_ThreadDetach(BH_Thread *thread) return BH_NOIMPL; } + + +void BH_ThreadSleep(uint32_t timeout) +{ + BH_UNUSED(timeout); +} diff --git a/src/Platform/Posix/Thread.c b/src/Platform/Posix/Thread.c index 2c249d8..21f14a9 100644 --- a/src/Platform/Posix/Thread.c +++ b/src/Platform/Posix/Thread.c @@ -2,6 +2,7 @@ #include #include +#include struct BH_ThreadContext @@ -95,3 +96,21 @@ int BH_ThreadDetach(BH_Thread *thread) free(thread); return BH_OK; } + + +void BH_ThreadSleep(uint32_t timeout) +{ + struct timespec ts; + int result; + + /* We don't care about nanoseconds */ + ts.tv_sec = timeout / 1000; + ts.tv_nsec = (timeout % 1000) * 1000000; + + do + { + result = nanosleep(&ts, &ts); + if (errno != EINTR) + break; + } while (result); +} diff --git a/src/Platform/Posix/Timespec.h b/src/Platform/Posix/Timespec.h index 2ed157a..95b79f2 100644 --- a/src/Platform/Posix/Timespec.h +++ b/src/Platform/Posix/Timespec.h @@ -6,6 +6,7 @@ #include #include #include +#include static int convertToTimespec(struct timespec *ts, @@ -13,16 +14,16 @@ static int convertToTimespec(struct timespec *ts, { #if (_POSIX_TIMERS > 0) || defined(BH_USE_CLOCK_GETTIME) if (clock_gettime(CLOCK_REALTIME, ts)) - return BH_ERROR; -#else - struct timeval tv; +#endif + { + struct timeval tv; - if (gettimeofday(&tv, NULL)) - return BH_ERROR; + if (gettimeofday(&tv, NULL)) + return BH_ERROR; - ts->tv_sec = tv.tv_sec; - ts->tv_nsec = tv.tv_usec * 1000; -#endif + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; + } /* Calculate absoulute time for timed wait */ ts->tv_sec += timeout / 1000; diff --git a/src/Platform/Win32/Thread.c b/src/Platform/Win32/Thread.c index 88b8906..54f109e 100644 --- a/src/Platform/Win32/Thread.c +++ b/src/Platform/Win32/Thread.c @@ -87,3 +87,9 @@ int BH_ThreadDetach(BH_Thread *thread) return BH_OK; } + + +void BH_ThreadSleep(uint32_t timeout) +{ + Sleep(timeout); +} diff --git a/test/src/TestThread.c b/test/src/TestThread.c index f2a59c2..c530703 100644 --- a/test/src/TestThread.c +++ b/test/src/TestThread.c @@ -3,6 +3,18 @@ #include +BH_UNIT_TEST(Sleep) +{ + time_t start; + + start = time(NULL); + BH_ThreadSleep(5000); + BH_VERIFY(time(NULL) - start >= 5); + + return 0; +} + + BH_UNIT_TEST(Mutex) { BH_Mutex *mutex; @@ -102,6 +114,7 @@ int main(int argc, char **argv) (void)argc; (void)argv; + BH_UNIT_ADD(Sleep); BH_UNIT_ADD(Mutex); BH_UNIT_ADD(Semaphore); BH_UNIT_ADD(Condition); -- cgit v1.2.3