From nobody Tue Feb 10 15:01:08 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1512730777551996.8695471695892; Fri, 8 Dec 2017 02:59:37 -0800 (PST) Received: from localhost ([::1]:36556 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGNN-0003cp-2y for importer@patchew.org; Fri, 08 Dec 2017 05:59:29 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47577) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eNGK8-00011d-Mx for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eNGK7-0001mU-PY for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:08 -0500 Received: from mail-wr0-x244.google.com ([2a00:1450:400c:c0c::244]:42112) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eNGK7-0001mK-FS for qemu-devel@nongnu.org; Fri, 08 Dec 2017 05:56:07 -0500 Received: by mail-wr0-x244.google.com with SMTP id s66so10421640wrc.9 for ; Fri, 08 Dec 2017 02:56:07 -0800 (PST) Received: from donizetti.lan (dynamic-adsl-78-12-251-125.clienti.tiscali.it. [78.12.251.125]) by smtp.gmail.com with ESMTPSA id v195sm1325461wmf.25.2017.12.08.02.56.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 08 Dec 2017 02:56:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=3IouiZhdTWBaQFl9Fga5R94i8paWQals15TPfKCUiaA=; b=Se4j+kEJuGEIrtH5QIRn3FaCh0KB1/A13SVhfaAfnzZ62S/EaNMeU2EOsKYZug/qmz HgoW7jybxCxgw5eZ7PbBuox5+jT4spfeaONFJhrK+72eMmLrBUR+qfgdDDuKCwHOVR3N 0JkuV6yJy0HGrp9AXPB9QUcIxn3YARRJsYaazu4S2p9RBR/zegIx8cNNoKP8Z5RW3aXD KopuKh/ZAGy8ZF8ynyfM3+FAyUCBz8EI8kpeYnDCn2i4uG6TCeflyCm7VIL3hzFvphq2 jTQzpCKme9x7BSBSaTa++3MlusABNgt+9TX7mxhF2C++KU2Gq0W8g/B02FGM/cxvtfG8 UiDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=3IouiZhdTWBaQFl9Fga5R94i8paWQals15TPfKCUiaA=; b=Ga9puPZ+O2tfsSHQl1hPXJ0UGYkdZtGaRMmD2Qru1uZ0n/+Eyhv4Tbm8IiR2P0e3Z/ +ZvF9Uqbs2d5Z1XVo/kFjKP5rDuAHwfQUdZ5bWmvA2AY8QtXO5z8Q93wuZ9bWWAzN9q8 8nip19GhDFN/j/rtWASqJDIFNqk0MExEkh4XbIsSXA/AwpyEkOTJhqQHV4/m796Q8IUB e3jlhT1/SYZmYRPN4qKtI6H8JwfZK5HOCnfglEhkAq4GAVUlP6SLTXNpLYx/nyrgfzlW eUb98O8Gs7Pm5GxYvMtuUzm2yX9zTpNDOqE6ttELPUUb1eEo5V1abtEfGEAZPDaCFwyW 8rRw== X-Gm-Message-State: AJaThX48Rvv6ELgWZRPeIB8lOXE8PnvPdeQqmvLcf24JB8dyqOqVIDEN kIvRRpz8R66O04Hmao8waNXN3JmQ X-Google-Smtp-Source: AGs4zMY64vyYNsTAVQiTgQqtWhdvLHrlnRGkJR4J4ws9CxGQQLZvGFEdIvGZqD2qLTtGykEWjz2cUg== X-Received: by 10.223.135.169 with SMTP id b38mr25751209wrb.278.1512730565958; Fri, 08 Dec 2017 02:56:05 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 8 Dec 2017 11:55:51 +0100 Message-Id: <20171208105553.12249-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20171208105553.12249-1-pbonzini@redhat.com> References: <20171208105553.12249-1-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::244 Subject: [Qemu-devel] [PATCH 3/5] qemu-timer: convert to use lock guards X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Emilio G . Cota" , Fam Zheng , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Paolo Bonzini --- 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; } =20 - qemu_mutex_lock(&timer_list->active_timers_lock); + QEMU_LOCK_GUARD(QemuMutex, timers_guard, &timer_list->active_timers_lo= ck); if (!timer_list->active_timers) { - qemu_mutex_unlock(&timer_list->active_timers_lock); return false; } expire_time =3D timer_list->active_timers->expire_time; - qemu_mutex_unlock(&timer_list->active_timers_lock); - return expire_time <=3D qemu_clock_get_ns(timer_list->clock->type); } =20 @@ -231,13 +228,13 @@ int64_t timerlist_deadline_ns(QEMUTimerList *timer_li= st) * value but ->notify_cb() is called when the deadline changes. There= fore * 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 =3D timer_list->active_timers->expire_time; } - expire_time =3D timer_list->active_timers->expire_time; - qemu_mutex_unlock(&timer_list->active_timers_lock); =20 delta =3D expire_time - qemu_clock_get_ns(timer_list->clock->type); =20 @@ -410,9 +407,8 @@ void timer_del(QEMUTimer *ts) QEMUTimerList *timer_list =3D ts->timer_list; =20 if (timer_list) { - qemu_mutex_lock(&timer_list->active_timers_lock); + QEMU_LOCK_GUARD(QemuMutex, timers_guard, &timer_list->active_timer= s_lock); timer_del_locked(timer_list, ts); - qemu_mutex_unlock(&timer_list->active_timers_lock); } } =20 @@ -423,10 +419,11 @@ void timer_mod_ns(QEMUTimer *ts, int64_t expire_time) QEMUTimerList *timer_list =3D ts->timer_list; bool rearm; =20 - qemu_mutex_lock(&timer_list->active_timers_lock); - timer_del_locked(timer_list, ts); - rearm =3D 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 =3D timer_mod_ns_locked(timer_list, ts, expire_time); + } =20 if (rearm) { timerlist_rearm(timer_list); @@ -441,16 +438,17 @@ void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t e= xpire_time) QEMUTimerList *timer_list =3D ts->timer_list; bool rearm; =20 - qemu_mutex_lock(&timer_list->active_timers_lock); - if (ts->expire_time =3D=3D -1 || ts->expire_time > expire_time) { - if (ts->expire_time !=3D -1) { - timer_del_locked(timer_list, ts); + QEMU_WITH_LOCK(QemuMutex, timers_guard, + &timer_list->active_timers_lock) { + if (ts->expire_time =3D=3D -1 || ts->expire_time > expire_time) { + if (ts->expire_time !=3D -1) { + timer_del_locked(timer_list, ts); + } + rearm =3D timer_mod_ns_locked(timer_list, ts, expire_time); + } else { + rearm =3D false; } - rearm =3D timer_mod_ns_locked(timer_list, ts, expire_time); - } else { - rearm =3D false; } - qemu_mutex_unlock(&timer_list->active_timers_lock); =20 if (rearm) { timerlist_rearm(timer_list); @@ -516,25 +514,27 @@ bool timerlist_run_timers(QEMUTimerList *timer_list) } =20 current_time =3D qemu_clock_get_ns(timer_list->clock->type); - for(;;) { - qemu_mutex_lock(&timer_list->active_timers_lock); - ts =3D 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 =3D timer_list->active_timers; + if (!timer_expired_ns(ts, current_time)) { + break; + } =20 - /* remove timer from the list before calling the callback */ - timer_list->active_timers =3D ts->next; - ts->next =3D NULL; - ts->expire_time =3D -1; - cb =3D ts->cb; - opaque =3D ts->opaque; - qemu_mutex_unlock(&timer_list->active_timers_lock); - - /* run the callback (the timer list can be modified) */ - cb(opaque); - progress =3D true; + /* remove timer from the list before calling the callback */ + timer_list->active_timers =3D ts->next; + ts->next =3D NULL; + ts->expire_time =3D -1; + cb =3D ts->cb; + opaque =3D ts->opaque; + + /* run the callback (the timer list can be modified) */ + qemu_lock_guard_unlock(&timers_guard); + cb(opaque); + progress =3D true; + qemu_lock_guard_lock(&timers_guard); + } } =20 out: --=20 2.14.3