From patchwork Fri Apr 15 11:32:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 8850471 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 749019FBEA for ; Fri, 15 Apr 2016 11:41:28 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A0D7620295 for ; Fri, 15 Apr 2016 11:41:26 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EAAA420377 for ; Fri, 15 Apr 2016 11:41:25 +0000 (UTC) Received: from localhost ([::1]:60648 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar27p-00059f-Do for patchwork-qemu-devel@patchwork.kernel.org; Fri, 15 Apr 2016 07:41:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55635) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar1zC-00065w-RX for qemu-devel@nongnu.org; Fri, 15 Apr 2016 07:32:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ar1zA-00009S-6G for qemu-devel@nongnu.org; Fri, 15 Apr 2016 07:32:30 -0400 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:35516) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar1zA-00007m-00 for qemu-devel@nongnu.org; Fri, 15 Apr 2016 07:32:28 -0400 Received: by mail-wm0-x244.google.com with SMTP id a140so5416301wma.2 for ; Fri, 15 Apr 2016 04:32:27 -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:in-reply-to:references; bh=BKztJ6N19dX5dCW4LtcXMAY6V96L4IT4UQhTihgdtIM=; b=ckHTC4+zE417VuUPwggv5jTRcaH87NwAOmiFX3zVCRcv0E1IclwJwQ5S7hZ9jrQe3e l1jq3EDMPDh2UaimFBt0s3qQsjUgtcnUtdLt2DPuHVsrratRFvnO6QwVnZ1CLLD4LDQ1 e0adYZZi4EKzBbUVzPBhLeLSUZfJZm2DDgVVl4drcS2aAIe/Yl5sYrta15m29i+7xP2j iNzc4SHXeR/kzkE/OwVnbd+K0guJsenmxDpTW0gBRozsUJkMMUxsjkB1iSHuBfDFXtkd JMO+eEF/aeiECzJnutKurhM5JQDOqnPLSaqtMTFqyAiw48+9swMuCjAWimfW42QK3iS4 qv1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=BKztJ6N19dX5dCW4LtcXMAY6V96L4IT4UQhTihgdtIM=; b=MPaywrtRhUM2BZj8l7K891YXmgN4vxw4vRmh86smkQm7/5sdsDweRKkNbL1ImS69j5 bSPZllCvk8mzWKg4+/lW7kXOq7++CIjoi2trnk2ROISyjOuih4UhlzGHbZPim36DzDPY ISpW3JIhkMxhtI8bIy6OHWTNQJi4YypFCtm0A50fHRcRy+NRFuY0EgckfpB8qlun0fPS au5TjaaLfEy/i9nKUBOIue5kNcOuk8mSKDyJLLX6YLNhybCRMAlv+tnTxnY/wHQmJeCN f03OfzVY3uW+bkrAqZfkzOXbjgLUia6mkeut/Wbm1WrEUHYV6LjxldHblG+1nbqSDf9w U/gw== X-Gm-Message-State: AOPr4FV26wdqRGL2x0YoNfxzXy/wqXPf/0enPakwiDgR0t2kAPhgu7lqEouKC5sJBmmroA== X-Received: by 10.28.140.12 with SMTP id o12mr3855584wmd.19.1460719947401; Fri, 15 Apr 2016 04:32:27 -0700 (PDT) Received: from donizetti.redhat.com (94-39-141-76.adsl-ull.clienti.tiscali.it. [94.39.141.76]) by smtp.gmail.com with ESMTPSA id qt3sm15217493wjc.32.2016.04.15.04.32.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Apr 2016 04:32:26 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 15 Apr 2016 13:32:06 +0200 Message-Id: <1460719926-12950-12-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1460719926-12950-1-git-send-email-pbonzini@redhat.com> References: <1460719926-12950-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::244 Subject: [Qemu-devel] [PATCH 11/11] coroutine-lock: make CoRwlock thread-safe and fair X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, berto@igalia.com, famz@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This adds a CoMutex around the existing CoQueue. Because the write-side can just take CoMutex, the old "writer" field is not necessary anymore. Instead of removing it altogether, count the number of pending writers during a read-side critical section and forbid further readers from entering. Signed-off-by: Paolo Bonzini --- include/qemu/coroutine.h | 3 ++- util/qemu-coroutine-lock.c | 29 +++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index 25e31a1..0dfb301 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -198,8 +198,9 @@ bool qemu_co_queue_empty(CoQueue *queue); typedef struct CoRwlock { - bool writer; + int pending_writer; int reader; + CoMutex mutex; CoQueue queue; } CoRwlock; diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c index 828d79a..db9cf01 100644 --- a/util/qemu-coroutine-lock.c +++ b/util/qemu-coroutine-lock.c @@ -322,36 +322,49 @@ void qemu_co_rwlock_init(CoRwlock *lock) { memset(lock, 0, sizeof(*lock)); qemu_co_queue_init(&lock->queue); + qemu_co_mutex_init(&lock->mutex); } void qemu_co_rwlock_rdlock(CoRwlock *lock) { - while (lock->writer) { - qemu_co_queue_wait(&lock->queue, NULL); + qemu_co_mutex_lock(&lock->mutex); + /* For fairness, wait if a writer is in line. */ + while (lock->pending_writer) { + qemu_co_queue_wait(&lock->queue, &lock->mutex); } lock->reader++; + qemu_co_mutex_unlock(&lock->mutex); + + /* The rest of the read-side critical section is run without the mutex. */ } void qemu_co_rwlock_unlock(CoRwlock *lock) { assert(qemu_in_coroutine()); - if (lock->writer) { - lock->writer = false; + if (!lock->reader) { + /* The critical section started in qemu_co_rwlock_wrlock. */ qemu_co_queue_restart_all(&lock->queue); } else { + qemu_co_mutex_lock(&lock->mutex); lock->reader--; - assert(lock->reader >= 0); /* Wakeup only one waiting writer */ if (!lock->reader) { qemu_co_queue_next(&lock->queue); } } + qemu_co_mutex_unlock(&lock->mutex); } void qemu_co_rwlock_wrlock(CoRwlock *lock) { - while (lock->writer || lock->reader) { - qemu_co_queue_wait(&lock->queue, NULL); + qemu_co_mutex_lock(&lock->mutex); + lock->pending_writer++; + while (lock->reader) { + qemu_co_queue_wait(&lock->queue, &lock->mutex); } - lock->writer = true; + lock->pending_writer--; + + /* The rest of the write-side critical section is run with + * the mutex taken, so lock->reader remains zero. + */ }