From 3d90a609a7813eb89b94a03ac3a98edfb7bcdced Mon Sep 17 00:00:00 2001
From: Christopher Haster <chaster@utexas.edu>
Date: Mon, 9 Jan 2017 19:45:09 -0600
Subject: [PATCH] events - Fixed overflow of timeout on STM32F4

For equeue_sema_wait, -1 is used to indicate an infinite wait.
This wasn't handled in the nonrtos implementation and caused
undefined/weird behaviour after an overflow on integer multiplication.

On most boards, the infinite wait would return after ~50 days, on the
STM32F4 the timeout killed all other timeouts for some reason.
---
 equeue/equeue_mbed.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/equeue/equeue_mbed.cpp b/equeue/equeue_mbed.cpp
index 122910a..af1f49d 100644
--- a/equeue/equeue_mbed.cpp
+++ b/equeue/equeue_mbed.cpp
@@ -110,7 +110,9 @@ static void equeue_sema_timeout(equeue_sema_t *s) {
 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
     int signal = 0;
     Timeout timeout;
-    timeout.attach_us(s, equeue_sema_timeout, ms*1000);
+    if (ms < 0) {
+        timeout.attach_us(Callback<void()>(equeue_sema_timeout, s), ms*1000);
+    }
 
     core_util_critical_section_enter();
     while (!*s) {