qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: alex@alex.org.uk
Subject: [Qemu-devel] [PATCH 2/8] timers: add timer_mod_anticipate and timer_mod_anticipate_ns
Date: Tue,  8 Oct 2013 10:47:32 +0200	[thread overview]
Message-ID: <1381222058-16701-3-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1381222058-16701-1-git-send-email-pbonzini@redhat.com>

These let a user anticipate the deadline of a timer, atomically with
other sites that call the function.  This helps avoiding complicated
lock hierarchies.  It is useful whenever the timer does work based on
the current value of the clock (rather than doing something periodically
on every tick).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/timer.h | 26 ++++++++++++++++++++++++++
 qemu-timer.c         | 29 +++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index b58903b..f215b0b 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -539,6 +539,19 @@ void timer_del(QEMUTimer *ts);
 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
 
 /**
+ * timer_mod_anticipate_ns:
+ * @ts: the timer
+ * @expire_time: the expiry time in nanoseconds
+ *
+ * Modify a timer to expire at @expire_time or the current time,
+ * whichever comes earlier.
+ *
+ * This function is thread-safe but the timer and its timer list must not be
+ * freed while this function is running.
+ */
+void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
+
+/**
  * timer_mod:
  * @ts: the timer
  * @expire_time: the expire time in the units associated with the timer
@@ -552,6 +565,19 @@ void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
 
 /**
+ * timer_mod_anticipate:
+ * @ts: the timer
+ * @expire_time: the expiry time in nanoseconds
+ *
+ * Modify a timer to expire at @expire_time or the current time, whichever
+ * comes earlier, taking into account the scale associated with the timer.
+ *
+ * This function is thread-safe but the timer and its timer list must not be
+ * freed while this function is running.
+ */
+void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
+
+/**
  * timer_pending:
  * @ts: the timer
  *
diff --git a/qemu-timer.c b/qemu-timer.c
index 95fc6eb..202e9a2 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -393,11 +393,40 @@ void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
     }
 }
 
+/* modify the current timer so that it will be fired when current_time
+   >= expire_time or the current deadline, whichever comes earlier.
+   The corresponding callback will be called. */
+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);
+        }
+        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);
+    }
+}
+
 void timer_mod(QEMUTimer *ts, int64_t expire_time)
 {
     timer_mod_ns(ts, expire_time * ts->scale);
 }
 
+void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
+{
+    timer_mod_anticipate_ns(ts, expire_time * ts->scale);
+}
+
 bool timer_pending(QEMUTimer *ts)
 {
     return ts->expire_time >= 0;
-- 
1.8.3.1

  parent reply	other threads:[~2013-10-08  8:48 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-08  8:47 [Qemu-devel] [PATCH 0/8] Make icount thread-safe Paolo Bonzini
2013-10-08  8:47 ` [Qemu-devel] [PATCH 1/8] timers: extract timer_mod_ns_locked and timerlist_rearm Paolo Bonzini
2013-10-08  9:06   ` Alex Bligh
2013-10-08  8:47 ` Paolo Bonzini [this message]
2013-10-08  9:15   ` [Qemu-devel] [PATCH 2/8] timers: add timer_mod_anticipate and timer_mod_anticipate_ns Alex Bligh
2013-10-08  9:25     ` Paolo Bonzini
2013-10-08 17:01       ` Alex Bligh
2013-10-08  8:47 ` [Qemu-devel] [PATCH 3/8] timers: use cpu_get_icount() directly Paolo Bonzini
2013-10-08 16:49   ` Alex Bligh
2013-10-08  8:47 ` [Qemu-devel] [PATCH 4/8] timers: reorganize icount_warp_rt Paolo Bonzini
2013-10-08 16:50   ` Alex Bligh
2013-10-08  8:47 ` [Qemu-devel] [PATCH 5/8] timers: prepare the code for future races in calling qemu_clock_warp Paolo Bonzini
2013-10-08 16:54   ` Alex Bligh
2013-10-08 16:56     ` Paolo Bonzini
2013-10-08 17:08       ` Alex Bligh
2013-10-08 17:10         ` Paolo Bonzini
2013-10-08  8:47 ` [Qemu-devel] [PATCH 6/8] timers: introduce cpu_get_clock_locked Paolo Bonzini
2013-10-08 16:55   ` Alex Bligh
2013-10-08  8:47 ` [Qemu-devel] [PATCH 7/8] timers: document (future) locking rules for icount Paolo Bonzini
2013-10-08 16:56   ` Alex Bligh
2013-10-08  8:47 ` [Qemu-devel] [PATCH 8/8] timers: make icount thread-safe Paolo Bonzini
2013-10-08 16:57   ` Alex Bligh
2013-10-08 13:47 ` [Qemu-devel] [PATCH 0/8] Make " Andreas Färber
2013-10-08 13:55   ` Paolo Bonzini
2013-11-05  9:27 ` Stefan Hajnoczi

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=1381222058-16701-3-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=alex@alex.org.uk \
    --cc=qemu-devel@nongnu.org \
    /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).