Merge tag 'sound-fix-4.21-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux-2.6-block.git] / lib / lockref.c
index 47169ed7e964f53dbc7735a3ad5b19c492878f45..3d468b53d4c931de6d76d0f51be6a07b3a066ecf 100644 (file)
@@ -80,6 +80,34 @@ int lockref_get_not_zero(struct lockref *lockref)
 }
 EXPORT_SYMBOL(lockref_get_not_zero);
 
+/**
+ * lockref_put_not_zero - Decrements count unless count <= 1 before decrement
+ * @lockref: pointer to lockref structure
+ * Return: 1 if count updated successfully or 0 if count would become zero
+ */
+int lockref_put_not_zero(struct lockref *lockref)
+{
+       int retval;
+
+       CMPXCHG_LOOP(
+               new.count--;
+               if (old.count <= 1)
+                       return 0;
+       ,
+               return 1;
+       );
+
+       spin_lock(&lockref->lock);
+       retval = 0;
+       if (lockref->count > 1) {
+               lockref->count--;
+               retval = 1;
+       }
+       spin_unlock(&lockref->lock);
+       return retval;
+}
+EXPORT_SYMBOL(lockref_put_not_zero);
+
 /**
  * lockref_get_or_lock - Increments count unless the count is 0 or dead
  * @lockref: pointer to lockref structure