qemu-devel.nongnu.org archive mirror
 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 3/5] qemu-timer: convert to use lock guards
Date: Fri,  8 Dec 2017 11:55:51 +0100	[thread overview]
Message-ID: <20171208105553.12249-4-pbonzini@redhat.com> (raw)
In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 util/qemu-timer.c | 84 +++++++++++++++++++++++++++----------------------------
 1 file changed, 42 insertions(+), 42 deletions(-)

diff --git a/util/qemu-timer.c b/util/qemu-timer.c
index 82d56507a2..7a99e0e336 100644
--- a/util/qemu-timer.c
+++ b/util/qemu-timer.c
@@ -192,14 +192,11 @@ bool timerlist_expired(QEMUTimerList *timer_list)
         return false;
     }
 
-    qemu_mutex_lock(&timer_list->active_timers_lock);
+    QEMU_LOCK_GUARD(QemuMutex, timers_guard, &timer_list->active_timers_lock);
     if (!timer_list->active_timers) {
-        qemu_mutex_unlock(&timer_list->active_timers_lock);
         return false;
     }
     expire_time = timer_list->active_timers->expire_time;
-    qemu_mutex_unlock(&timer_list->active_timers_lock);
-
     return expire_time <= qemu_clock_get_ns(timer_list->clock->type);
 }
 
@@ -231,13 +228,13 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
      * value but ->notify_cb() is called when the deadline changes.  Therefore
      * the caller should notice the change and there is no race condition.
      */
-    qemu_mutex_lock(&timer_list->active_timers_lock);
-    if (!timer_list->active_timers) {
-        qemu_mutex_unlock(&timer_list->active_timers_lock);
-        return -1;
+    QEMU_WITH_LOCK(QemuMutex, timers_guard,
+                         &timer_list->active_timers_lock) {
+        if (!timer_list->active_timers) {
+            return -1;
+        }
+        expire_time = timer_list->active_timers->expire_time;
     }
-    expire_time = timer_list->active_timers->expire_time;
-    qemu_mutex_unlock(&timer_list->active_timers_lock);
 
     delta = expire_time - qemu_clock_get_ns(timer_list->clock->type);
 
@@ -410,9 +407,8 @@ void timer_del(QEMUTimer *ts)
     QEMUTimerList *timer_list = ts->timer_list;
 
     if (timer_list) {
-        qemu_mutex_lock(&timer_list->active_timers_lock);
+        QEMU_LOCK_GUARD(QemuMutex, timers_guard, &timer_list->active_timers_lock);
         timer_del_locked(timer_list, ts);
-        qemu_mutex_unlock(&timer_list->active_timers_lock);
     }
 }
 
@@ -423,10 +419,11 @@ void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
     QEMUTimerList *timer_list = ts->timer_list;
     bool rearm;
 
-    qemu_mutex_lock(&timer_list->active_timers_lock);
-    timer_del_locked(timer_list, ts);
-    rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
-    qemu_mutex_unlock(&timer_list->active_timers_lock);
+    QEMU_WITH_LOCK(QemuMutex, timers_guard,
+                         &timer_list->active_timers_lock) {
+        timer_del_locked(timer_list, ts);
+        rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
+    }
 
     if (rearm) {
         timerlist_rearm(timer_list);
@@ -441,16 +438,17 @@ void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
     QEMUTimerList *timer_list = ts->timer_list;
     bool rearm;
 
-    qemu_mutex_lock(&timer_list->active_timers_lock);
-    if (ts->expire_time == -1 || ts->expire_time > expire_time) {
-        if (ts->expire_time != -1) {
-            timer_del_locked(timer_list, ts);
+    QEMU_WITH_LOCK(QemuMutex, timers_guard,
+                         &timer_list->active_timers_lock) {
+        if (ts->expire_time == -1 || ts->expire_time > expire_time) {
+            if (ts->expire_time != -1) {
+                timer_del_locked(timer_list, ts);
+            }
+            rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
+        } else {
+            rearm = false;
         }
-        rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
-    } else {
-        rearm = false;
     }
-    qemu_mutex_unlock(&timer_list->active_timers_lock);
 
     if (rearm) {
         timerlist_rearm(timer_list);
@@ -516,25 +514,27 @@ bool timerlist_run_timers(QEMUTimerList *timer_list)
     }
 
     current_time = qemu_clock_get_ns(timer_list->clock->type);
-    for(;;) {
-        qemu_mutex_lock(&timer_list->active_timers_lock);
-        ts = timer_list->active_timers;
-        if (!timer_expired_ns(ts, current_time)) {
-            qemu_mutex_unlock(&timer_list->active_timers_lock);
-            break;
-        }
+    QEMU_WITH_LOCK(QemuMutex, timers_guard,
+                         &timer_list->active_timers_lock) {
+        for(;;) {
+            ts = timer_list->active_timers;
+            if (!timer_expired_ns(ts, current_time)) {
+                break;
+            }
 
-        /* remove timer from the list before calling the callback */
-        timer_list->active_timers = ts->next;
-        ts->next = NULL;
-        ts->expire_time = -1;
-        cb = ts->cb;
-        opaque = ts->opaque;
-        qemu_mutex_unlock(&timer_list->active_timers_lock);
-
-        /* run the callback (the timer list can be modified) */
-        cb(opaque);
-        progress = true;
+            /* remove timer from the list before calling the callback */
+            timer_list->active_timers = ts->next;
+            ts->next = NULL;
+            ts->expire_time = -1;
+            cb = ts->cb;
+            opaque = ts->opaque;
+
+            /* run the callback (the timer list can be modified) */
+            qemu_lock_guard_unlock(&timers_guard);
+            cb(opaque);
+            progress = true;
+            qemu_lock_guard_lock(&timers_guard);
+        }
     }
 
 out:
-- 
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 ` Paolo Bonzini [this message]
2017-12-08 14:26   ` [Qemu-devel] [PATCH 3/5] qemu-timer: convert to use lock guards Stefan Hajnoczi
2017-12-08 10:55 ` [Qemu-devel] [PATCH 4/5] qht: " Paolo Bonzini
2017-12-08 14:27   ` 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-4-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).