Commit 9219cc984147de5c59d4a3f63c522f031b3a90fa
authorMichel Pollet <buserror@gmail.com>
Mon, 3 Mar 2014 13:14:13 +0000 (13:14 +0000)
committerMichel Pollet <buserror@gmail.com>
Mon, 3 Mar 2014 13:14:13 +0000 (13:14 +0000)
Cleanup the macros

Signed-off-by: Michel Pollet <buserror@gmail.com>
simavr/sim/sim_cycle_timers.c

index 6ca04623b7e7489fa0123a679d7c1fb5abc61ae9..1064dc5ded186dda6d50d896ed3d4a8f736bd6f1 100644 (file)
 #include "sim_time.h"
 #include "sim_cycle_timers.h"
 
-#define QUEUE(__q, __e) { (__e)->next = (__q); (__q) = __e; }
+#define QUEUE(__q, __e) { \
+               (__e)->next = (__q); \
+               (__q) = __e; \
+       }
 #define DETACH(__q, __l, __e) { \
-               if ((__l)) (__l)->next = (__e)->next; \
-               else (__q) = (__e)->next; \
+               if (__l) \
+                       (__l)->next = (__e)->next; \
+               else \
+                       (__q) = (__e)->next; \
        }
 #define INSERT(__q, __l, __e) { \
-               if ((__l)) (__e)->next = (__l)->next; \
-               else { (__e)->next = (__q); (__q) = (__e); } \
+               if (__l) { \
+                       (__e)->next = (__l)->next; \
+                       (__l)->next = (__e); \
+               } else { \
+                       (__e)->next = (__q); \
+                       (__q) = (__e); \
+               } \
        }
 
 void
@@ -174,19 +184,24 @@ avr_cycle_timer_process(
 
        do {
                avr_cycle_timer_slot_p t = pool->timer;
-               if (t->when > avr->cycle)
+               avr_cycle_count_t when = t->when;
+
+               if (when > avr->cycle)
                        return t->when - avr->cycle;
 
                // detach from active timers
                pool->timer = t->next;
                t->next = NULL;
-               avr_cycle_count_t when = t->when;
                do {
-                       when = t->timer(avr, when, t->param);
+                       avr_cycle_count_t w = t->timer(avr, when, t->param);
+                       // make sure the return value is either zero, or greater
+                       // than the last one to prevent infinite loop here
+                       when = w > when ? w : 0;
                } while (when && when <= avr->cycle);
-               if (when) {     // reschedule then
+               
+               if (when) // reschedule then
                        avr_cycle_timer_insert(avr, when - avr->cycle, t->timer, t->param);
-               }
+               
                // requeue this one into the free ones
                QUEUE(pool->timer_free, t);
        } while (pool->timer);