qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Anthony Liguori <aliguori@us.ibm.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	alex@alex.org.uk, rth@twiddle.net
Subject: [Qemu-devel] [PATCH 3/3] qemu-timer: add qemu_alarm_timer->timer_modified_lock
Date: Fri,  5 Jul 2013 14:39:46 +0200	[thread overview]
Message-ID: <1373027986-17868-4-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1373027986-17868-1-git-send-email-stefanha@redhat.com>

When a timer's expiration time is changed we may need to rearm the alarm
timer.  Rearming is necessary when the nearest deadline changes.

This patch introduces the qemu_alarm_timer->timer_modified boolean and a
lock to protect it.  It moves rearming the alarm timer from inside
qemu_mod_timer_ns() to qemu_run_all_timers() since we cannot rearm
outside the QEMU global mutex.

The following code is dropped because we always kick the main loop now:

  /* Interrupt execution to force deadline recalculation.  */
  qemu_clock_warp(ts->clock);
  if (use_icount) {
      qemu_notify_event();
  }

cpus.c will invoke qemu_clock_warp() again since the main loop ran.

It is now safe to call qemu_mod_timer_ns() for outside the QEMU global
mutex.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 qemu-timer.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/qemu-timer.c b/qemu-timer.c
index c773af0..9500d12 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -78,6 +78,10 @@ struct qemu_alarm_timer {
 #endif
     bool expired;
     bool pending;
+
+    /* Was the nearest deadline timer modified (possibly by another thread)? */
+    QemuMutex timer_modified_lock;
+    bool timer_modified;
 };
 
 static struct qemu_alarm_timer *alarm_timer;
@@ -371,14 +375,10 @@ void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
 
     /* Rearm if necessary  */
     if (pt == &ts->clock->active_timers) {
-        if (!alarm_timer->pending) {
-            qemu_rearm_alarm_timer(alarm_timer);
-        }
-        /* Interrupt execution to force deadline recalculation.  */
-        qemu_clock_warp(ts->clock);
-        if (use_icount) {
-            qemu_notify_event();
-        }
+        qemu_mutex_lock(&alarm_timer->timer_modified_lock);
+        alarm_timer->timer_modified = true;
+        qemu_mutex_unlock(&alarm_timer->timer_modified_lock);
+        qemu_notify_event();
     }
 }
 
@@ -484,6 +484,8 @@ uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts)
 
 void qemu_run_all_timers(void)
 {
+    bool timer_modified;
+
     alarm_timer->pending = false;
 
     /* vm time timers */
@@ -491,11 +493,20 @@ void qemu_run_all_timers(void)
     qemu_run_timers(rt_clock);
     qemu_run_timers(host_clock);
 
+    /* Check if qemu_mod_timer_ns() has been called */
+    qemu_mutex_lock(&alarm_timer->timer_modified_lock);
+    timer_modified = alarm_timer->timer_modified;
+    alarm_timer->timer_modified = false;
+    qemu_mutex_unlock(&alarm_timer->timer_modified_lock);
+
     /* rearm timer, if not periodic */
     if (alarm_timer->expired) {
         alarm_timer->expired = false;
         qemu_rearm_alarm_timer(alarm_timer);
+    } else if (timer_modified) {
+        qemu_rearm_alarm_timer(alarm_timer);
     }
+
 }
 
 #ifdef _WIN32
@@ -805,6 +816,8 @@ int init_timer_alarm(void)
         goto fail;
     }
 
+    qemu_mutex_init(&t->timer_modified_lock);
+
     atexit(quit_timers);
 #ifdef CONFIG_POSIX
     pthread_atfork(NULL, NULL, reinit_timers);
-- 
1.8.1.4

  parent reply	other threads:[~2013-07-05 12:49 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-05 12:39 [Qemu-devel] [PATCH 0/3] qemu-timer: make QEMUTimer functions thread-safe Stefan Hajnoczi
2013-07-05 12:39 ` [Qemu-devel] [PATCH 1/3] qemu-timer: drop outdated signal safety comments Stefan Hajnoczi
2013-07-05 17:52   ` Jan Kiszka
2013-07-05 12:39 ` [Qemu-devel] [PATCH 2/3] qemu-timer: add QEMUClock->active_timers list lock Stefan Hajnoczi
2013-07-05 13:01   ` Paolo Bonzini
2013-07-05 12:39 ` Stefan Hajnoczi [this message]
2013-07-05 17:51 ` [Qemu-devel] [PATCH 0/3] qemu-timer: make QEMUTimer functions thread-safe Jan Kiszka
2013-07-15 12:45   ` Paolo Bonzini
2013-07-15 12:57     ` Jan Kiszka
2013-07-15 13:38       ` Paolo Bonzini
2013-07-18  4:00 ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1373027986-17868-4-git-send-email-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=alex@alex.org.uk \
    --cc=aliguori@us.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).