[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2 2/7] virlockspace: Introduce VIR_LOCK_SPACE_ACQUIRE_WAIT



This flag modifies the way the lock is acquired. It waits for the
lock to be set instead of usual set-or-fail logic that happens
without this flag.

Signed-off-by: Michal Privoznik <mprivozn redhat com>
---
 src/util/virlockspace.c | 14 ++++++++++----
 src/util/virlockspace.h |  1 +
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index 60bfef4c5f..d75a6d4a6e 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -122,6 +122,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
 {
     virLockSpaceResourcePtr res;
     bool shared = !!(flags & VIR_LOCK_SPACE_ACQUIRE_SHARED);
+    bool waitForLock = !!(flags & VIR_LOCK_SPACE_ACQUIRE_WAIT);
 
     if (VIR_ALLOC(res) < 0)
         return NULL;
@@ -159,7 +160,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
                 goto error;
             }
 
-            if (virFileLock(res->fd, shared, start, len, false) < 0) {
+            if (virFileLock(res->fd, shared, start, len, waitForLock) < 0) {
                 if (errno == EACCES || errno == EAGAIN) {
                     virReportError(VIR_ERR_RESOURCE_BUSY,
                                    _("Lockspace resource '%s' is locked"),
@@ -206,7 +207,7 @@ virLockSpaceResourceNew(virLockSpacePtr lockspace,
             goto error;
         }
 
-        if (virFileLock(res->fd, shared, start, len, false) < 0) {
+        if (virFileLock(res->fd, shared, start, len, waitForLock) < 0) {
             if (errno == EACCES || errno == EAGAIN) {
                 virReportError(VIR_ERR_RESOURCE_BUSY,
                                _("Lockspace resource '%s' is locked"),
@@ -625,11 +626,16 @@ int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
               lockspace, resname, flags, (unsigned long long)owner);
 
     virCheckFlags(VIR_LOCK_SPACE_ACQUIRE_SHARED |
-                  VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE, -1);
+                  VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE |
+                  VIR_LOCK_SPACE_ACQUIRE_WAIT, -1);
+
+    VIR_EXCLUSIVE_FLAGS_RET(VIR_LOCK_SPACE_ACQUIRE_WAIT,
+                            VIR_LOCK_SPACE_ACQUIRE_SHARED, -1);
 
     virMutexLock(&lockspace->lock);
 
-    if ((res = virHashLookup(lockspace->resources, resname))) {
+    if (!(flags & VIR_LOCK_SPACE_ACQUIRE_WAIT) &&
+        (res = virHashLookup(lockspace->resources, resname))) {
         if ((res->flags & VIR_LOCK_SPACE_ACQUIRE_SHARED) &&
             (flags & VIR_LOCK_SPACE_ACQUIRE_SHARED)) {
 
diff --git a/src/util/virlockspace.h b/src/util/virlockspace.h
index 24f2c89be6..bfaed34d5b 100644
--- a/src/util/virlockspace.h
+++ b/src/util/virlockspace.h
@@ -47,6 +47,7 @@ int virLockSpaceDeleteResource(virLockSpacePtr lockspace,
 typedef enum {
     VIR_LOCK_SPACE_ACQUIRE_SHARED     = (1 << 0),
     VIR_LOCK_SPACE_ACQUIRE_AUTOCREATE = (1 << 1),
+    VIR_LOCK_SPACE_ACQUIRE_WAIT       = (1 << 2),
 } virLockSpaceAcquireFlags;
 
 int virLockSpaceAcquireResource(virLockSpacePtr lockspace,
-- 
2.16.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]