From nobody Tue Feb 10 05:10:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1643364050; cv=none; d=zohomail.com; s=zohoarc; b=RykJ0QKqbEMfkdjMqxzZemO/PQgYubs1nbrIedCDMIiHK2sjBHeZd9dvyWkLyMzDD08uf8Mcz363qOrRb4vQDI8xUZe1KtR8oyWhVjvYJ5wXQzcDIEao1t81j1+3YF6xBiLa3+6rkxW1czJcFICFJ4YNex/pm/3+QCucGE1Usns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1643364050; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ji9Sx08DerxLEEdo28X+dDZj5Dzv/1jx8c0qzs4vidU=; b=ZnfefJPi/XkeaGqF576luMhO6J5GK2p0u0/fafkPY8hw3D6+0OZUlZ87DR17HJjmuKETozh0gtgm2FLbClK0zGDzwFmJGX0xbFsi+64TLU7KFKkKIvK5G1Pz9XrPf4xifpU7virQQlP3yxJSNKBNK14DsMJCzy621Ew9NC5h+tk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1643364050089716.953340370783; Fri, 28 Jan 2022 02:00:50 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-589-gAfuxYQ2N9masOpZPXlm3Q-1; Fri, 28 Jan 2022 05:00:25 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 44A908C7195; Fri, 28 Jan 2022 09:59:54 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 23C416D033; Fri, 28 Jan 2022 09:59:54 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E4D644A7C8; Fri, 28 Jan 2022 09:59:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 20S9xg0x031724 for ; Fri, 28 Jan 2022 04:59:42 -0500 Received: by smtp.corp.redhat.com (Postfix) id A874A708C9; Fri, 28 Jan 2022 09:59:42 +0000 (UTC) Received: from work.speedport.ip (unknown [10.39.194.58]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9F9574E8F; Fri, 28 Jan 2022 09:59:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643364049; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=ji9Sx08DerxLEEdo28X+dDZj5Dzv/1jx8c0qzs4vidU=; b=UUJaukHhOlmm/vyIPqieccl2XkCfDU4FetyyzZdIMHW1A++vKZLxhbAYquEVkHzWrKrXO1 fz4ec1Wu3LBf5dvLC6E89ywAnM2mVz0U7h/ezJnNVpcXEN+UqOuK1CIoexPh2QrxCCy2+D FihupzC1D3GE5SfTXjXmx1DblXqZDl4= X-MC-Unique: gAfuxYQ2N9masOpZPXlm3Q-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH v4 2/9] virthread: Introduce virLockGuard Date: Fri, 28 Jan 2022 10:59:15 +0100 Message-Id: <20220128095922.17782-3-twiederh@redhat.com> In-Reply-To: <20220128095922.17782-1-twiederh@redhat.com> References: <20220128095922.17782-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Cc: Tim Wiederhake X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1643364051960100001 Content-Type: text/plain; charset="utf-8" Locks a virMutex on creation and unlocks it in its destructor. The VIR_LOCK_GUARD macro is used instead of "g_auto(virLockGuard)" to work around a clang issue (see https://bugs.llvm.org/show_bug.cgi?id=3D3888 and https://bugs.llvm.org/show_bug.cgi?id=3D43482). Typical usage: void function(virMutex *m) { VIR_LOCK_GUARD lock =3D virLockGuardLock(m); /* `m` is locked, and released automatically on scope exit */ ... while (expression) { VIR_LOCK_GUARD lock2 =3D virLockGuardLock(...); /* similar */ } } Signed-off-by: Tim Wiederhake --- src/libvirt_private.syms | 2 ++ src/util/virthread.c | 15 +++++++++++++++ src/util/virthread.h | 10 ++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ba3462d849..4cfe6c17eb 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3376,6 +3376,8 @@ virCondInit; virCondSignal; virCondWait; virCondWaitUntil; +virLockGuardLock; +virLockGuardUnlock; virMutexDestroy; virMutexInit; virMutexInitRecursive; diff --git a/src/util/virthread.c b/src/util/virthread.c index e89c1a09fb..5422bb74fd 100644 --- a/src/util/virthread.c +++ b/src/util/virthread.c @@ -96,6 +96,21 @@ void virMutexUnlock(virMutex *m) pthread_mutex_unlock(&m->lock); } =20 +virLockGuard virLockGuardLock(virMutex *m) +{ + virLockGuard l =3D { m }; + virMutexLock(m); + return l; +} + +void virLockGuardUnlock(virLockGuard *l) +{ + if (!l || !l->mutex) + return; + + virMutexUnlock(g_steal_pointer(&l->mutex)); +} + =20 int virRWLockInit(virRWLock *m) { diff --git a/src/util/virthread.h b/src/util/virthread.h index 55c8263ae6..6cdaf2820e 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -31,6 +31,11 @@ struct virMutex { pthread_mutex_t lock; }; =20 +typedef struct virLockGuard virLockGuard; +struct virLockGuard { + virMutex *mutex; +}; + typedef struct virRWLock virRWLock; struct virRWLock { pthread_rwlock_t lock; @@ -121,6 +126,11 @@ void virMutexLock(virMutex *m); void virMutexUnlock(virMutex *m); =20 =20 +virLockGuard virLockGuardLock(virMutex *m); +void virLockGuardUnlock(virLockGuard *l); +G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virLockGuard, virLockGuardUnlock); +#define VIR_LOCK_GUARD g_auto(virLockGuard) G_GNUC_UNUSED + int virRWLockInit(virRWLock *m) G_GNUC_WARN_UNUSED_RESULT; void virRWLockDestroy(virRWLock *m); =20 --=20 2.31.1