All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Emilio G . Cota" <cota@braap.org>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	Fam Zheng <famz@redhat.com>
Subject: [Qemu-devel] [PATCH 4/5] qht: convert to use lock guards
Date: Fri,  8 Dec 2017 11:55:52 +0100	[thread overview]
Message-ID: <20171208105553.12249-5-pbonzini@redhat.com> (raw)
In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 util/qht.c | 59 +++++++++++++++++++++++++++++------------------------------
 1 file changed, 29 insertions(+), 30 deletions(-)

diff --git a/util/qht.c b/util/qht.c
index ff4d2e6974..95ac32f990 100644
--- a/util/qht.c
+++ b/util/qht.c
@@ -248,18 +248,18 @@ void qht_map_lock_buckets__no_stale(struct qht *ht, struct qht_map **pmap)
     map = atomic_rcu_read(&ht->map);
     qht_map_lock_buckets(map);
     if (likely(!qht_map_is_stale__locked(ht, map))) {
-        *pmap = map;
-        return;
+        goto out;
     }
     qht_map_unlock_buckets(map);
 
     /* we raced with a resize; acquire ht->lock to see the updated ht->map */
-    qemu_mutex_lock(&ht->lock);
-    map = ht->map;
-    qht_map_lock_buckets(map);
-    qemu_mutex_unlock(&ht->lock);
+    QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) {
+        map = ht->map;
+        qht_map_lock_buckets(map);
+    }
+
+out:
     *pmap = map;
-    return;
 }
 
 /*
@@ -282,17 +282,18 @@ struct qht_bucket *qht_bucket_lock__no_stale(struct qht *ht, uint32_t hash,
 
     qemu_spin_lock(&b->lock);
     if (likely(!qht_map_is_stale__locked(ht, map))) {
-        *pmap = map;
-        return b;
+        goto out;
     }
     qemu_spin_unlock(&b->lock);
 
     /* we raced with a resize; acquire ht->lock to see the updated ht->map */
-    qemu_mutex_lock(&ht->lock);
-    map = ht->map;
-    b = qht_map_to_bucket(map, hash);
-    qemu_spin_lock(&b->lock);
-    qemu_mutex_unlock(&ht->lock);
+    QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) {
+        map = ht->map;
+        b = qht_map_to_bucket(map, hash);
+        qemu_spin_lock(&b->lock);
+    }
+
+out:
     *pmap = map;
     return b;
 }
@@ -427,13 +428,13 @@ bool qht_reset_size(struct qht *ht, size_t n_elems)
 
     n_buckets = qht_elems_to_buckets(n_elems);
 
-    qemu_mutex_lock(&ht->lock);
-    map = ht->map;
-    if (n_buckets != map->n_buckets) {
-        new = qht_map_create(n_buckets);
+    QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) {
+        map = ht->map;
+        if (n_buckets != map->n_buckets) {
+            new = qht_map_create(n_buckets);
+        }
+        qht_do_resize_and_reset(ht, new);
     }
-    qht_do_resize_and_reset(ht, new);
-    qemu_mutex_unlock(&ht->lock);
 
     return !!new;
 }
@@ -771,19 +772,17 @@ static void qht_do_resize_reset(struct qht *ht, struct qht_map *new, bool reset)
 bool qht_resize(struct qht *ht, size_t n_elems)
 {
     size_t n_buckets = qht_elems_to_buckets(n_elems);
-    size_t ret = false;
 
-    qemu_mutex_lock(&ht->lock);
-    if (n_buckets != ht->map->n_buckets) {
-        struct qht_map *new;
+    QEMU_WITH_LOCK(QemuMutex, ht_guard, &ht->lock) {
+        if (n_buckets != ht->map->n_buckets) {
+            struct qht_map *new;
 
-        new = qht_map_create(n_buckets);
-        qht_do_resize(ht, new);
-        ret = true;
+            new = qht_map_create(n_buckets);
+            qht_do_resize(ht, new);
+            return true;
+        }
     }
-    qemu_mutex_unlock(&ht->lock);
-
-    return ret;
+    return false;
 }
 
 /* pass @stats to qht_statistics_destroy() when done */
-- 
2.14.3

  parent reply	other threads:[~2017-12-08 10:56 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-08 10:55 [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup)) Paolo Bonzini
2017-12-08 10:55 ` [Qemu-devel] [PATCH 1/5] compiler: add a helper for C99 inline functions Paolo Bonzini
2017-12-08 10:55 ` [Qemu-devel] [PATCH 2/5] lock-guard: add scoped lock implementation Paolo Bonzini
2017-12-08 15:30   ` Stefan Hajnoczi
2017-12-08 17:56     ` Paolo Bonzini
2017-12-08 20:12       ` Eric Blake
2017-12-11 10:16       ` Stefan Hajnoczi
2017-12-11 13:51         ` Eric Blake
2017-12-12  9:16           ` Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 3/5] qemu-timer: convert to use lock guards Paolo Bonzini
2017-12-08 14:26   ` Stefan Hajnoczi
2017-12-08 10:55 ` Paolo Bonzini [this message]
2017-12-08 14:27   ` [Qemu-devel] [PATCH 4/5] qht: " Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 5/5] thread-pool: " Paolo Bonzini
2017-12-08 15:13   ` Stefan Hajnoczi
2017-12-08 18:12     ` Paolo Bonzini
2017-12-08 20:02       ` Eric Blake
2017-12-11 10:23         ` Stefan Hajnoczi
2017-12-11 22:03           ` Paolo Bonzini
2017-12-08 19:50     ` Eric Blake
2017-12-11  6:35     ` Peter Xu
2017-12-08 19:40 ` [Qemu-devel] [RFC PATCH 0/5] Scoped locks using attribute((cleanup)) Eric Blake
2017-12-11  9:38   ` Peter Maydell
2017-12-11 14:11     ` Eric Blake
2017-12-11 21:32       ` Paolo Bonzini
2017-12-12 20:41         ` Eric Blake
2017-12-15 15:50           ` Richard Henderson
2017-12-11  6:40 ` no-reply
2017-12-11  6:40 ` no-reply
2017-12-11  6:46 ` no-reply
2017-12-11 22:06 ` Emilio G. Cota

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171208105553.12249-5-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=cota@braap.org \
    --cc=famz@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.