summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-11-19 09:25:00 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-11-19 09:25:00 -0600
commit1b34ed125d15814841d928499da8775355635e22 (patch)
tree34a9c15c98bb3ac25357b764617f1c2171a0f43e
parent1b48f56ed6bf41797ad2ed66f84703bbbce09828 (diff)
downloadnuttx-1b34ed125d15814841d928499da8775355635e22.tar.gz
nuttx-1b34ed125d15814841d928499da8775355635e22.tar.bz2
nuttx-1b34ed125d15814841d928499da8775355635e22.zip
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>
-rw-r--r--nuttx/sched/clock/clock_abstime2ticks.c41
1 files changed, 39 insertions, 2 deletions
diff --git a/nuttx/sched/clock/clock_abstime2ticks.c b/nuttx/sched/clock/clock_abstime2ticks.c
index 1f8845d05..46b108685 100644
--- a/nuttx/sched/clock/clock_abstime2ticks.c
+++ b/nuttx/sched/clock/clock_abstime2ticks.c
@@ -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
@@ -65,6 +65,32 @@
********************************************************************************/
/********************************************************************************
+ * 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);