fix: 修改DoNanoSleep 以纳秒为单位
DoNanoSleep 接口以微秒为单位,纳秒级别的在转换成微秒时被整除为0, 导致转换成tick时为0,导致延时时触发yield,导致延时时间超大 Close #I3Z9DP Signed-off-by: zhushengle <zhushengle@huawei.com> Change-Id: Ib662fdc80707be6040b2bb06a1b457344bd48b30
This commit is contained in:
parent
9ec208a273
commit
6917e08431
|
@ -950,11 +950,11 @@ int timer_getoverrun(timer_t timerID)
|
|||
return (overRun > DELAYTIMER_MAX) ? DELAYTIMER_MAX : overRun;
|
||||
}
|
||||
|
||||
STATIC INT32 DoNanoSleep(UINT64 useconds)
|
||||
STATIC INT32 DoNanoSleep(UINT64 nanoseconds)
|
||||
{
|
||||
UINT32 ret;
|
||||
|
||||
ret = LOS_TaskDelay(OsUS2Tick(useconds));
|
||||
ret = LOS_TaskDelay(OsNS2Tick(nanoseconds));
|
||||
if (ret == LOS_OK || ret == LOS_ERRNO_TSK_YIELD_NOT_ENOUGH_TASK) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -963,12 +963,12 @@ STATIC INT32 DoNanoSleep(UINT64 useconds)
|
|||
|
||||
int usleep(unsigned useconds)
|
||||
{
|
||||
return DoNanoSleep((UINT64)useconds);
|
||||
return DoNanoSleep((UINT64)useconds * OS_SYS_NS_PER_US);
|
||||
}
|
||||
|
||||
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
||||
{
|
||||
UINT64 useconds;
|
||||
UINT64 nanoseconds;
|
||||
INT32 ret = -1;
|
||||
|
||||
(VOID)rmtp;
|
||||
|
@ -979,14 +979,14 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
|
|||
return ret;
|
||||
}
|
||||
|
||||
useconds = (UINT64)rqtp->tv_sec * OS_SYS_US_PER_SECOND + rqtp->tv_nsec / OS_SYS_NS_PER_US;
|
||||
nanoseconds = (UINT64)rqtp->tv_sec * OS_SYS_NS_PER_SECOND + rqtp->tv_nsec;
|
||||
|
||||
return DoNanoSleep(useconds);
|
||||
return DoNanoSleep(nanoseconds);
|
||||
}
|
||||
|
||||
unsigned int sleep(unsigned int seconds)
|
||||
{
|
||||
return DoNanoSleep((UINT64)seconds * OS_SYS_US_PER_SECOND);
|
||||
return DoNanoSleep((UINT64)seconds * OS_SYS_NS_PER_SECOND);
|
||||
}
|
||||
|
||||
double difftime(time_t time2, time_t time1)
|
||||
|
|
|
@ -59,11 +59,11 @@ LITE_OS_SEC_TEXT_MINOR UINT32 LOS_Tick2MS(UINT32 tick)
|
|||
return ((UINT64)tick * OS_SYS_MS_PER_SECOND) / LOSCFG_BASE_CORE_TICK_PER_SECOND;
|
||||
}
|
||||
|
||||
LITE_OS_SEC_TEXT_MINOR UINT32 OsUS2Tick(UINT64 microsec)
|
||||
LITE_OS_SEC_TEXT_MINOR UINT32 OsNS2Tick(UINT64 nanoseconds)
|
||||
{
|
||||
const UINT32 usPerTick = OS_SYS_US_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
|
||||
const UINT32 nsPerTick = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
|
||||
|
||||
UINT64 ticks = (microsec + usPerTick - 1) / usPerTick;
|
||||
UINT64 ticks = (nanoseconds + nsPerTick - 1) / nsPerTick;
|
||||
if (ticks > OS_MAX_VALUE) {
|
||||
ticks = OS_MAX_VALUE;
|
||||
}
|
||||
|
|
|
@ -121,9 +121,9 @@ extern "C" {
|
|||
|
||||
/**
|
||||
* @ingroup los_sys
|
||||
* Convert microseconds to Ticks.
|
||||
* Convert nanoseconds to Ticks.
|
||||
*/
|
||||
extern UINT32 OsUS2Tick(UINT64 microsec);
|
||||
extern UINT32 OsNS2Tick(UINT64 nanoseconds);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
|
|
|
@ -647,7 +647,7 @@ INT32 OsFutexWait(const UINT32 *userVaddr, UINT32 flags, UINT32 val, UINT32 absT
|
|||
return ret;
|
||||
}
|
||||
if (absTime != LOS_WAIT_FOREVER) {
|
||||
timeOut = OsUS2Tick(absTime);
|
||||
timeOut = OsNS2Tick((UINT64)absTime * OS_SYS_NS_PER_US);
|
||||
}
|
||||
|
||||
return OsFutexWaitTask(userVaddr, flags, val, timeOut);
|
||||
|
|
|
@ -64,12 +64,12 @@ static int SleepTest(int64_t expectTime)
|
|||
LogPrintln("slept time (expected --> actual): %" PRId64 "ns --> %" PRId64 "ns, delta: %" PRId64 "ns\n", expectTime,
|
||||
escapeTime, escapeTime - expectTime);
|
||||
|
||||
g_failCnt += (escapeTime < expectTime || (escapeTime - expectTime) >= 20000000); // 20000000, 2 ticks.
|
||||
g_failCnt += (escapeTime < expectTime);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ClockTest(void)
|
||||
static void *ClockTestThread(void *arg)
|
||||
{
|
||||
(void)SleepTest(0);
|
||||
(void)SleepTest(2); // 2, ns.
|
||||
|
@ -84,6 +84,27 @@ static int ClockTest(void)
|
|||
(void)SleepTest(10e6 + 1); // 10ms+1ns
|
||||
(void)SleepTest(25e6); // 25ms
|
||||
(void)SleepTest(1e9); // 1s
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int ClockTest(void)
|
||||
{
|
||||
int ret;
|
||||
pthread_t thread;
|
||||
struct sched_param param = { 0 };
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
|
||||
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
|
||||
pthread_attr_setschedparam(&attr, ¶m);
|
||||
|
||||
ret = pthread_create(&thread, &attr, ClockTestThread, NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ret = pthread_join(thread, NULL);
|
||||
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
|
||||
|
||||
ICUNIT_ASSERT_EQUAL(g_failCnt, 0, g_failCnt);
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue