Add BH_ThreadSleep, fix Timespec.h

This commit is contained in:
2025-09-21 21:20:31 +03:00
parent 8064ec2aff
commit 9bd2007023
8 changed files with 75 additions and 9 deletions

View File

@@ -27,3 +27,9 @@ int BH_ThreadDetach(BH_Thread *thread)
return BH_NOIMPL;
}
void BH_ThreadSleep(uint32_t timeout)
{
BH_UNUSED(timeout);
}

View File

@@ -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);
}

View File

@@ -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;
if (gettimeofday(&tv, NULL))
return BH_ERROR;
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
#endif
{
struct timeval tv;
if (gettimeofday(&tv, NULL))
return BH_ERROR;
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
}
/* Calculate absoulute time for timed wait */
ts->tv_sec += timeout / 1000;

View File

@@ -87,3 +87,9 @@ int BH_ThreadDetach(BH_Thread *thread)
return BH_OK;
}
void BH_ThreadSleep(uint32_t timeout)
{
Sleep(timeout);
}