qemu-timer: do not take the lock in timer_pending
We can deduce the result from expire_time, by making it always -1 if
the timer is not in the active_timers list. We need to check against
negative times passed to timer_mod_ns; clamping them to zero is not
a problem because the only clock that has a zero value at VM startup
is QEMU_CLOCK_VIRTUAL, and it is monotonic so it cannot be non-zero.
QEMU_CLOCK_HOST, instead, is not monotonic but it cannot go to negative
values unless the host time is seriously screwed up and points to
the 1960s.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
diff --git a/qemu-timer.c b/qemu-timer.c
index e504747..6b62e88 100644
--- a/qemu-timer.c
+++ b/qemu-timer.c
@@ -312,6 +312,7 @@
ts->cb = cb;
ts->opaque = opaque;
ts->scale = scale;
+ ts->expire_time = -1;
}
void timer_free(QEMUTimer *ts)
@@ -323,6 +324,7 @@
{
QEMUTimer **pt, *t;
+ ts->expire_time = -1;
pt = &timer_list->active_timers;
for(;;) {
t = *pt;
@@ -365,7 +367,7 @@
}
pt = &t->next;
}
- ts->expire_time = expire_time;
+ ts->expire_time = MAX(expire_time, 0);
ts->next = *pt;
*pt = ts;
qemu_mutex_unlock(&timer_list->active_timers_lock);
@@ -385,19 +387,7 @@
bool timer_pending(QEMUTimer *ts)
{
- QEMUTimerList *timer_list = ts->timer_list;
- QEMUTimer *t;
- bool found = false;
-
- qemu_mutex_lock(&timer_list->active_timers_lock);
- for (t = timer_list->active_timers; t != NULL; t = t->next) {
- if (t == ts) {
- found = true;
- break;
- }
- }
- qemu_mutex_unlock(&timer_list->active_timers_lock);
- return found;
+ return ts->expire_time >= 0;
}
bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
@@ -429,6 +419,7 @@
/* 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);