All of lore.kernel.org
 help / color / mirror / Atom feed
From: Liu Ping Fan <qemulist@gmail.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Jan Kiszka <jan.kiszka@siemens.com>,
	Alex Bligh <alex@alex.org.uk>,
	Anthony Liguori <anthony@codemonkey.ws>,
	Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb
Date: Mon, 29 Jul 2013 11:16:06 +0800	[thread overview]
Message-ID: <1375067768-11342-4-git-send-email-pingfank@linux.vnet.ibm.com> (raw)
In-Reply-To: <1375067768-11342-1-git-send-email-pingfank@linux.vnet.ibm.com>

After disabling the QemuClock, we should make sure that no QemuTimers
are still in flight. To implement that, the caller of disabling will
wait until the last user's exit.

Note, the callers of qemu_clock_enable() should be sync by themselves,
not protected by this patch.

Signed-off-by: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
---
 qemu-timer.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)

diff --git a/qemu-timer.c b/qemu-timer.c
index 5a42035..d941a83 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -60,6 +60,14 @@ struct QEMUClock {
 
     int type;
     bool enabled;
+
+    /* rule: Innermost lock
+     * to protect the disable against timer's cb in flight.
+     */
+    QemuMutex lock;
+    /* reference by timer list */
+    int using;
+    QemuCond wait_using;
 };
 
 struct QEMUTimer {
@@ -274,6 +282,9 @@ static QEMUClock *qemu_new_clock(int type)
     clock->type = type;
     clock->enabled = true;
     clock->last = INT64_MIN;
+    clock->using = 0;
+    qemu_cond_init(&clock->wait_using);
+    qemu_mutex_init(&clock->lock);
     notifier_list_init(&clock->reset_notifiers);
     tlist = clock_to_timerlist(clock);
     timer_list_init(tlist);
@@ -287,6 +298,13 @@ void qemu_clock_enable(QEMUClock *clock, bool enabled)
     clock->enabled = enabled;
     if (enabled && !old) {
         qemu_rearm_alarm_timer(alarm_timer);
+    } else {
+        qemu_mutex_lock(&clock->lock);
+        /* Wait for cb in flight to terminate */
+        while (atomic_read(clock->using)) {
+            qemu_cond_wait(&clock->wait_using, &clock->lock);
+        }
+        qemu_mutex_unlock(&clock->lock);
     }
 }
 
@@ -440,8 +458,11 @@ void qemu_run_timers(QEMUClock *clock)
     int64_t current_time;
     TimerList *tlist;
 
-    if (!clock->enabled)
-        return;
+    atomic_inc(&clock->using);
+    if (unlikely(!clock->enabled)) {
+        goto exit;
+    }
+
 
     current_time = qemu_get_clock_ns(clock);
     tlist = clock_to_timerlist(clock);
@@ -461,6 +482,15 @@ void qemu_run_timers(QEMUClock *clock)
         /* run the callback (the timer list can be modified) */
         ts->cb(ts->opaque);
     }
+
+exit:
+    qemu_mutex_lock(&clock->lock);
+    if (atomic_fetch_dec(&clock->using) == 1) {
+        if (unlikely(!clock->enabled)) {
+            qemu_cond_signal(&clock->wait_using);
+        }
+    }
+    qemu_mutex_unlock(&clock->lock);
 }
 
 int64_t qemu_get_clock_ns(QEMUClock *clock)
-- 
1.8.1.4

  parent reply	other threads:[~2013-07-29  3:18 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-29  3:16 [Qemu-devel] [RFC v2 0/5] arm AioContext with its own timer stuff Liu Ping Fan
2013-07-29  3:16 ` [Qemu-devel] [RFC v2 1/5] timer: protect timers_state with lock Liu Ping Fan
2013-07-29  6:26   ` Paolo Bonzini
2013-07-29  8:01     ` liu ping fan
2013-07-29  3:16 ` [Qemu-devel] [RFC v2 2/5] timer: pick out timer list info from QemuClock Liu Ping Fan
2013-07-29  3:16 ` Liu Ping Fan [this message]
2013-07-29  6:30   ` [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb Paolo Bonzini
2013-07-29  8:10     ` liu ping fan
2013-07-29 11:21       ` Paolo Bonzini
2013-07-30  2:42         ` liu ping fan
2013-07-30  9:17           ` Paolo Bonzini
2013-07-30  9:51             ` Alex Bligh
2013-07-30 10:12               ` Paolo Bonzini
2013-08-01  5:54             ` liu ping fan
2013-08-01  8:57               ` Paolo Bonzini
2013-08-01  9:35                 ` Alex Bligh
2013-08-01 12:19                   ` Paolo Bonzini
2013-08-01 13:28                     ` Alex Bligh
2013-08-01 13:51                       ` Paolo Bonzini
2013-08-01 14:20                         ` Alex Bligh
2013-08-01 14:28                           ` Paolo Bonzini
2013-08-02  3:31                             ` liu ping fan
2013-08-02 10:01                               ` Paolo Bonzini
2013-08-02  3:33                       ` liu ping fan
2013-08-02 14:43                         ` Stefan Hajnoczi
2013-08-05  2:13                           ` liu ping fan
2013-07-29  3:16 ` [Qemu-devel] [RFC v2 4/5] timer: associate three timerlists with AioContext Liu Ping Fan
2013-07-29  6:32   ` Paolo Bonzini
2013-07-29  8:20     ` liu ping fan
2013-07-29 13:11       ` Paolo Bonzini
2013-07-30  2:35         ` liu ping fan
2013-07-29  3:16 ` [Qemu-devel] [RFC v2 5/5] timer: run timers on aio_poll Liu Ping Fan
2013-07-29  9:22 ` [Qemu-devel] [RFC v2 0/5] arm AioContext with its own timer stuff Stefan Hajnoczi
2013-07-29 10:23   ` Alex Bligh
2013-07-29 13:36     ` Stefan Hajnoczi
2013-07-29 13:56       ` Alex Bligh
2013-07-30  3:35   ` liu ping fan
2013-07-29 10:18 ` Alex Bligh

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=1375067768-11342-4-git-send-email-pingfank@linux.vnet.ibm.com \
    --to=qemulist@gmail.com \
    --cc=alex@alex.org.uk \
    --cc=anthony@codemonkey.ws \
    --cc=jan.kiszka@siemens.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.