From patchwork Wed Oct 31 15:30:50 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 195975 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DC9562C0091 for ; Thu, 1 Nov 2012 04:52:39 +1100 (EST) Received: from localhost ([::1]:33682 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTaIN-0001OE-OT for incoming@patchwork.ozlabs.org; Wed, 31 Oct 2012 11:33:31 -0400 Received: from eggs.gnu.org ([208.118.235.92]:46892) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTaIC-0001Cg-FI for qemu-devel@nongnu.org; Wed, 31 Oct 2012 11:33:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TTaI8-0005mf-C5 for qemu-devel@nongnu.org; Wed, 31 Oct 2012 11:33:20 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:41123) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TTaI8-0004ui-6M for qemu-devel@nongnu.org; Wed, 31 Oct 2012 11:33:16 -0400 Received: by mail-pb0-f45.google.com with SMTP id rp2so1021969pbb.4 for ; Wed, 31 Oct 2012 08:33:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=sQdYUQKKqxiCO6MfSIHdGGsHTIUtBgEzJkMPgleppvg=; b=Q9T+jxDrELR9bP7z45AeKq4KPnNWKR1/1JsS6JBt7XFnGaI9Tr0Vcu7DGrV22f5jg0 gOpLuPj3HxKyvaB0godaDyaceKZCKplPQ+Brf48PnHl8dMt2nLBSW2hcTgizsz6S15Hb 0hWYwBjDFrfd7efDp4WoyBUbotPgD9fvO58Krx0p+uD25Jmvgi8wpHmQudCj6kA0bSRw 1f8i71ZUGwhwuK8FCnonEtHlKWgmMIWZ9ZuMA9Y9ygJEIuYPVuVd3aom6pF2yd8aPTIb pIqYP1BltcHiINnA0bC0Jn8wVzVtSm15sh+1dIELbWMYPwXGv1MYh/W7TD+7f2yZ2d6/ H9Rg== Received: by 10.68.204.103 with SMTP id kx7mr5631305pbc.33.1351697595849; Wed, 31 Oct 2012 08:33:15 -0700 (PDT) Received: from yakj.usersys.redhat.com (93-34-169-1.ip50.fastwebnet.it. [93.34.169.1]) by mx.google.com with ESMTPS id sz6sm2445230pbc.52.2012.10.31.08.33.13 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 31 Oct 2012 08:33:14 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 31 Oct 2012 16:30:50 +0100 Message-Id: <1351697456-16107-34-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.12.1 In-Reply-To: <1351697456-16107-1-git-send-email-pbonzini@redhat.com> References: <1351697456-16107-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.160.45 Cc: aliguori@us.ibm.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH v2 33/39] threadpool: do not take lock in event_notifier_ready 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 The ordering is: worker thread consumer thread ------------------------------------------------------------------- write ret event_notifier_test_and_clear wmb() read state write state rmb() event_notifier_set read ret Signed-off-by: Paolo Bonzini --- New patch. thread-pool.c | 19 +++++++++++++------ 1 file modificato, 13 inserzioni(+), 6 rimozioni(-) diff --git a/thread-pool.c b/thread-pool.c index 80749b7..651b324 100644 --- a/thread-pool.c +++ b/thread-pool.c @@ -39,6 +39,11 @@ struct ThreadPoolElement { BlockDriverAIOCB common; ThreadPoolFunc *func; void *arg; + + /* Moving state out of THREAD_QUEUED is protected by lock. After + * that, only the worker thread can write to it. Reads and writes + * of state and ret are ordered with memory barriers. + */ enum ThreadState state; int ret; @@ -95,9 +100,12 @@ static void *worker_thread(void *unused) ret = req->func(req->arg); - qemu_mutex_lock(&lock); - req->state = THREAD_DONE; req->ret = ret; + /* Write ret before state. */ + smp_wmb(); + req->state = THREAD_DONE; + + qemu_mutex_lock(&lock); if (pending_cancellations) { qemu_cond_broadcast(&check_cancel); } @@ -162,11 +170,10 @@ restart: trace_thread_pool_complete(elem, elem->common.opaque, elem->ret); } if (elem->state == THREAD_DONE && elem->common.cb) { - qemu_mutex_lock(&lock); - int ret = elem->ret; - qemu_mutex_unlock(&lock); QLIST_REMOVE(elem, all); - elem->common.cb(elem->common.opaque, ret); + /* Read state before ret. */ + smp_rmb(); + elem->common.cb(elem->common.opaque, elem->ret); qemu_aio_release(elem); goto restart; } else {