aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Romanko <me@blankhex.com>2025-09-21 21:20:31 +0300
committerMikhail Romanko <me@blankhex.com>2025-09-21 21:20:31 +0300
commit9bd200702311fdf3794e53c56c212d9ac31728ca (patch)
tree633ec9e4a39948d466f8eb8de394aac939ff2a38
parent8064ec2aff386e7bfaaa29c63bbd60f2ce38a6e6 (diff)
downloadbhlib-9bd200702311fdf3794e53c56c212d9ac31728ca.tar.gz
Add BH_ThreadSleep, fix Timespec.h
-rw-r--r--doc/Manual/en/BH_Thread.pod9
-rw-r--r--doc/Manual/ru/BH_Thread.pod9
-rw-r--r--include/BH/Thread.h3
-rw-r--r--src/Platform/Dummy/Thread.c6
-rw-r--r--src/Platform/Posix/Thread.c19
-rw-r--r--src/Platform/Posix/Timespec.h17
-rw-r--r--src/Platform/Win32/Thread.c6
-rw-r--r--test/src/TestThread.c13
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<thread> 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<timeout> 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<callback> и данными I<data>.
В случае успеха возвращает 0, иначе код ошибки.
+=head2 BH_ThreadSleep
+
+ void BH_ThreadSleep(uint32_t timeout)
+
+Останавливает выполнения потока на указнный промежуток времени.
+
+Параметр I<timeout> задаёт время ожидания в миллисекундах.
+
+
=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 <limits.h>
#include <stdlib.h>
+#include <errno.h>
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 <BH/Thread.h>
#include <sys/time.h>
#include <time.h>
+#include <unistd.h>
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 <time.h>
+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);