[PATCH] spring: Avoid looping if time appears to be going backwards

Rob Bradford rob at robster.org.uk
Fri Aug 3 09:02:04 PDT 2012


From: Rob Bradford <rob at linux.intel.com>

Since the time values are unsigned integers we can check whether the msec is
smaller than spring->timestamp by checking if the subtraction overflows into a
value greater than half the maximum unsigned integer range (ie. top bit set)
---
 src/util.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/util.c b/src/util.c
index cb03dde..fa360c2 100644
--- a/src/util.c
+++ b/src/util.c
@@ -46,6 +46,14 @@ weston_spring_update(struct weston_spring *spring, uint32_t msec)
 {
 	double force, v, current, step;
 
+	/* Avoid entering into an infinite loop */
+	if (msec - spring->timestamp > UINT32_MAX / 2) {
+		weston_log("timestamps going backwards (from %u to %u)\n",
+				spring->timestamp, msec);
+		spring->current = spring->previous = spring->target;
+		return;
+	}
+
 	step = 0.01;
 	while (4 < msec - spring->timestamp) {
 		current = spring->current;
-- 
1.7.11.2



More information about the wayland-devel mailing list