summaryrefslogtreecommitdiff
path: root/include/linux/lockref.h
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-09-02 22:14:19 +0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-09-02 22:14:19 +0400
commitb3abd80250c13414bc258b53e57242feb159af91 (patch)
tree0120474080dfd6664a84beae85d3a2a1b0778e24 /include/linux/lockref.h
parent98474236f72e5a8b89c14cd7c74f0bb77a4b1a99 (diff)
downloadlinux-b3abd80250c13414bc258b53e57242feb159af91.tar.xz
lockref: add 'lockref_get_or_lock() helper
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>
Diffstat (limited to 'include/linux/lockref.h')
-rw-r--r--include/linux/lockref.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/include/linux/lockref.h b/include/linux/lockref.h
index 01233e01627a..0ea026092d1d 100644
--- a/include/linux/lockref.h
+++ b/include/linux/lockref.h
@@ -54,6 +54,22 @@ static inline int lockref_get_not_zero(struct lockref *lockref)
}
/**
+ * 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
* Return: 1 if count updated successfully or 0 if count <= 1 and lock taken