From patchwork Wed Sep 25 06:21:00 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 277680 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B2EB12C00A7 for ; Wed, 25 Sep 2013 16:22:26 +1000 (EST) Received: from localhost ([::1]:49677 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOiUS-0007Te-GB for incoming@patchwork.ozlabs.org; Wed, 25 Sep 2013 02:22:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41440) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOiTm-0007Ap-Tp for qemu-devel@nongnu.org; Wed, 25 Sep 2013 02:21:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOiTe-0003Y4-HH for qemu-devel@nongnu.org; Wed, 25 Sep 2013 02:21:42 -0400 Received: from mail-ob0-x22e.google.com ([2607:f8b0:4003:c01::22e]:58849) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOiTe-0003Xx-C8 for qemu-devel@nongnu.org; Wed, 25 Sep 2013 02:21:34 -0400 Received: by mail-ob0-f174.google.com with SMTP id uz6so6239673obc.33 for ; Tue, 24 Sep 2013 23:21:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4v4VeRBSZpGUbHNpkDaGmN4bugBiSMunkcvzmSf5YBQ=; b=A0A9X2cmp+yWQXk3NJHAPXfG/+A5izjDKjmJhAPTCsDjJ2ewwT99XrKq+jdMirk6GQ piPGliYfvdvZM2hoD3sMIEyEkeROkP3FxClS491/slkPwx857CWBarBMQ0pEpXg4q1Hq c+4wfmSJVUHCFg+WLnB/FQ4GqpiymnAdnuhG5Q1heB2rqq65sAvJ8rGdpRgNHDL7pBOv HeYQNRl63Sx+vRC0ZY/rs3SNUl5FuoxZ0SjypwUPLNopVAz3cOtWfljcqviEbnn7IWm0 2330X2hYsT5/4Sze1H6fOxczV7d32oF5150925FoEXWCi9pjwwC/M1C1P8ATPmEsyKgf pVvQ== X-Received: by 10.60.93.105 with SMTP id ct9mr5598970oeb.42.1380090093937; Tue, 24 Sep 2013 23:21:33 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPSA id d3sm34811970oek.5.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 24 Sep 2013 23:21:33 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 25 Sep 2013 14:21:00 +0800 Message-Id: <1380090060-6764-5-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1380090060-6764-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1380090060-6764-1-git-send-email-pingfank@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c01::22e Cc: Kevin Wolf , Paolo Bonzini , Alex Bligh , Stefan Hajnoczi , Jan Kiszka Subject: [Qemu-devel] [PATCH v5 4/4] timer: make qemu_clock_enable sync between disable and timer's cb X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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. And the callers of qemu_clock_enable() should be sync by themselves, not protected by this patch. Signed-off-by: Liu Ping Fan --- include/qemu/timer.h | 6 ++++++ qemu-timer.c | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/include/qemu/timer.h b/include/qemu/timer.h index bb1de23..c7fa83a 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -185,6 +185,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 95ff47f..3a828aa 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; @@ -70,6 +71,8 @@ struct QEMUTimerList { QLIST_ENTRY(QEMUTimerList) list; QEMUTimerListNotifyCB *notify_cb; void *notify_opaque; + /* light weight method to mark the end of timerlist's running */ + QemuEvent timers_done_ev; }; /** @@ -98,6 +101,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; @@ -140,13 +144,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); + } } } @@ -373,8 +389,10 @@ bool timerlist_run_timers(QEMUTimerList *timer_list) QEMUTimer *ts; int64_t current_time; bool progress = false; - + + qemu_event_reset(&timer_list->timers_done_ev); if (!timer_list->clock->enabled) { + qemu_event_set(&timer_list->timers_done_ev); return progress; } @@ -392,6 +410,7 @@ bool timerlist_run_timers(QEMUTimerList *timer_list) ts->cb(ts->opaque); progress = true; } + qemu_event_set(&timer_list->timers_done_ev); return progress; }