From nobody Wed Feb 11 03:30:10 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=1633001174; cv=none; d=zohomail.com; s=zohoarc; b=l/yQ/IStZLltXWc/pxpwCI2ltOJgT5mwSGncXM8KuAPXtSYUJMJqLWDeAowZtZhq2Nbp86fmENajNUkY6k5vt/7wUyyo3ZE3G/hZKWs/CFeIaNnL+3mbtKYRhONOwC6F5lRphI7+hbKN7CLRO7Xyxs4mBu7T0gPMGcBK3/c+UqQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1633001174; 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=8eJCprshXhLHW2OLi5ag6K9HgZhoDa6Aas5JTkpvveA=; b=lhINgnEgQn2QbJypucUf8mhpKItPtpL7Ki37i85zswKBvgG0mdKt9YmYTrw1OUG1DDhrc6jhOGCRhPGfTxtZ+ec6mtgPgilwmCNRyojmu9wE9JqFlHMrM0bWZLD+W/Tu349aiYfIkLPoXk7r+pqMc+7hYD/8xnD7UAAYBxGXEmo= 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 1633001174709847.5773096725175; Thu, 30 Sep 2021 04:26:14 -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-372-vks17SP0PfuUMzIvfRxlYQ-1; Thu, 30 Sep 2021 07:26:11 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EA551A0CAB; Thu, 30 Sep 2021 11:26:06 +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 CB1C360C82; Thu, 30 Sep 2021 11:26:06 +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 9920E1806D02; Thu, 30 Sep 2021 11:26:06 +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 18UBPEuJ021104 for ; Thu, 30 Sep 2021 07:25:14 -0400 Received: by smtp.corp.redhat.com (Postfix) id A351A5F4EC; Thu, 30 Sep 2021 11:25:14 +0000 (UTC) Received: from work.redhat.com (unknown [10.39.194.122]) by smtp.corp.redhat.com (Postfix) with ESMTP id EACC45F4EB; Thu, 30 Sep 2021 11:25:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1633001173; 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=8eJCprshXhLHW2OLi5ag6K9HgZhoDa6Aas5JTkpvveA=; b=SNlx90O7wH4J9SAzmPmzIVToNj+h79AURrIrK9PfN+oysgw7S13aglYimqCiexbZkeDmd0 668npTO1xSy52k3orQsCb7FwuKHk/vSrShiucnStTSVfnzn7/jp5iCvzNxKJBPJTxZ+Vzb t89XZtVTTPI+YNWAI3Jt8hRoCY3kq10= X-MC-Unique: vks17SP0PfuUMzIvfRxlYQ-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH v3 2/9] virthread: Introduce virLockGuard Date: Thu, 30 Sep 2021 13:24:52 +0200 Message-Id: <20210930112459.11429-3-twiederh@redhat.com> In-Reply-To: <20210930112459.11429-1-twiederh@redhat.com> References: <20210930112459.11429-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.12 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: 1633001175531100001 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_autoptr(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 virLockGuardNew(m); /* `m` is locked, and released automatically on scope exit */ ... while (expression) { VIR_LOCK_GUARD lock2 =3D virLockGuardNew(...); /* similar */ } } Signed-off-by: Tim Wiederhake --- src/libvirt_private.syms | 3 +++ src/util/virthread.c | 26 ++++++++++++++++++++++++++ src/util/virthread.h | 11 +++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6de9d9aef1..f41674781d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3373,6 +3373,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..d896a6ad3a 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,12 @@ 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); +#define VIR_LOCK_GUARD g_autoptr(virLockGuard) G_GNUC_UNUSED + int virRWLockInit(virRWLock *m) G_GNUC_WARN_UNUSED_RESULT; void virRWLockDestroy(virRWLock *m); =20 --=20 2.31.1