From mboxrd@z Thu Jan 1 00:00:00 1970 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) Sender: Paolo Bonzini From: Paolo Bonzini Date: Fri, 15 Apr 2016 13:32:06 +0200 Message-Id: <1460719926-12950-12-git-send-email-pbonzini@redhat.com> In-Reply-To: <1460719926-12950-1-git-send-email-pbonzini@redhat.com> References: <1460719926-12950-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 11/11] coroutine-lock: make CoRwlock thread-safe and fair List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, famz@redhat.com, kwolf@redhat.com, berto@igalia.com 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. + */ } -- 2.5.5