Received: from eggs.gnu.org ([2001:4830:134:3::10]:41416) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWppM-00076f-QS for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VWppC-0000mo-4Z for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:32 -0400 Received: from mail-ea0-x235.google.com ([2a00:1450:4013:c01::235]:58633) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWppB-0000mX-TA for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:22 -0400 Received: by mail-ea0-f181.google.com with SMTP id d10so1189393eaj.12 for ; Thu, 17 Oct 2013 08:49:21 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Thu, 17 Oct 2013 17:48:47 +0200 Message-Id: <1382024935-28297-10-git-send-email-pbonzini@redhat.com> In-Reply-To: <1382024935-28297-1-git-send-email-pbonzini@redhat.com> References: <1382024935-28297-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PULL 09/17] timer: make qemu_clock_enable sync between disable and timer's cb List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Liu Ping Fan , Liu Ping Fan From: Liu Ping Fan 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 Signed-off-by: Paolo Bonzini --- 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