From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Liu Ping Fan <pingfank@linux.vnet.ibm.com>,
Liu Ping Fan <qemulist@gmail.com>
Subject: [Qemu-devel] [PULL 09/17] timer: make qemu_clock_enable sync between disable and timer's cb
Date: Thu, 17 Oct 2013 17:48:47 +0200 [thread overview]
Message-ID: <1382024935-28297-10-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1382024935-28297-1-git-send-email-pbonzini@redhat.com>
From: Liu Ping Fan <qemulist@gmail.com>
After disabling the QemuClock, we should make sure that no QemuTimers
are still in flight. To implement that with light overhead, we resort
to QemuEvent. The caller of disabling will wait on QemuEvent of each
timerlist.
Note, qemu_clock_enable(foo,false) can _not_ be called from timer's cb.
Also, the callers of qemu_clock_enable() should be protected by the BQL.
Signed-off-by: Liu Ping Fan <pingfank@linux.vnet.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
include/qemu/timer.h | 6 ++++++
qemu-timer.c | 23 ++++++++++++++++++++++-
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index 016e29a..1254ef7 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -189,6 +189,12 @@ void qemu_clock_notify(QEMUClockType type);
* @enabled: true to enable, false to disable
*
* Enable or disable a clock
+ * Disabling the clock will wait for related timerlists to stop
+ * executing qemu_run_timers. Thus, this functions should not
+ * be used from the callback of a timer that is based on @clock.
+ * Doing so would cause a deadlock.
+ *
+ * Caller should hold BQL.
*/
void qemu_clock_enable(QEMUClockType type, bool enabled);
diff --git a/qemu-timer.c b/qemu-timer.c
index 6b62e88..2b533da 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -45,6 +45,7 @@
/* timers */
typedef struct QEMUClock {
+ /* We rely on BQL to protect the timerlists */
QLIST_HEAD(, QEMUTimerList) timerlists;
NotifierList reset_notifiers;
@@ -71,6 +72,9 @@ struct QEMUTimerList {
QLIST_ENTRY(QEMUTimerList) list;
QEMUTimerListNotifyCB *notify_cb;
void *notify_opaque;
+
+ /* lightweight method to mark the end of timerlist's running */
+ QemuEvent timers_done_ev;
};
/**
@@ -99,6 +103,7 @@ QEMUTimerList *timerlist_new(QEMUClockType type,
QEMUClock *clock = qemu_clock_ptr(type);
timer_list = g_malloc0(sizeof(QEMUTimerList));
+ qemu_event_init(&timer_list->timers_done_ev, false);
timer_list->clock = clock;
timer_list->notify_cb = cb;
timer_list->notify_opaque = opaque;
@@ -143,13 +148,25 @@ void qemu_clock_notify(QEMUClockType type)
}
}
+/* Disabling the clock will wait for related timerlists to stop
+ * executing qemu_run_timers. Thus, this functions should not
+ * be used from the callback of a timer that is based on @clock.
+ * Doing so would cause a deadlock.
+ *
+ * Caller should hold BQL.
+ */
void qemu_clock_enable(QEMUClockType type, bool enabled)
{
QEMUClock *clock = qemu_clock_ptr(type);
+ QEMUTimerList *tl;
bool old = clock->enabled;
clock->enabled = enabled;
if (enabled && !old) {
qemu_clock_notify(type);
+ } else if (!enabled && old) {
+ QLIST_FOREACH(tl, &clock->timerlists, list) {
+ qemu_event_wait(&tl->timers_done_ev);
+ }
}
}
@@ -403,8 +420,9 @@ bool timerlist_run_timers(QEMUTimerList *timer_list)
QEMUTimerCB *cb;
void *opaque;
+ qemu_event_reset(&timer_list->timers_done_ev);
if (!timer_list->clock->enabled) {
- return progress;
+ goto out;
}
current_time = qemu_clock_get_ns(timer_list->clock->type);
@@ -428,6 +446,9 @@ bool timerlist_run_timers(QEMUTimerList *timer_list)
cb(opaque);
progress = true;
}
+
+out:
+ qemu_event_set(&timer_list->timers_done_ev);
return progress;
}
--
1.8.3.1
next prev parent reply other threads:[~2013-10-17 15:49 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-17 15:48 [Qemu-devel] [PULL 00/17] Memory/threading changes for 1.7 Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 01/17] memory: fix 128 arithmetic in info mtree Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 02/17] compatfd: switch to QemuThread Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 03/17] portio: Allow to mark portio lists as coalesced MMIO flushing Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 04/17] cirrus: Mark vga io region " Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 05/17] vga: Mark relevant portio lists regions " Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 06/17] seqlock: introduce read-write seqlock Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 07/17] timer: protect timers_state's clock with seqlock Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 08/17] qemu-thread: add QemuEvent Paolo Bonzini
2013-10-17 15:48 ` Paolo Bonzini [this message]
2013-10-17 15:48 ` [Qemu-devel] [PULL 10/17] timer: extract timer_mod_ns_locked and timerlist_rearm Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 11/17] timer: add timer_mod_anticipate and timer_mod_anticipate_ns Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 12/17] icount: use cpu_get_icount() directly Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 13/17] icount: reorganize icount_warp_rt Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 14/17] icount: prepare the code for future races in calling qemu_clock_warp Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 15/17] icount: document (future) locking rules for icount Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 16/17] icount: make it thread-safe Paolo Bonzini
2013-10-17 15:48 ` [Qemu-devel] [PULL 17/17] exec: remove qemu_safe_ram_ptr Paolo Bonzini
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=1382024935-28297-10-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=pingfank@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=qemulist@gmail.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 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).