From nobody Mon Feb 9 22:59:51 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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.133.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=1629895441; cv=none; d=zohomail.com; s=zohoarc; b=U5rZFDFl9P5z0HbgVmhH2McPHy8HjqhgTgB8VDjGWcpZ5rsUq3MNgBwBpwyXtMlHlzwBRXRLhhgTtgjUnAXUzeByMkQu4XlkfjTsB+yVVnRsS9m2OHRRLILvMQVj9i5g8GP5CK8BVzrSBbPEcMtN3YOS2+9P8GB4C1S0+cYWTGs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629895441; 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=bwN5x0hc6MSvwWTGW25uqrRCWL0MaSJUbZCOiasUTLc=; b=ZRjLUX9tIX6pEH163PY0AQD8V53wUJQW1y4qGj5lA+dgzTE4dkQGgctDy1KTJLaEbzxKjK5LyJkEcHJ3Zuwfkbkhn6Hcjb+Ek1HmpvLR+pN3yI2uVmirWCyCgZWdI3abHF/+HnVztTbgvEhmXQYmXctBIfR5XVOB2LJ6pQhChnw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.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.133.124]) by mx.zohomail.com with SMTPS id 1629895441980171.5209696767721; Wed, 25 Aug 2021 05:44:01 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-530-djb7UeZHNdmpC92xFShrpA-1; Wed, 25 Aug 2021 08:43:59 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 51DFA100806B; Wed, 25 Aug 2021 12:43:54 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3074B1036D28; Wed, 25 Aug 2021 12:43: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 ED0C7182188C; Wed, 25 Aug 2021 12:43: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 17PChlPc013636 for ; Wed, 25 Aug 2021 08:43:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0B539669ED; Wed, 25 Aug 2021 12:43:47 +0000 (UTC) Received: from work.redhat.com (unknown [10.39.192.26]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9C29188E4; Wed, 25 Aug 2021 12:43:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1629895440; 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=bwN5x0hc6MSvwWTGW25uqrRCWL0MaSJUbZCOiasUTLc=; b=eqKWX+NTCM6Nat9DT56s9eVxfutfuAQUbw10J4GG57/DXSTMFIp468RlpHtGNIXfniRk03 8WWjbdVO53+gbj04WhI+mcLjrHUSY2/N4W1R1pt5NjW3SeU8fRg+y0y+tr+SeLlZqrVtWd RnF170pl9eBRdelpAG1NI+T2MhHXhoM= X-MC-Unique: djb7UeZHNdmpC92xFShrpA-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH 05/11] virthread: Introduce virLockGuard Date: Wed, 25 Aug 2021 14:43:24 +0200 Message-Id: <20210825124330.84282-6-twiederh@redhat.com> In-Reply-To: <20210825124330.84282-1-twiederh@redhat.com> References: <20210825124330.84282-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.84 on 10.5.11.22 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: 1629895443256100003 Content-Type: text/plain; charset="utf-8" Locks a virMutex on creation and unlocks it in its destructor. Typical usage: void function(virMutex *m) { vir_g_autoptr(virLockGuard) lock =3D virLockGuardNew(m); /* `m` is locked, and released automatically on scope exit */ ... while (expression) { vir_g_autoptr(virLockGuard) lock2 =3D virLockGuardNew(...); /* similar */ } } Signed-off-by: Tim Wiederhake --- src/libvirt_private.syms | 3 +++ src/util/virthread.c | 26 ++++++++++++++++++++++++++ src/util/virthread.h | 10 ++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fa11ee3df5..d8170a59af 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3367,6 +3367,9 @@ virCondInit; virCondSignal; virCondWait; virCondWaitUntil; +virLockGuardFree; +virLockGuardNew; +virLockGuardUnlock; virMutexDestroy; virMutexInit; virMutexInitRecursive; diff --git a/src/util/virthread.c b/src/util/virthread.c index e89c1a09fb..a5a948985f 100644 --- a/src/util/virthread.c +++ b/src/util/virthread.c @@ -96,6 +96,32 @@ void virMutexUnlock(virMutex *m) pthread_mutex_unlock(&m->lock); } =20 +virLockGuard *virLockGuardNew(virMutex *m) +{ + virLockGuard *l =3D g_new0(virLockGuard, 1); + l->mutex =3D m; + + virMutexLock(l->mutex); + return l; +} + +void virLockGuardFree(virLockGuard *l) +{ + if (!l) + return; + + virLockGuardUnlock(l); + g_free(l); +} + +void virLockGuardUnlock(virLockGuard *l) +{ + if (!l) + 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..b394dbd226 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 *virLockGuardNew(virMutex *m); +void virLockGuardFree(virLockGuard *l); +void virLockGuardUnlock(virLockGuard *l); +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virLockGuard, virLockGuardFree); + int virRWLockInit(virRWLock *m) G_GNUC_WARN_UNUSED_RESULT; void virRWLockDestroy(virRWLock *m); =20 --=20 2.31.1