]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
netdev: Use automatic mutex management
authorTim Wiederhake <twiederh@redhat.com>
Wed, 16 Feb 2022 15:15:02 +0000 (16:15 +0100)
committerTim Wiederhake <twiederh@redhat.com>
Wed, 16 Mar 2022 09:54:25 +0000 (10:54 +0100)
Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virnetdev.c

index fcf679ec37afc2bba8f59bf90a44f7f19bcb936b..5df48af60c86451f985f0a4acce735e667348905 100644 (file)
@@ -3568,12 +3568,10 @@ virNetDevReserveName(const char *name)
     idstr = name + strlen(virNetDevGenNames[type].prefix);
 
     if (virStrToLong_ui(idstr, NULL, 10, &id) >= 0) {
-        virMutexLock(&virNetDevGenNames[type].mutex);
+        VIR_LOCK_GUARD lock = virLockGuardLock(&virNetDevGenNames[type].mutex);
 
         if (virNetDevGenNames[type].lastID < (int)id)
             virNetDevGenNames[type].lastID = id;
-
-        virMutexUnlock(&virNetDevGenNames[type].mutex);
     }
 }
 
@@ -3599,7 +3597,6 @@ virNetDevReserveName(const char *name)
 int
 virNetDevGenerateName(char **ifname, virNetDevGenNameType type)
 {
-    int id;
     const char *prefix = virNetDevGenNames[type].prefix;
     double maxIDd = pow(10, IFNAMSIZ - 1 - strlen(prefix));
     int maxID = INT_MAX;
@@ -3617,16 +3614,15 @@ virNetDevGenerateName(char **ifname, virNetDevGenNameType type)
 
     do {
         g_autofree char *try = NULL;
+        int id = 0;
 
-        virMutexLock(&virNetDevGenNames[type].mutex);
-
-        id = ++virNetDevGenNames[type].lastID;
+        VIR_WITH_OBJECT_LOCK_GUARD(&virNetDevGenNames[type].mutex) {
+            id = ++virNetDevGenNames[type].lastID;
 
-        /* reset before overflow */
-        if (virNetDevGenNames[type].lastID >= maxID)
-            virNetDevGenNames[type].lastID = -1;
-
-        virMutexUnlock(&virNetDevGenNames[type].mutex);
+            /* reset before overflow */
+            if (virNetDevGenNames[type].lastID >= maxID)
+                virNetDevGenNames[type].lastID = -1;
+        }
 
         if (*ifname)
             try = g_strdup_printf(*ifname, id);