]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
lockref: add 'lockref_get_or_lock() helper
authorLinus Torvalds <torvalds@linux-foundation.org>
Mon, 2 Sep 2013 18:14:19 +0000 (11:14 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Mon, 2 Sep 2013 18:14:19 +0000 (11:14 -0700)
This behaves like "lockref_get_not_zero()", but instead of doing nothing
if the count was zero, it returns with the lock held.

This allows callers to revalidate the lockref-protected data structure
if required even if the count was zero to begin with, and possibly
increment the count if it passes muster.

In particular, the dentry code wants this when it wants to turn an
RCU-protected dentry into a stable refcounted one: if the dentry count
it zero, but the sequence number still validates the dentry, we can take
a reference to it.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
include/linux/lockref.h

index 01233e01627abb3a9d708834c0dfc0c808463bba..0ea026092d1d5b34ab51596f4d324efe33db21e8 100644 (file)
@@ -53,6 +53,22 @@ static inline int lockref_get_not_zero(struct lockref *lockref)
        return retval;
 }
 
+/**
+ * lockref_get_or_lock - Increments count unless the count is 0
+ * @lockcnt: pointer to lockref structure
+ * Return: 1 if count updated successfully or 0 if count was zero
+ * and we got the lock instead.
+ */
+static inline int lockref_get_or_lock(struct lockref *lockref)
+{
+       spin_lock(&lockref->lock);
+       if (!lockref->count)
+               return 0;
+       lockref->count++;
+       spin_unlock(&lockref->lock);
+       return 1;
+}
+
 /**
  * lockref_put_or_lock - decrements count unless count <= 1 before decrement
  * @lockcnt: pointer to lockref structure