diff options
Diffstat (limited to 'src/Platform')
| -rw-r--r-- | src/Platform/Dummy/Thread.c | 6 | ||||
| -rw-r--r-- | src/Platform/Posix/Thread.c | 19 | ||||
| -rw-r--r-- | src/Platform/Posix/Timespec.h | 17 | ||||
| -rw-r--r-- | src/Platform/Win32/Thread.c | 6 |
4 files changed, 40 insertions, 8 deletions
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); +} |
