From patchwork Thu Oct 17 15:48:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 284298 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 07FDF2C0077 for ; Fri, 18 Oct 2013 03:07:01 +1100 (EST) Received: from localhost ([::1]:52972 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWptm-0005ah-IC for incoming@patchwork.ozlabs.org; Thu, 17 Oct 2013 11:54:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41402) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWppL-00074p-Qc for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VWppF-0000oH-KB for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:31 -0400 Received: from mail-ea0-x233.google.com ([2a00:1450:4013:c01::233]:61259) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VWppF-0000nn-BN for qemu-devel@nongnu.org; Thu, 17 Oct 2013 11:49:25 -0400 Received: by mail-ea0-f179.google.com with SMTP id b10so1173503eae.10 for ; Thu, 17 Oct 2013 08:49:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=jyCKvb73Mow0wHnX04LdEsl3nK4mdwtmMvSXqVfBzew=; b=CUG8JWJPJDYlCOHhhv1Jum0yYNZ6UGNlG2j2e6c/LY3Y2QA3ouwp1E6HNCDjB7sF/D zIOhLM/KAeGyu52B5xbGXlYy1c7bGOmQITX25uxemFjhk6gHPjziHu4WlosuU/YAj1w9 E35mGPW8a60809Z0IgkKfO7LBxcBTtuZEGS7oVC0r9+VYAVplTQfWP9HZK1+m+Dy45Yc Bd6TkxdST/hn0SDzsEbfi+lZuA5hW1pd0ub+ecgv0rUHB4kSpZ4jE8tNa2RBIVBgbk/q v21i6Z2uQ4uZxGXrz97g2H3enHBufE4pi2zHnSt6j2Ijr/CD3jEt4fv9ydL6lsfkiNXD /OBg== X-Received: by 10.15.44.202 with SMTP id z50mr4533451eev.68.1382024964500; Thu, 17 Oct 2013 08:49:24 -0700 (PDT) Received: from playground.lan (net-37-116-196-7.cust.dsl.vodafone.it. [37.116.196.7]) by mx.google.com with ESMTPSA id a43sm194431296eep.9.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 17 Oct 2013 08:49:23 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 17 Oct 2013 17:48:49 +0200 Message-Id: <1382024935-28297-12-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1382024935-28297-1-git-send-email-pbonzini@redhat.com> References: <1382024935-28297-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c01::233 Subject: [Qemu-devel] [PULL 11/17] timer: add timer_mod_anticipate and timer_mod_anticipate_ns X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org These let a user anticipate the deadline of a timer, atomically with other sites that call the function. This helps avoiding complicated lock hierarchies. Reviewed-by: Alex Bligh Signed-off-by: Paolo Bonzini --- 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 1254ef7..5afcffc 100644 --- a/include/qemu/timer.h +++ b/include/qemu/timer.h @@ -545,6 +545,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 @@ -558,6 +571,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 0305ad5..e15ce47 100644 --- a/qemu-timer.c +++ b/qemu-timer.c @@ -410,11 +410,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;