Fix windows event loop handling.

This change fixes the windows emulator binary which got stuck
due to changes in the timer implementation that occured during
the last upstream integration.

Change-Id: I090ce4ac9c09b3b95e0df95513446922120b2d9a
diff --git a/qemu-timer.c b/qemu-timer.c
index 402f567..0ed5ba9 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -589,7 +589,7 @@
 {
     QEMUTimer **ptimer_head, *ts;
     int64_t current_time;
-   
+
     if (!clock->enabled)
         return;
 
@@ -743,6 +743,15 @@
     qemu_run_timers(host_clock);
 }
 
+static int timer_alarm_pending = 1;
+
+int qemu_timer_alarm_pending(void)
+{
+    int ret = timer_alarm_pending;
+    timer_alarm_pending = 0;
+    return ret;
+}
+
 #ifdef _WIN32
 static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
                                         DWORD_PTR dwUser, DWORD_PTR dw1,
@@ -795,6 +804,7 @@
 
         t->expired = alarm_has_dynticks(t);
         t->pending = 1;
+        timer_alarm_pending = 1;
         qemu_notify_event();
     }
 }
@@ -961,7 +971,7 @@
 
     sigaction(SIGALRM, &act, NULL);
 
-    /* 
+    /*
      * Initialize ev struct to 0 to avoid valgrind complaining
      * about uninitialized data in timer_create call
      */
@@ -1181,6 +1191,8 @@
     t->stop(t);
 }
 
+extern int tcg_has_work(void);
+
 int qemu_calculate_timeout(void)
 {
 #ifndef CONFIG_IOTHREAD
@@ -1188,7 +1200,24 @@
 
     if (!vm_running)
         timeout = 5000;
-    else {
+    else if (tcg_has_work())
+        timeout = 0;
+    else if (!use_icount) {
+#ifdef WIN32
+        /* This corresponds to the case where the emulated system is
+         * totally idle and waiting for i/o. The problem is that on
+         * Windows, the default value will prevent Windows user events
+         * to be delivered in less than 5 seconds.
+         *
+         * Upstream contains a different way to handle this, for now
+         * this hack should be sufficient until we integrate it into
+         * our tree.
+         */
+        timeout = 1000/15;  /* deliver user events every 15/th of second */
+#else
+        timeout = 5000;
+#endif
+    } else {
      /* XXX: use timeout computed from timers */
         int64_t add;
         int64_t delta;