Add BH_ThreadSleep, fix Timespec.h
This commit is contained in:
@@ -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.
|
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
|
=head2 BH_MutexNew
|
||||||
|
|
||||||
BH_Mutex *BH_MutexNew(void);
|
BH_Mutex *BH_MutexNew(void);
|
||||||
|
|||||||
@@ -57,6 +57,15 @@ I<callback> и данными I<data>.
|
|||||||
В случае успеха возвращает 0, иначе код ошибки.
|
В случае успеха возвращает 0, иначе код ошибки.
|
||||||
|
|
||||||
|
|
||||||
|
=head2 BH_ThreadSleep
|
||||||
|
|
||||||
|
void BH_ThreadSleep(uint32_t timeout)
|
||||||
|
|
||||||
|
Останавливает выполнения потока на указнный промежуток времени.
|
||||||
|
|
||||||
|
Параметр I<timeout> задаёт время ожидания в миллисекундах.
|
||||||
|
|
||||||
|
|
||||||
=head2 BH_MutexNew
|
=head2 BH_MutexNew
|
||||||
|
|
||||||
BH_Mutex *BH_MutexNew(void);
|
BH_Mutex *BH_MutexNew(void);
|
||||||
|
|||||||
@@ -27,6 +27,9 @@ int BH_ThreadJoin(BH_Thread *thread);
|
|||||||
int BH_ThreadDetach(BH_Thread *thread);
|
int BH_ThreadDetach(BH_Thread *thread);
|
||||||
|
|
||||||
|
|
||||||
|
void BH_ThreadSleep(uint32_t timeout);
|
||||||
|
|
||||||
|
|
||||||
BH_Mutex *BH_MutexNew(void);
|
BH_Mutex *BH_MutexNew(void);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,3 +27,9 @@ int BH_ThreadDetach(BH_Thread *thread)
|
|||||||
|
|
||||||
return BH_NOIMPL;
|
return BH_NOIMPL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void BH_ThreadSleep(uint32_t timeout)
|
||||||
|
{
|
||||||
|
BH_UNUSED(timeout);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
|
||||||
struct BH_ThreadContext
|
struct BH_ThreadContext
|
||||||
@@ -95,3 +96,21 @@ int BH_ThreadDetach(BH_Thread *thread)
|
|||||||
free(thread);
|
free(thread);
|
||||||
return BH_OK;
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <BH/Thread.h>
|
#include <BH/Thread.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
|
||||||
static int convertToTimespec(struct timespec *ts,
|
static int convertToTimespec(struct timespec *ts,
|
||||||
@@ -13,8 +14,8 @@ static int convertToTimespec(struct timespec *ts,
|
|||||||
{
|
{
|
||||||
#if (_POSIX_TIMERS > 0) || defined(BH_USE_CLOCK_GETTIME)
|
#if (_POSIX_TIMERS > 0) || defined(BH_USE_CLOCK_GETTIME)
|
||||||
if (clock_gettime(CLOCK_REALTIME, ts))
|
if (clock_gettime(CLOCK_REALTIME, ts))
|
||||||
return BH_ERROR;
|
#endif
|
||||||
#else
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
if (gettimeofday(&tv, NULL))
|
if (gettimeofday(&tv, NULL))
|
||||||
@@ -22,7 +23,7 @@ static int convertToTimespec(struct timespec *ts,
|
|||||||
|
|
||||||
ts->tv_sec = tv.tv_sec;
|
ts->tv_sec = tv.tv_sec;
|
||||||
ts->tv_nsec = tv.tv_usec * 1000;
|
ts->tv_nsec = tv.tv_usec * 1000;
|
||||||
#endif
|
}
|
||||||
|
|
||||||
/* Calculate absoulute time for timed wait */
|
/* Calculate absoulute time for timed wait */
|
||||||
ts->tv_sec += timeout / 1000;
|
ts->tv_sec += timeout / 1000;
|
||||||
|
|||||||
@@ -87,3 +87,9 @@ int BH_ThreadDetach(BH_Thread *thread)
|
|||||||
|
|
||||||
return BH_OK;
|
return BH_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void BH_ThreadSleep(uint32_t timeout)
|
||||||
|
{
|
||||||
|
Sleep(timeout);
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,18 @@
|
|||||||
#include <time.h>
|
#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_UNIT_TEST(Mutex)
|
||||||
{
|
{
|
||||||
BH_Mutex *mutex;
|
BH_Mutex *mutex;
|
||||||
@@ -102,6 +114,7 @@ int main(int argc, char **argv)
|
|||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
|
BH_UNIT_ADD(Sleep);
|
||||||
BH_UNIT_ADD(Mutex);
|
BH_UNIT_ADD(Mutex);
|
||||||
BH_UNIT_ADD(Semaphore);
|
BH_UNIT_ADD(Semaphore);
|
||||||
BH_UNIT_ADD(Condition);
|
BH_UNIT_ADD(Condition);
|
||||||
|
|||||||
Reference in New Issue
Block a user