alling mq_timedreceived with immediate timeout was getting stuck and not

timeout. Immediate timeout is achieved by setting absolute timeout value to
past time, for example abstime={ .tv_sec=0, .tv_nsec=0 }. However absolute
time was converted to relative time using unsigned integer arithmetic and
resulted large ticks count by clock_abstime2ticks, instead of expected negative
ticks value.

Patch corrects clock_abstime2ticks to return negative ticks, if absolute time
is in the past.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@haltian.com>
This commit is contained in:
Gregory Nutt 2014-11-19 09:25:00 -06:00
parent 8b8dd03e92
commit 8c28718bcb

View File

@ -1,7 +1,7 @@
/********************************************************************************
* sched/clock/clock_abstime2ticks.c
*
* Copyright (C) 2007, 2008, 2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2008, 2013-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -64,6 +64,32 @@
* Private Functions
********************************************************************************/
/********************************************************************************
* Name: compare_timespec
*
* Description:
* Return < 0 if time a is before time b
* Return > 0 if time b is before time a
* Return 0 if time a is the same as time b
*
********************************************************************************/
static long compare_timespec(FAR const struct timespec *a,
FAR const struct timespec *b)
{
if (a->tv_sec < b->tv_sec)
{
return -1;
}
if (a->tv_sec > b->tv_sec)
{
return 1;
}
return (long)a->tv_nsec -(long)b->tv_nsec;
}
/********************************************************************************
* Public Functions
********************************************************************************/
@ -100,11 +126,22 @@ int clock_abstime2ticks(clockid_t clockid, FAR const struct timespec *abstime,
*/
ret = clock_gettime(clockid, &currtime);
if (ret)
if (ret != OK)
{
return EINVAL;
}
if (compare_timespec(abstime, &currtime) < 0)
{
/* Every caller of clock_abstime2ticks check 'ticks < 0' to see if
* absolute time is in the past. So lets just return negative tick
* here.
*/
*ticks = -1;
return OK;
}
/* The relative time to wait is the absolute time minus the current time. */
reltime.tv_nsec = (abstime->tv_nsec - currtime.tv_nsec);